aboutsummaryrefslogtreecommitdiff
path: root/src/Downloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Downloader.cpp')
-rw-r--r--src/Downloader.cpp54
1 files changed, 46 insertions, 8 deletions
diff --git a/src/Downloader.cpp b/src/Downloader.cpp
index 57a0349..75cef1c 100644
--- a/src/Downloader.cpp
+++ b/src/Downloader.cpp
@@ -2,6 +2,7 @@
#include "../include/Storage.hpp"
#include "../include/NetUtils.hpp"
#include "../include/Notification.hpp"
+#include "../include/StringUtils.hpp"
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
@@ -147,7 +148,44 @@ namespace QuickMedia {
return download_speed_text;
}
- YoutubeDlDownloader::YoutubeDlDownloader(const std::string &url, const std::string &output_filepath, bool no_video) : Downloader(url, output_filepath), no_video(no_video) {
+ static bool parse_ytdl_download_line(const char *line, size_t size, std::string &progress, std::string &content_size, std::string &download_speed) {
+ progress.clear();
+ content_size.clear();
+ download_speed.clear();
+
+ const std::string_view line_v(line, size);
+ size_t index = line_v.find("[download]", 0);
+ if(index == std::string::npos)
+ return false;
+
+ index += 10;
+ size_t end_index = line_v.find(" of ~ ", index);
+ if(end_index == std::string::npos)
+ return false;
+
+ progress = line_v.substr(index, end_index - index);
+ index = end_index + 6;
+
+ end_index = line_v.find(" at", index);
+ if(end_index == std::string::npos)
+ return false;
+
+ content_size = line_v.substr(index, end_index - index);
+ index = end_index + 3;
+
+ end_index = line_v.find(" ETA ", index);
+ if(end_index == std::string::npos)
+ return false;
+
+ download_speed = line_v.substr(index, end_index - index);
+
+ progress = strip(progress);
+ content_size = strip(content_size);
+ download_speed = strip(download_speed);
+ return true;
+ }
+
+ YoutubeDlDownloader::YoutubeDlDownloader(const char *yt_dl_name, const std::string &url, const std::string &output_filepath, bool no_video) : Downloader(url, output_filepath), no_video(no_video), yt_dl_name(yt_dl_name) {
// youtube-dl requires a file extension for the file
if(this->output_filepath.find('.') == std::string::npos)
this->output_filepath += ".mkv";
@@ -161,7 +199,7 @@ namespace QuickMedia {
bool YoutubeDlDownloader::start() {
remove(output_filepath.c_str());
- std::vector<const char*> args = { "youtube-dl", "--no-warnings", "--no-continue", "--output", output_filepath.c_str(), "--newline" };
+ std::vector<const char*> args = { yt_dl_name, "--no-warnings", "--no-continue", "--output", output_filepath.c_str(), "--newline" };
if(no_video) {
args.push_back("-f");
args.push_back("bestaudio/best");
@@ -185,9 +223,9 @@ namespace QuickMedia {
// TODO: Remove this async task and make the fd non blocking instead
youtube_dl_output_reader = AsyncTask<bool>([this]{
char line[128];
- char progress_c[11];
- char content_size_c[21];
- char download_speed_c[21];
+ std::string progress_c;
+ std::string content_size_c;
+ std::string download_speed_c;
while(true) {
if(fgets(line, sizeof(line), read_program_file)) {
@@ -197,10 +235,10 @@ namespace QuickMedia {
--len;
}
- if(sscanf(line, "[download] %10s of %20s at %20s", progress_c, content_size_c, download_speed_c) == 3) {
+ if(parse_ytdl_download_line(line, len, progress_c, content_size_c, download_speed_c)) {
std::lock_guard<std::mutex> lock(progress_update_mutex);
- if(strcmp(progress_c, "Unknown") != 0 && strcmp(content_size_c, "Unknown") != 0) {
+ if(strcmp(progress_c.c_str(), "Unknown") != 0 && strcmp(content_size_c.c_str(), "Unknown") != 0) {
std::string progress_str = progress_c;
progress_text = progress_str + " of " + content_size_c;
if(progress_str.back() == '%') {
@@ -212,7 +250,7 @@ namespace QuickMedia {
}
}
- if(strcmp(download_speed_c, "Unknown") == 0)
+ if(strcmp(download_speed_c.c_str(), "Unknown") == 0)
download_speed_text = "Unknown/s";
else
download_speed_text = download_speed_c;