Fix bug where the program relied on episodes sorted by the episode number

This commit is contained in:
zvon 2019-01-23 22:57:52 +01:00
parent 85d459a3f0
commit 72706a5c0e
3 changed files with 61 additions and 65 deletions

View File

@ -147,13 +147,12 @@ void MainWindow::getNames() {
* orig - original filenames * orig - original filenames
* renamed - renamed filenames (sorted in the same order as `orig`) * renamed - renamed filenames (sorted in the same order as `orig`)
*/ */
void renameFiles(const std::set<std::string> &orig, const std::vector<std::pair<std::string, std::string>> &renamed) { void renameFiles(const std::vector<std::pair<std::string, std::pair<std::string, std::string>>> &renamed) {
auto orig_it = orig.begin();
for(auto renamed_it = renamed.begin(); renamed_it != renamed.end(); ++renamed_it) { for(auto renamed_it = renamed.begin(); renamed_it != renamed.end(); ++renamed_it) {
std::cout << *orig_it << " --> " << renamed_it->first + "/" + renamed_it->second << std::endl; std::cout << renamed_it->first << "/" << renamed_it->second.first << " --> "
FSLib::rename(*orig_it, renamed_it->first + "/" + renamed_it->second); << renamed_it->first << "/" << renamed_it->second.second << std::endl;
++orig_it; FSLib::rename(renamed_it->first + "/" + renamed_it->second.first
, renamed_it->first + "/" + renamed_it->second.second);
} }
} }
@ -167,7 +166,13 @@ void MainWindow::finishedSelection() {
// debug output // debug output
std::cout << (*iter)[m_columns_url.m_col_show] << " " << language_code << std::endl; std::cout << (*iter)[m_columns_url.m_col_show] << " " << language_code << std::endl;
std::cout << "https://www.thetvdb.com" << static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_url]) << std::endl; std::string url = static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_url]);
// shouldn't ever happen, but just to be sure
if( url.empty() )
return;
std::cout << "https://www.thetvdb.com" << url << std::endl;
std::string input_pattern = m_entry_pattern.get_text(); std::string input_pattern = m_entry_pattern.get_text();
@ -181,7 +186,7 @@ void MainWindow::finishedSelection() {
for( auto &x : selected ) { for( auto &x : selected ) {
// get renamed files for given season // get renamed files for given season
auto renamed_files = singleSeason( static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_show]), x, auto renamed_files = singleSeason( static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_show]), x,
"https://www.thetvdb.com" + static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_url]), "https://www.thetvdb.com" + url,
language_code, language_code,
(input_pattern.empty() ? default_pattern : input_pattern), (input_pattern.empty() ? default_pattern : input_pattern),
!m_check_linux.get_active(), c, files[x] ); !m_check_linux.get_active(), c, files[x] );
@ -191,7 +196,7 @@ void MainWindow::finishedSelection() {
// if trust checkbox is ticked, rename files // if trust checkbox is ticked, rename files
if( m_check_trust.get_active() ) { if( m_check_trust.get_active() ) {
renameFiles(files[x], renamed_files); renameFiles(renamed_files);
continue; continue;
} }
@ -208,11 +213,15 @@ void MainWindow::finishedSelection() {
auto buff = tx.get_buffer(); auto buff = tx.get_buffer();
buff->place_cursor(buff->begin()); buff->place_cursor(buff->begin());
buff->insert_at_cursor(renamed_files[0].second.c_str()); buff->insert_at_cursor(renamed_files[0].second.first.c_str());
buff->insert_at_cursor(" --> ");
buff->insert_at_cursor(renamed_files[0].second.second.c_str());
for( size_t i = 1; i < renamed_files.size(); i++ ) { for( size_t i = 1; i < renamed_files.size(); i++ ) {
buff->insert_at_cursor("\n"); buff->insert_at_cursor("\n");
buff->insert_at_cursor(renamed_files[i].second.c_str()); buff->insert_at_cursor(renamed_files[i].second.first.c_str());
buff->insert_at_cursor(" --> ");
buff->insert_at_cursor(renamed_files[i].second.second.c_str());
} }
auto response = dialog.run(); auto response = dialog.run();
@ -220,7 +229,7 @@ void MainWindow::finishedSelection() {
// if user clicked "Yes" in dialog, rename files // if user clicked "Yes" in dialog, rename files
switch(response) { switch(response) {
case Gtk::RESPONSE_OK: case Gtk::RESPONSE_OK:
renameFiles(files[x], renamed_files); renameFiles(renamed_files);
default: default:
break; break;
} }

View File

@ -36,18 +36,25 @@ std::vector<std::string> parseEpisodeNames( const std::string &season_code, cons
return episodes; return episodes;
} }
#ifndef GUI #ifdef GUI
void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set<std::string> const *files) { std::vector<std::pair<std::string, std::pair<std::string, std::string>>> defaultSingleSeasonReturn() {
return {};
}
#else #else
std::vector<std::pair<std::string, std::string>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set<std::string> &files) { void defaultSingleSeasonReturn() {
if( files.empty() ) return;
return {}; }
#endif #endif
if( url.empty() )
#ifndef GUI #ifdef GUI
url = getDefUrl(show, language, c); std::vector<std::pair<std::string, std::pair<std::string, std::string>>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set<std::string> &files) {
#else #else
return {}; void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set<std::string> const *files_ptr) {
#endif
#ifndef GUI
if( url.empty() )
url = getDefUrl(show, language, c);
#endif #endif
url += "/seasons/" + std::to_string(season); url += "/seasons/" + std::to_string(season);
@ -65,54 +72,41 @@ std::vector<std::pair<std::string, std::string>> singleSeason( const std::string
if( pos != std::string::npos ) if( pos != std::string::npos )
season_code = season_code.substr(0,pos); season_code = season_code.substr(0,pos);
else else
#ifndef GUI return defaultSingleSeasonReturn();
return;
#else
return {};
#endif
} else { } else {
#ifndef GUI return defaultSingleSeasonReturn();
return;
#else
return {};
#endif
} }
auto episodes = parseEpisodeNames(season_code, language); auto episodes = parseEpisodeNames(season_code, language);
if( episodes.empty() ) if( episodes.empty() )
#ifndef GUI return defaultSingleSeasonReturn();
return;
#else std::vector<std::pair<std::string, std::pair<std::string, std::string>>> renamed_files;
return {};
#endif
#ifndef GUI #ifndef GUI
std::set<std::string> found_files; std::set<std::string> found_files;
std::set<std::string> renamed_files;
if( files == nullptr ) { if( files_ptr == nullptr ) {
findSeason(found_files, season, path); findSeason(found_files, season, path);
files = &found_files; files_ptr = &found_files;
} }
if( files->empty() ) const std::set<std::string> &files = *files_ptr;
return; #endif
for( const auto &x : *files ) { if( files.empty() )
#else return defaultSingleSeasonReturn();
std::vector<std::pair<std::string, std::string>> renamed_files;
for( const auto &x : files ) { for( const auto &x : files ) {
#endif
auto last = x.find_last_of("/"); auto last = x.find_last_of("/");
std::string name; std::string og_name;
std::string dir; std::string dir;
if( last == static_cast<size_t>(-1) ) { if( last == std::string::npos ) {
name = x; og_name = x;
dir = "."; dir = ".";
} else { } else {
name = x.substr(last+1); og_name = x.substr(last+1);
dir = x.substr(0, last); dir = x.substr(0, last);
} }
unsigned long num; unsigned long num;
@ -124,9 +118,9 @@ std::vector<std::pair<std::string, std::string>> singleSeason( const std::string
} }
num -= 1; num -= 1;
if( num < episodes.size() ) { if( num < episodes.size() ) {
auto pos = name.find_last_of('.'); auto pos = og_name.find_last_of('.');
// get desired filename // get desired filename
name = compilePattern(pattern, season, num+1, name.substr(0, pos), episodes[num], show) + name.substr(pos); auto name = compilePattern(pattern, season, num+1, og_name.substr(0, pos), episodes[num], show) + og_name.substr(pos);
// replace characters illegal in windows if desired // replace characters illegal in windows if desired
if( !linux ) { if( !linux ) {
season_code.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), season_code.end()); season_code.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), season_code.end());
@ -147,17 +141,15 @@ std::vector<std::pair<std::string, std::string>> singleSeason( const std::string
} }
} }
} }
#ifndef GUI renamed_files.emplace_back(dir, std::pair<std::string, std::string>(og_name, name));
renamed_files.insert(dir + "/" + name);
#else
renamed_files.emplace_back(dir, name);
#endif
} }
} }
#ifndef GUI #ifdef GUI
return renamed_files;
#else
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
std::cout << *renamed << std::endl; std::cout << renamed->second.first << " --> " << renamed->second.second << std::endl;
} }
if( !trust ) { if( !trust ) {
@ -170,14 +162,9 @@ std::vector<std::pair<std::string, std::string>> singleSeason( const std::string
return; return;
} }
auto orig = files->begin();
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(*orig, *renamed); FSLib::rename(renamed->first + "/" + renamed->second.first, renamed->first + "/" + renamed->second.second);
++orig;
} }
#else
return renamed_files;
#endif #endif
} }

View File

@ -8,7 +8,7 @@
#include <vector> #include <vector>
std::vector<std::pair<std::string, std::string>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set<std::string> &files); std::vector<std::pair<std::string, std::pair<std::string, std::string>>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set<std::string> &files);
#else #else