Instead of looping on items where we expect only one to be, we just use items[0]

This commit is contained in:
Benjamin Loison 2023-01-22 02:19:26 +01:00
parent 993d0b9771
commit d2391e5d54

119
main.cpp
View File

@ -313,38 +313,32 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str
while(true) while(true)
{ {
json data = getJson(threadId, "channels?part=channels&id=" + id + (pageToken == "" ? "" : "&pageToken=" + pageToken), false, id), json data = getJson(threadId, "channels?part=channels&id=" + id + (pageToken == "" ? "" : "&pageToken=" + pageToken), false, id),
items = data["items"]; channelSections = data["items"][0]["channelSections"];
for(const auto& item : items) for(const auto& channelSection : channelSections)
{ {
json channelSections = item["channelSections"]; for(const auto& sectionChannel : channelSection["sectionChannels"])
for(const auto& channelSection : channelSections)
{ {
for(const auto& sectionChannel : channelSection["sectionChannels"]) string channelId = sectionChannel["channelId"];
{ addChannelToTreat(threadId, channelId);
string channelId = sectionChannel["channelId"];
addChannelToTreat(threadId, channelId);
}
} }
if(channelSections.size() == 1) }
if(channelSections.size() == 1)
{
json channelSection = channelSections[0];
if(!channelSection["nextPageToken"].is_null())
{ {
json channelSection = channelSections[0]; pageToken = channelSection["nextPageToken"];
if(!channelSection["nextPageToken"].is_null())
{
pageToken = channelSection["nextPageToken"];
}
else
{
goto breakChannelsTreatment;
}
} }
else else
{ {
goto breakChannelsTreatment; break;
} }
} }
else
{
break;
}
} }
breakChannelsTreatment:
;
// `COMMUNITY` // `COMMUNITY`
pageToken = ""; pageToken = "";
while(true) while(true)
@ -410,57 +404,54 @@ breakChannelsTreatment:
while(true) while(true)
{ {
json data = getJson(threadId, "channels?part=playlists&id=" + id + (pageToken == "" ? "" : "&pageToken=" + pageToken), false, id), json data = getJson(threadId, "channels?part=playlists&id=" + id + (pageToken == "" ? "" : "&pageToken=" + pageToken), false, id),
items = data["items"]; playlistSections = data["items"][0]["playlistSections"];
for(const auto& item : items) for(const auto& playlistSection : playlistSections)
{ {
for(const auto& playlistSection : item["playlistSections"]) for(const auto& playlist : playlistSection["playlists"])
{ {
for(const auto& playlist : playlistSection["playlists"]) string playlistId = playlist["id"];
//PRINT(threadId, playlistId)
string pageToken = "";
while(true)
{ {
string playlistId = playlist["id"]; json data = getJson(threadId, "playlistItems?part=contentDetails,snippet,status&playlistId=" + playlistId + "&maxResults=50&pageToken=" + pageToken, true, id),
//PRINT(threadId, playlistId) items = data["items"];
string pageToken = ""; for(const auto& item : items)
while(true)
{ {
json data = getJson(threadId, "playlistItems?part=contentDetails,snippet,status&playlistId=" + playlistId + "&maxResults=50&pageToken=" + pageToken, true, id), json snippet = item["snippet"];
items = data["items"]; string privacyStatus = item["status"]["privacyStatus"];
for(const auto& item : items) // `5-CXVU8si3A` in `PLTYUE9O6WCrjQsnOm56rMMNmFy_A-SjUx` has its privacy status on `privacyStatusUnspecified` and is inaccessible.
// `GMiVi8xkEXA` in `PLTYUE9O6WCrgNpeSiryP8LYVX-7tOJ1f1` has its privacy status on `private`.
// Of course `commentThreads?videoId=` doesn't work for these videos (same result on YouTube UI).
// By hypothesis that the discovery algorithm never ends we can't postpone the treatment of these unlisted videos, because we can find such unlisted videos at any point in time (before or after the given channel treatment).
// Maybe modifying this hypothesis would make sense, otherwise we have to treat them right-away (note that except code architecture, there is no recursion problem as documented on this function).
if(privacyStatus != "public" && privacyStatus != "private" && snippet["title"] != "Deleted video")
{ {
json snippet = item["snippet"]; string videoId = snippet["resourceId"]["videoId"],
string privacyStatus = item["status"]["privacyStatus"]; channelId = snippet["videoOwnerChannelId"];
// `5-CXVU8si3A` in `PLTYUE9O6WCrjQsnOm56rMMNmFy_A-SjUx` has its privacy status on `privacyStatusUnspecified` and is inaccessible. PRINT("Found non public video (" << videoId << ") in: " << playlistId)
// `GMiVi8xkEXA` in `PLTYUE9O6WCrgNpeSiryP8LYVX-7tOJ1f1` has its privacy status on `private`. string channelUnlistedVideosFilePath = CHANNELS_DIRECTORY + UNLISTED_VIDEOS_FILE_PATH;
// Of course `commentThreads?videoId=` doesn't work for these videos (same result on YouTube UI). bool doesChannelUnlistedVideosFileExist = doesFileExist(channelUnlistedVideosFilePath);
// By hypothesis that the discovery algorithm never ends we can't postpone the treatment of these unlisted videos, because we can find such unlisted videos at any point in time (before or after the given channel treatment). writeFile(threadId, channelUnlistedVideosFilePath, !doesChannelUnlistedVideosFileExist ? "w" : "a", (!doesChannelUnlistedVideosFileExist ? "" : "\n") + channelId);
// Maybe modifying this hypothesis would make sense, otherwise we have to treat them right-away (note that except code architecture, there is no recursion problem as documented on this function). }
if(privacyStatus != "public" && privacyStatus != "private" && snippet["title"] != "Deleted video") if(snippet.contains("videoOwnerChannelId"))
{
// There isn't any `videoOwnerChannelId` to retrieve for `5-CXVU8si3A` for instance.
string channelId = snippet["videoOwnerChannelId"];
if(channelId != id)
{ {
string videoId = snippet["resourceId"]["videoId"], addChannelToTreat(threadId, channelId);
channelId = snippet["videoOwnerChannelId"];
PRINT("Found non public video (" << videoId << ") in: " << playlistId)
string channelUnlistedVideosFilePath = CHANNELS_DIRECTORY + UNLISTED_VIDEOS_FILE_PATH;
bool doesChannelUnlistedVideosFileExist = doesFileExist(channelUnlistedVideosFilePath);
writeFile(threadId, channelUnlistedVideosFilePath, !doesChannelUnlistedVideosFileExist ? "w" : "a", (!doesChannelUnlistedVideosFileExist ? "" : "\n") + channelId);
}
if(snippet.contains("videoOwnerChannelId"))
{
// There isn't any `videoOwnerChannelId` to retrieve for `5-CXVU8si3A` for instance.
string channelId = snippet["videoOwnerChannelId"];
if(channelId != id)
{
addChannelToTreat(threadId, channelId);
}
} }
} }
if(data.contains("nextPageToken")) }
{ if(data.contains("nextPageToken"))
pageToken = data["nextPageToken"]; {
} pageToken = data["nextPageToken"];
else }
{ else
break; {
} break;
} }
} }
} }