From 286fbb969bfda35bfad73bc167aed3ebdb84337a Mon Sep 17 00:00:00 2001 From: Moss Date: Tue, 7 Feb 2023 22:21:21 -0800 Subject: [PATCH] Season: Changed How Episode Numbers Are Parsed For Seasons Now episode numbers are scraped from the season page rather than counting. This allow for specials (which dont have an episode number) to be saved with a slightly different format. --- src/episode.cpp | 6 +++++- src/episode.h | 2 +- src/season.cpp | 11 +++++++---- src/season.h | 2 +- src/util.cpp | 26 ++++++++++++++++++++++++-- src/util.h | 13 ++++++++++++- 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/episode.cpp b/src/episode.cpp index e3166fc..1b8bb22 100644 --- a/src/episode.cpp +++ b/src/episode.cpp @@ -338,7 +338,11 @@ namespace dropout_dl { void episode::download(const std::string& quality, const std::string& series_directory, std::string filename) { if (filename.empty()) { if (this->episode_number != 0) { - if (this->season_number != 0) { + if (this->episode_number == -1) { + /// Episode file without season or episode number in case of special episode + filename = this->series + " - " + this->name; + } + else if (this->season_number != 0) { filename = this->series + " - S" + ((this->season_number < 10) ? "0" : "") + std::to_string(this->season_number) + "E" + ((this->episode_number < 10) ? "0" : "") + std::to_string(this->episode_number) + " - " + this->name; } else { diff --git a/src/episode.h b/src/episode.h index e23e04a..16274fe 100644 --- a/src/episode.h +++ b/src/episode.h @@ -36,7 +36,7 @@ namespace dropout_dl { /// The name of the episode std::string name; /// The number of the episode (only set when downloading a season or series) - int episode_number = 0; + int episode_number = -1; /// The url for the main episode page std::string episode_url; /// The data of the main episode page diff --git a/src/season.cpp b/src/season.cpp index 1b6ce98..89b231d 100644 --- a/src/season.cpp +++ b/src/season.cpp @@ -5,7 +5,7 @@ #include "season.h" namespace dropout_dl { - episode season::get_episode(const std::string& html_data, int& start_point, const std::vector& cookies, int episode_number) { + episode season::get_episode(const std::string& html_data, int& start_point, const std::vector& cookies) { int link_start = 0; for (int i = start_point; i > 0; i--) { if (substr_is(html_data, i, ")", "", i); + int episode_number = -1; + if (!episode_text.empty()) { + episode_number = get_int_in_string(episode_text); + } return episode(html_data.substr(i, j), cookies, this->series_name, this->name, episode_number, this->season_number, false, this->download_captions); } } @@ -38,14 +43,12 @@ namespace dropout_dl { std::string site_video(R"(class="browse-item-link" data-track-event="site_video")"); - int number_of_episodes = 0; for (int i = 0; i < this->page_data.size(); i++) { if (substr_is(this->page_data, i, site_video)) { - episode e = get_episode(this->page_data, i, cookies, number_of_episodes + 1); + episode e = get_episode(this->page_data, i, cookies); if (e.episode_url.empty()) { continue; } - number_of_episodes++; std::cout << '\t' << e.name << '\n'; out.push_back(e); } diff --git a/src/season.h b/src/season.h index d8ef971..2f4a448 100644 --- a/src/season.h +++ b/src/season.h @@ -27,7 +27,7 @@ namespace dropout_dl { /// Whether or not to download captions bool download_captions; - episode get_episode(const std::string& html_data, int& start_point, const std::vector& cookies, int episode_number = 0); + episode get_episode(const std::string& html_data, int& start_point, const std::vector& cookies); /** * diff --git a/src/util.cpp b/src/util.cpp index 7d81fc3..304c0c8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -281,8 +281,8 @@ namespace dropout_dl { - std::string get_substring_in(const std::string& string, const std::string& begin, const std::string& end) { - size_t substring_start = string.find(begin); + std::string get_substring_in(const std::string& string, const std::string& begin, const std::string& end, int starting_index) { + size_t substring_start = string.find(begin, starting_index); if (substring_start == std::string::npos) { std::cerr << "ERROR: Could not find start of substring\n"; @@ -333,4 +333,26 @@ namespace dropout_dl { return result; } + + int get_int_in_string(const std::string& string, uint32_t starting_index) { + int out = 0; + int negative = 1; + bool found_number = false; + for (uint32_t i = starting_index; i < string.length(); i++) { + if (string[i] == '-' && (string[i] <= '9' && string[i] >= '0')) { + negative = -1; + } + + if (string[i] <= '9' && string[i] >= '0') { + found_number = true; + out *= 10; + out += string[i] - '0'; + } + else if (found_number) { + return out * negative; + } + } + return out * negative; + } + } diff --git a/src/util.h b/src/util.h index 9e97f0c..bb31400 100644 --- a/src/util.h +++ b/src/util.h @@ -140,7 +140,18 @@ namespace dropout_dl { * @param end - the ending string * @return the substring of 'string' between 'start' and 'end' */ - std::string get_substring_in(const std::string& string, const std::string& begin, const std::string& end); + std::string get_substring_in(const std::string& string, const std::string& begin, const std::string& end, int staring_index = 0); + + + /** + * + * @param string - the string that contains an integer. + * @param starting_index - the index within the string to start at. + * @return the int within string + * + * Gets the first int in string after starting_index. The first non number character will cause the function to stop parsing the int + */ + int get_int_in_string(const std::string& string, uint32_t starting_index = 0); /**