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
* 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) {
auto orig_it = orig.begin();
void renameFiles(const std::vector<std::pair<std::string, std::pair<std::string, std::string>>> &renamed) {
for(auto renamed_it = renamed.begin(); renamed_it != renamed.end(); ++renamed_it) {
std::cout << *orig_it << " --> " << renamed_it->first + "/" + renamed_it->second << std::endl;
FSLib::rename(*orig_it, renamed_it->first + "/" + renamed_it->second);
++orig_it;
std::cout << renamed_it->first << "/" << renamed_it->second.first << " --> "
<< renamed_it->first << "/" << renamed_it->second.second << std::endl;
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
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();
@ -181,7 +186,7 @@ void MainWindow::finishedSelection() {
for( auto &x : selected ) {
// get renamed files for given season
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,
(input_pattern.empty() ? default_pattern : input_pattern),
!m_check_linux.get_active(), c, files[x] );
@ -191,7 +196,7 @@ void MainWindow::finishedSelection() {
// if trust checkbox is ticked, rename files
if( m_check_trust.get_active() ) {
renameFiles(files[x], renamed_files);
renameFiles(renamed_files);
continue;
}
@ -208,11 +213,15 @@ void MainWindow::finishedSelection() {
auto buff = tx.get_buffer();
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++ ) {
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();
@ -220,7 +229,7 @@ void MainWindow::finishedSelection() {
// if user clicked "Yes" in dialog, rename files
switch(response) {
case Gtk::RESPONSE_OK:
renameFiles(files[x], renamed_files);
renameFiles(renamed_files);
default:
break;
}

View File

@ -36,18 +36,25 @@ std::vector<std::string> parseEpisodeNames( const std::string &season_code, cons
return episodes;
}
#ifndef 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) {
#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) {
if( files.empty() )
#ifdef GUI
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> defaultSingleSeasonReturn() {
return {};
}
#else
void defaultSingleSeasonReturn() {
return;
}
#endif
if( url.empty() )
#ifndef GUI
url = getDefUrl(show, language, c);
#ifdef GUI
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
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
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 )
season_code = season_code.substr(0,pos);
else
#ifndef GUI
return;
#else
return {};
#endif
return defaultSingleSeasonReturn();
} else {
#ifndef GUI
return;
#else
return {};
#endif
return defaultSingleSeasonReturn();
}
auto episodes = parseEpisodeNames(season_code, language);
if( episodes.empty() )
#ifndef GUI
return;
#else
return {};
#endif
return defaultSingleSeasonReturn();
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> renamed_files;
#ifndef GUI
std::set<std::string> found_files;
std::set<std::string> renamed_files;
if( files == nullptr ) {
if( files_ptr == nullptr ) {
findSeason(found_files, season, path);
files = &found_files;
files_ptr = &found_files;
}
if( files->empty() )
return;
const std::set<std::string> &files = *files_ptr;
#endif
for( const auto &x : *files ) {
#else
std::vector<std::pair<std::string, std::string>> renamed_files;
if( files.empty() )
return defaultSingleSeasonReturn();
for( const auto &x : files ) {
#endif
auto last = x.find_last_of("/");
std::string name;
std::string og_name;
std::string dir;
if( last == static_cast<size_t>(-1) ) {
name = x;
if( last == std::string::npos ) {
og_name = x;
dir = ".";
} else {
name = x.substr(last+1);
og_name = x.substr(last+1);
dir = x.substr(0, last);
}
unsigned long num;
@ -124,9 +118,9 @@ std::vector<std::pair<std::string, std::string>> singleSeason( const std::string
}
num -= 1;
if( num < episodes.size() ) {
auto pos = name.find_last_of('.');
auto pos = og_name.find_last_of('.');
// 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
if( !linux ) {
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.insert(dir + "/" + name);
#else
renamed_files.emplace_back(dir, name);
#endif
renamed_files.emplace_back(dir, std::pair<std::string, std::string>(og_name, name));
}
}
#ifndef GUI
#ifdef GUI
return renamed_files;
#else
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 ) {
@ -170,14 +162,9 @@ std::vector<std::pair<std::string, std::string>> singleSeason( const std::string
return;
}
auto orig = files->begin();
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(*orig, *renamed);
++orig;
FSLib::rename(renamed->first + "/" + renamed->second.first, renamed->first + "/" + renamed->second.second);
}
#else
return renamed_files;
#endif
}

View File

@ -8,7 +8,7 @@
#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