From 68b1f9a77f1ee840f82a5f09e5f935b7738a4663 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sun, 22 Jan 2023 04:00:11 +0100 Subject: [PATCH] #11: Add current livestreams support to discover channels --- main.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index b31bb95..5cef5f4 100644 --- a/main.cpp +++ b/main.cpp @@ -25,7 +25,8 @@ void createDirectory(string path), deleteDirectory(string path), addChannelToTreat(unsigned short threadId, string channelId); string getHttps(string url), - exec(string cmd); + exec(string cmd), + join(vector parts, string delimiter); size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp); bool doesFileExist(string filePath), 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; } } + // `LIVES` + pageToken = ""; + string playlistId = "UU" + id.substr(2); + vector 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++; } +string join(vector 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) { array buffer;