#7: Make commentsCount
and requestsPerChannel
compatible with multithreading
This commit is contained in:
parent
7456685f2b
commit
f114aac0cf
21
main.cpp
21
main.cpp
@ -42,9 +42,9 @@ set<string> channelsAlreadyTreated;
|
||||
map<unsigned int, string> channelsToTreat;
|
||||
map<string, unsigned int> channelsToTreatRev;
|
||||
vector<string> keys;
|
||||
unsigned int commentsCount = 0,
|
||||
commentsPerSecondCount = 0,
|
||||
requestsPerChannel = 0;
|
||||
unsigned int commentsPerSecondCount = 0;
|
||||
map<unsigned short, unsigned int> commentsCountThreads,
|
||||
requestsPerChannelThreads;
|
||||
unsigned short THREADS_NUMBER = 1;
|
||||
string CHANNELS_DIRECTORY = "channels/",
|
||||
CHANNELS_FILE_PATH = "channels.txt",
|
||||
@ -160,6 +160,9 @@ void treatChannels(unsigned short threadId)
|
||||
|
||||
PRINT(threadId, "Treating channel " << channelToTreat << " (treated: " << channelsAlreadyTreated.size() << ", to treat: " << channelsToTreat.size() << ")")
|
||||
|
||||
commentsCountThreads[threadId] = 0;
|
||||
requestsPerChannelThreads[threadId] = 0;
|
||||
|
||||
channelsToTreat.erase(channelsToTreatRev[channelToTreat]);
|
||||
channelsToTreatRev.erase(channelToTreat);
|
||||
|
||||
@ -181,14 +184,13 @@ void treatChannels(unsigned short threadId)
|
||||
deleteDirectory(channelToTreatDirectory);
|
||||
PRINT(threadId, "Deleting directory finished.")
|
||||
|
||||
PRINT(threadId, commentsCount << " comments were found for this channel.")
|
||||
commentsCount = 0;
|
||||
requestsPerChannel = 0;
|
||||
PRINT(threadId, commentsCountThreads[threadId] << " comments were found for this channel.")
|
||||
}
|
||||
|
||||
channelsAlreadyTreatedAndToTreatMutex.unlock();
|
||||
}
|
||||
|
||||
// Have to pay attention not to recursively call this function with another channel otherwise we break the ability of the program to halt at any top level channel.
|
||||
void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, string channelToTreat)
|
||||
{
|
||||
string pageToken = "";
|
||||
@ -303,6 +305,7 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str
|
||||
}
|
||||
if(isChannel)
|
||||
{
|
||||
// `CHANNELS`
|
||||
string pageToken = "";
|
||||
while(true)
|
||||
{
|
||||
@ -357,7 +360,7 @@ void treatComment(unsigned short threadId, json comment, string channelId)
|
||||
string channelId = snippet["authorChannelId"]["value"];
|
||||
addChannelToTreat(threadId, channelId);
|
||||
}
|
||||
commentsCount++;
|
||||
commentsCountThreads[threadId]++;
|
||||
commentsPerSecondCount++;
|
||||
}
|
||||
|
||||
@ -478,8 +481,8 @@ json getJson(unsigned short threadId, string url, bool usingYoutubeDataApiv3, st
|
||||
}
|
||||
|
||||
ostringstream toString;
|
||||
toString << CHANNELS_DIRECTORY << directoryPath << "/" << requestsPerChannel << ".json";
|
||||
requestsPerChannel++;
|
||||
toString << CHANNELS_DIRECTORY << directoryPath << "/" << requestsPerChannelThreads[threadId] << ".json";
|
||||
requestsPerChannelThreads[threadId]++;
|
||||
writeFile(threadId, toString.str(), "w", url + "\n" + content);
|
||||
|
||||
return data;
|
||||
|
Loading…
Reference in New Issue
Block a user