From 0f15bb023593298a0245ef8213eb20d9180ba67a Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sun, 22 Jan 2023 15:15:27 +0100 Subject: [PATCH] #11: Add the discovering of channels having commented on ended livestreams --- main.cpp | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 5cef5f4..e41bb2b 100644 --- a/main.cpp +++ b/main.cpp @@ -488,11 +488,11 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str { if(item.contains("liveStreamingDetails")) { - PRINT(item["id"]) + string videoId = item["id"]; + //PRINT(videoId) 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"]; @@ -500,12 +500,48 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str { string channelId = item["snippet"]["authorChannelId"]; addChannelToTreat(threadId, channelId); - PRINT("Found: " << channelId) } } else { - PRINT("no more streaming") + // As there isn't the usual pagination mechanism for these ended livestreams, we proceed in an uncertain way as follows. + set messageIds; + unsigned long long lastMessageTimestampRelativeMsec = 0; + while(true) + { + string time = to_string(lastMessageTimestampRelativeMsec); + json data = getJson(threadId, "liveChats?part=snippet&id=" + videoId + "&time=" + time, false, id), + snippet = data["items"][0]["snippet"]; + if(snippet.empty()) + { + break; + } + json firstMessage = snippet[0]; + string firstMessageId = firstMessage["id"]; + // We verify that we don't skip any message by verifying that the first message was already treated if we already treated some messages. + if(!messageIds.empty() && messageIds.find(firstMessageId) == messageIds.end()) + { + PRINT("The verification that we don't skip any message failed!") + exit(EXIT_FAILURE); + } + for(const auto& message : snippet) + { + string messageId = message["id"]; + if(messageIds.find(messageId) == messageIds.end()) + { + messageIds.insert(messageId); + string channelId = message["authorChannelId"]; + addChannelToTreat(threadId, channelId); + } + } + json lastMessage = snippet.back(); + // If there isn't any new message, then we stop the retrieving. + if(lastMessageTimestampRelativeMsec == lastMessage["videoOffsetTimeMsec"]) + { + break; + } + lastMessageTimestampRelativeMsec = lastMessage["videoOffsetTimeMsec"]; + } } } }