From 54fe40e588413172c9102a7c1db1494e95b827ed Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sun, 12 Feb 2023 16:24:16 +0100 Subject: [PATCH] Add logging to `exec` and make it crashless, `requests` and `captions` folders support for compressing, clean captions support for videos being livestreams and videos starting with `-` --- main.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/main.cpp b/main.cpp index 0a90700..303ffb9 100644 --- a/main.cpp +++ b/main.cpp @@ -23,9 +23,9 @@ void createDirectory(string path), treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, string channelToTreat), treatChannels(unsigned short threadId), deleteDirectory(string path), - addChannelToTreat(unsigned short threadId, string channelId); + addChannelToTreat(unsigned short threadId, string channelId), + exec(unsigned short threadId, string cmd, bool debug = true); string getHttps(string url), - exec(unsigned short threadId, string cmd), join(vector parts, string delimiter); size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp); bool doesFileExist(string filePath), @@ -189,7 +189,9 @@ void treatChannels(unsigned short threadId) // Note that compressing the French most subscribers channel took 4 minutes and 42 seconds. PRINT("Starting compression...") // As I haven't found any well-known library that compress easily a directory, I have chosen to rely on `zip` cli. - exec(threadId, "cd " + channelToTreatDirectory + " && ls | zip ../" + channelToTreat + ".zip -@"); + // We precise no `debug`ging, as otherwise the zipping operation doesn't work as expected. + // As the zipping process isn't recursive, we can't just rely on `ls`, but we are obliged to use `find`. + exec(threadId, "cd " + channelToTreatDirectory + " && find | zip ../" + channelToTreat + ".zip -@", false); PRINT("Compression finished, started deleting initial directory...") deleteDirectory(channelToTreatDirectory); @@ -586,9 +588,10 @@ void treatChannelOrVideo(unsigned short threadId, bool isChannel, string id, str // Firstly download all not automatically generated captions. // The underscore in `-o` argument is used to not end up with hidden files. + // We are obliged to precise the video id after `--`, otherwise if the video id starts with `-` it's considered as an argument. string cmdCommonPrefix = "yt-dlp --skip-download ", - cmdCommonPostfix = " '" + videoId + "' -o '" + channelCaptionsToTreatDirectory + "_'"; - string cmd = cmdCommonPrefix + "--all-subs" + cmdCommonPostfix; + cmdCommonPostfix = " -o '" + channelCaptionsToTreatDirectory + "_' -- " + videoId; + string cmd = cmdCommonPrefix + "--sub-lang all,-live_chat" + cmdCommonPostfix; exec(threadId, cmd); // Secondly download the automatically generated captions. @@ -655,25 +658,22 @@ string join(vector parts, string delimiter) return result; } -string exec(unsigned short threadId, string cmd) +void exec(unsigned short threadId, string cmd, bool debug) { - ostringstream toString; - toString << threadId; - string threadIdStr = toString.str(), debugCommonFilePath = DEBUG_DIRECTORY + threadIdStr; - cmd += " >> " + debugCommonFilePath + ".out"; - cmd += " 2>> " + debugCommonFilePath + ".err"; - array buffer; - string result; - unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); - if (!pipe) + if(debug) { - throw runtime_error("popen() failed!"); + ostringstream toString; + toString << threadId; + string initialCmd = cmd, + threadIdStr = toString.str(), + debugCommonFilePath = DEBUG_DIRECTORY + threadIdStr, + debugOutFilePath = debugCommonFilePath + ".out", + debugErrFilePath = debugCommonFilePath + ".err"; + cmd += " >> " + debugOutFilePath; + cmd += " 2>> " + debugErrFilePath; + cmd += "; echo \"" + initialCmd + "\" | tee -a " + debugOutFilePath + " " + debugErrFilePath; } - while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) - { - result += buffer.data(); - } - return result; + system(cmd.c_str()); } bool writeFile(unsigned short threadId, string filePath, string option, string toWrite)