#11: Add current livestreams support to discover channels

This commit is contained in:
Benjamin Loison 2023-01-22 04:00:11 +01:00
parent c17a33d181
commit 68b1f9a77f
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -25,7 +25,8 @@ void createDirectory(string path),
deleteDirectory(string path), deleteDirectory(string path),
addChannelToTreat(unsigned short threadId, string channelId); addChannelToTreat(unsigned short threadId, string channelId);
string getHttps(string url), string getHttps(string url),
exec(string cmd); exec(string cmd),
join(vector<string> parts, string delimiter);
size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp); size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp);
bool doesFileExist(string filePath), bool doesFileExist(string filePath),
writeFile(unsigned short threadId, string filePath, string option, string toWrite); writeFile(unsigned short threadId, string filePath, string option, string toWrite);
@ -465,6 +466,60 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str
break; break;
} }
} }
// `LIVES`
pageToken = "";
string playlistId = "UU" + id.substr(2);
vector<string> videoIds;
while(true)
{
json data = getJson(threadId, "playlistItems?part=contentDetails,snippet,status&playlistId=" + playlistId + "&maxResults=50&pageToken=" + pageToken, true, id, returnErrorIfPlaylistNotFound),
items = data["items"];
for(const auto& item : items)
{
string videoId = item["snippet"]["resourceId"]["videoId"];
videoIds.push_back(videoId);
}
bool hasNextPageToken = data.contains("nextPageToken");
if(videoIds.size() == 50 || !hasNextPageToken)
{
json data = getJson(threadId, "videos?part=contentDetails,id,liveStreamingDetails,localizations,player,snippet,statistics,status,topicDetails&id=" + join(videoIds, ","), true, id),
items = data["items"];
for(const auto& item : items)
{
if(item.contains("liveStreamingDetails"))
{
PRINT(item["id"])
json liveStreamingDetails = item["liveStreamingDetails"];
if(liveStreamingDetails.contains("activeLiveChatId"))
{
PRINT("streaming")
string activeLiveChatId = liveStreamingDetails["activeLiveChatId"];
json data = getJson(threadId, "liveChat/messages?part=snippet,authorDetails&liveChatId=" + activeLiveChatId, true, id),
items = data["items"];
for(const auto& item : items)
{
string channelId = item["snippet"]["authorChannelId"];
addChannelToTreat(threadId, channelId);
PRINT("Found: " << channelId)
}
}
else
{
PRINT("no more streaming")
}
}
}
videoIds.clear();
}
if(hasNextPageToken)
{
pageToken = data["nextPageToken"];
}
else
{
break;
}
}
} }
} }
@ -501,6 +556,21 @@ void treatComment(unsigned short threadId, json comment, string channelId)
commentsPerSecondCount++; commentsPerSecondCount++;
} }
string join(vector<string> parts, string delimiter)
{
string result = "";
unsigned int partsSize = parts.size();
for(unsigned int partsIndex = 0; partsIndex < partsSize; partsIndex++)
{
result += parts[partsIndex];
if(partsIndex < partsSize - 1)
{
result += delimiter;
}
}
return result;
}
string exec(string cmd) string exec(string cmd)
{ {
array<char, 128> buffer; array<char, 128> buffer;