From 7315c428c0ac2bc0c13c5e63f806c4e6e18c56aa Mon Sep 17 00:00:00 2001 From: Moss Date: Wed, 28 Sep 2022 23:10:35 -0400 Subject: [PATCH] Options: Added 'options' Class This class handles all command line arguments. --- src/episode.cpp | 9 ++++ src/episode.h | 1 + src/main.cpp | 128 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 118 insertions(+), 20 deletions(-) diff --git a/src/episode.cpp b/src/episode.cpp index 44f82e3..ccc85a1 100644 --- a/src/episode.cpp +++ b/src/episode.cpp @@ -27,6 +27,15 @@ namespace dropout_dl { } } + bool contains(const std::string& string, const std::string& test_str) { + for (int i = 0; i < string.size() - test_str.size(); i++) { + if (string.substr(i, test_str.size()) == test_str) { + return true; + } + } + return false; + } + #if defined(__WIN32__) #include msec_t time_ms(void) diff --git a/src/episode.h b/src/episode.h index 1fe4453..ec5523a 100644 --- a/src/episode.h +++ b/src/episode.h @@ -14,6 +14,7 @@ namespace dropout_dl { void replace_all(std::string& str, const std::string& from, const std::string& to); + bool contains(const std::string& string, const std::string& test_str); #if defined(__WIN32__) #include diff --git a/src/main.cpp b/src/main.cpp index 59dbd2e..3ffe829 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -294,48 +294,136 @@ std::vector get_cookies(bool verbose = false) { } } +/* + * + */ + +class options { +public: + + std::string url; + bool verbose = false; + bool cookies_forced = false; + std::string quality; + std::vector cookies; + + static std::vector convert_program_args(int argc, char** argv) { + std::vector out; + for (int i = 1; i < argc; i++) { + out.emplace_back(argv[i]); + } + return out; + } + + options(int argc, char** argv) { + std::vector args = convert_program_args(argc, argv); + + bool next_is_quality = false; + bool next_is_cookie = false; + for (const auto& arg : args) { + if (next_is_quality) { + quality = arg; + next_is_quality = false; + continue; + } + if (next_is_cookie) { + cookies.emplace_back(arg); + if (cookies.size() == 2) { + next_is_quality = false; + } + continue; + } + else if (arg.substr(0, 2) != "--") { + url = arg; + } + else { + if (arg == "--verbose") { + verbose = true; + } else if (arg == "--quality") { + next_is_quality = true; + } + else if (arg == "--force-cookies") { + next_is_cookie = true; + cookies_forced = true; + } + else if (arg == "--help") { + std::cout << "Usage: dropout-dl [OPTIONS] [OPTIONS]\n" + "\n" + "Options:\n" + "\t--help\t\t\t\tDisplay this message\n" + "\t--quality\t\t\tSet the quality of the downloaded video. Quality can be set to 'all' which\n" + "\t\t\t\t\t\twill download all qualities and place them into separate folders\n" + "\t--verbose\t\t\tDisplay debug information while running\n" + "\t--force-cookies\t\tInterpret the next to arguments as authentication cookie and session cookie\n" + << std::endl; + + exit(0); + } + } + } + + if (quality.empty()) { + quality = "1080p"; + } + } +}; + int main(int argc, char** argv) { - bool verbose = false; - std::string quality = "all"; + options options(argc, argv); + + std::cout << "quality: " << options.quality << std::endl; + std::cout << "verbose: " << options.verbose << std::endl; + std::cout << "url: \"" << options.url << '"' << std::endl; std::string firefox_profile; std::string chrome_profile; - std::string episode_url; - - std::vector cookies; - std::string video_data; - if (argc > 1) { - episode_url = argv[1]; - if (verbose) { - std::cout << "Got episode url: " << episode_url << " from program arguments\n"; - } - } - else { + if (options.url.empty()) { std::cout << "Enter episode url: "; - std::cin >> episode_url; + std::cin >> options.url; + } + else if (options.verbose) { + std::cout << "Got episode url: " << options.url << " from program arguments\n"; } - cookies = get_cookies(verbose); + if (!options.cookies_forced) { + options.cookies = get_cookies(options.verbose); + } - dropout_dl::episode ep(episode_url, cookies, verbose); + dropout_dl::episode ep(options.url, options.cookies, options.verbose); if (!std::filesystem::is_directory(ep.series)) { std::filesystem::create_directories(ep.series); - if (verbose) { + if (options.verbose) { std::cout << "Creating series directory" << '\n'; } } - if (quality == "all") { + if (options.quality == "all") { for (const auto& possible_quality : ep.qualities) { if (!std::filesystem::is_directory(ep.series + "/" + possible_quality)) { std::filesystem::create_directories(ep.series + "/" + possible_quality); - if (verbose) { + if (options.verbose) { std::cout << "Creating series directory" << '\n'; } } @@ -348,7 +436,7 @@ int main(int argc, char** argv) { else { std::fstream out(ep.series + "/" + ep.filename, std::ios_base::in | std::ios_base::out | std::ios_base::trunc); - out << ep.get_video_data(quality) << std::endl; + out << ep.get_video_data(options.quality) << std::endl; } return 0;