From 27cd5c3a6450d80aff5605d9f7f92940a6094ce1 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sun, 8 Jan 2023 18:26:20 +0100 Subject: [PATCH] Fix #24: Stop using macros for user inputs to notably make releases --- main.cpp | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index e350bfb..67dfa97 100644 --- a/main.cpp +++ b/main.cpp @@ -29,11 +29,9 @@ 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); -#define USE_YT_LEMNOSLIFE_COM_NO_KEY_SERVICE -#define THREADS_NUMBER 10 - #define PRINT(threadId, x) { ostringstream toPrint; toPrint << threadId << ": " << x; print(&toPrint); } #define DEFAULT_THREAD_ID 0 +#define MAIN_PRINT(x) PRINT(DEFAULT_THREAD_ID, x) mutex printMutex, channelsAlreadyTreatedAndToTreatMutex, @@ -44,13 +42,38 @@ vector keys; unsigned int commentsCount = 0, commentsPerSecondCount = 0, requestsPerChannel = 0; +unsigned short THREADS_NUMBER = 1; string CHANNELS_DIRECTORY = "channels/", CHANNELS_FILE_PATH = "channels.txt", KEYS_FILE_PATH = "keys.txt", apiKey = ""; // Will firstly be filled with `KEYS_FILE_PATH` first line. +bool USE_YT_LEMNOSLIFE_COM_NO_KEY_SERVICE = false; -int main() +int main(int argc, char *argv[]) { + for(unsigned short argvIndex = 1; argvIndex < argc; argvIndex++) + { + string argvStr = string(argv[argvIndex]); + if(argvStr == "--no-keys") + { + USE_YT_LEMNOSLIFE_COM_NO_KEY_SERVICE = true; + } + else if(argvStr.rfind("--threads=", 0) == 0) + { + THREADS_NUMBER = atoi(argvStr.substr(10).c_str()); + } + else if(argvStr == "-h" || argvStr == "--help") + { + MAIN_PRINT("Usage: " << argv[0] << " [--help/-h] [--no-keys] [--threads=N]") + exit(0); + } + else + { + MAIN_PRINT("Unrecognized parameter " << argvStr) + exit(1); + } + } + // The starting set should be written to `CHANNELS_FILE_PATH`. // To resume this algorithm after a shutdown, just restart it after having deleted the last channel folders in `CHANNELS_DIRECTORY` being treated. // On a restart, `CHANNELS_FILE_PATH` is read and every channel not found in `CHANNELS_DIRECTORY` is added to `channelsToTreat` or `channelsToTreat` otherwise before continuing, as if `CHANNELS_FILE_PATH` was containing a **treated** starting set. @@ -71,18 +94,18 @@ int main() channelsAlreadyTreated.insert(channelId); } - PRINT(DEFAULT_THREAD_ID, channelsToTreat.size() << " channel(s) to treat") - PRINT(DEFAULT_THREAD_ID, channelsAlreadyTreated.size() << " channel(s) already treated") + MAIN_PRINT(channelsToTreat.size() << " channel(s) to treat") + MAIN_PRINT(channelsAlreadyTreated.size() << " channel(s) already treated") - thread threads[THREADS_NUMBER]; + vector threads; for(unsigned short threadsIndex = 0; threadsIndex < THREADS_NUMBER; threadsIndex++) { - threads[threadsIndex] = thread(treatChannels, threadsIndex + 1); + threads.push_back(thread(treatChannels, threadsIndex + 1)); } while(true) { - PRINT(DEFAULT_THREAD_ID, "Comments per second: " << commentsPerSecondCount) + MAIN_PRINT("Comments per second: " << commentsPerSecondCount) commentsPerSecondCount = 0; sleep(1); } @@ -356,12 +379,9 @@ vector getFileContent(string filePath) json getJson(unsigned short threadId, string url, string directoryPath, getJsonBehavior behavior) { -#ifdef USE_YT_LEMNOSLIFE_COM_NO_KEY_SERVICE - string finalUrl = "https://yt.lemnoslife.com/noKey/" + url; -#else - string finalUrl = "https://www.googleapis.com/youtube/v3/" + url + "&key=" + apiKey; -#endif - string content = getHttps(finalUrl); + string finalUrl = USE_YT_LEMNOSLIFE_COM_NO_KEY_SERVICE ? "https://yt.lemnoslife.com/noKey/" + url : + "https://www.googleapis.com/youtube/v3/" + url + "&key=" + apiKey, + content = getHttps(finalUrl); json data; try {