No more regex, yay!
This commit is contained in:
parent
c2fd4cd106
commit
3bfa1e586e
@ -1,9 +1,10 @@
|
|||||||
#include "functions.hpp"
|
#include "functions.hpp"
|
||||||
#include "filesystem.hpp"
|
#include "filesystem.hpp"
|
||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <regex>
|
#include <vector>
|
||||||
|
|
||||||
size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) {
|
size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) {
|
||||||
*static_cast<std::string*>(target) += std::string(static_cast<char*>(contents), size*nmemb);
|
*static_cast<std::string*>(target) += std::string(static_cast<char*>(contents), size*nmemb);
|
||||||
@ -119,16 +120,20 @@ void iterateFS(std::set<int> &seasons, const std::string &path) {
|
|||||||
std::string getDefUrl( std::string show, const std::string &language, Curl &c ) {
|
std::string getDefUrl( std::string show, const std::string &language, Curl &c ) {
|
||||||
std::replace(show.begin(), show.end(), ' ', '+');
|
std::replace(show.begin(), show.end(), ' ', '+');
|
||||||
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + show + "&l=" + language);
|
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + show + "&l=" + language);
|
||||||
auto series = std::regex("<td><a href=\"/series.*?</td>");
|
size_t order{}, pos{};
|
||||||
int pos = 0;
|
|
||||||
std::vector<std::string> urls;
|
std::vector<std::string> urls;
|
||||||
for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) {
|
while( true ) {
|
||||||
// get show's name from link
|
pos = source_code.find("/ser", pos);
|
||||||
auto input = (*it).str();
|
if( pos != std::string::npos ) {
|
||||||
auto text = std::regex_replace(input, std::regex(".*series.*?>"), "");
|
auto end = source_code.find(">", pos);
|
||||||
text = text.substr(0, text.size() - 9);
|
end--;
|
||||||
std::cout << ++pos << ". " << text << std::endl;
|
urls.push_back(source_code.substr(pos, end - pos));
|
||||||
urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13));
|
pos = end + 2;
|
||||||
|
end = source_code.find("<", pos);
|
||||||
|
std::cout << ++order << ". " << source_code.substr(pos, end - pos) << std::endl;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
std::cout << "Which TV Show is the right one? ";
|
std::cout << "Which TV Show is the right one? ";
|
||||||
std::cin >> pos;
|
std::cin >> pos;
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "functions.hpp"
|
#include "functions.hpp"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <regex>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <vector>
|
||||||
#include "filesystem.hpp"
|
#include "filesystem.hpp"
|
||||||
|
|
||||||
void singleSeason( const std::string &path, const std::string &show, int season, std::string url );
|
void singleSeason( const std::string &path, const std::string &show, int season, std::string url );
|
||||||
@ -168,6 +169,26 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parseEpisodeNames( const std::string &season_code, std::vector<std::string> &episodes) {
|
||||||
|
size_t pos = 0;
|
||||||
|
while( true ) {
|
||||||
|
pos = season_code.find("ge=\"" + language + "\" ", pos);
|
||||||
|
if( pos != std::string::npos ) {
|
||||||
|
pos+= 17;
|
||||||
|
while( isspace(season_code[pos]) )
|
||||||
|
pos++;
|
||||||
|
auto end = season_code.find("<", pos);
|
||||||
|
end--;
|
||||||
|
while( isspace(season_code[end]) )
|
||||||
|
end--;
|
||||||
|
end++;
|
||||||
|
episodes.push_back(season_code.substr(pos, end - pos));
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void singleSeason( const std::string &path, const std::string &show, int season, std::string url) {
|
void singleSeason( const std::string &path, const std::string &show, int season, std::string url) {
|
||||||
if( url.empty() )
|
if( url.empty() )
|
||||||
url = getDefUrl(show, language, c);
|
url = getDefUrl(show, language, c);
|
||||||
@ -190,26 +211,24 @@ void singleSeason( const std::string &path, const std::string &show, int season,
|
|||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::regex title(".*<span.*?language=\"" + language + "\".*?>\\s*(.*?)\\s*?</span>.*");
|
|
||||||
std::regex episode_link("<td>.*?</td>");
|
|
||||||
std::smatch ep_match;
|
|
||||||
std::vector<std::string> episodes;
|
std::vector<std::string> episodes;
|
||||||
//get episode names in all languages
|
//get episode names in all languages
|
||||||
for( std::sregex_iterator it(season_code.begin(), season_code.end(), episode_link); it != std::sregex_iterator(); ++it) {
|
parseEpisodeNames(season_code, episodes);
|
||||||
auto input = (*it).str();
|
|
||||||
//only get the selected language
|
|
||||||
if( std::regex_search( input, title ) )
|
|
||||||
episodes.push_back(std::regex_replace(input, title, "$1"));
|
|
||||||
}
|
|
||||||
if( episodes.empty() )
|
if( episodes.empty() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::set<std::string> files;
|
std::set<std::string> files;
|
||||||
std::set<std::string> renamed_files;
|
std::set<std::string> renamed_files;
|
||||||
std::vector<std::string> renamed_episodes;
|
std::vector<std::string> renamed_episodes;
|
||||||
|
|
||||||
renamed_episodes.resize(episodes.size());
|
renamed_episodes.resize(episodes.size());
|
||||||
findSeason(files, season, path);
|
findSeason(files, season, path);
|
||||||
|
|
||||||
if( files.empty() )
|
if( files.empty() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for( const auto &x : files ) {
|
for( const auto &x : files ) {
|
||||||
auto last = x.find_last_of("/");
|
auto last = x.find_last_of("/");
|
||||||
std::string name;
|
std::string name;
|
||||||
@ -232,18 +251,33 @@ void singleSeason( const std::string &path, const std::string &show, int season,
|
|||||||
auto pos = name.find_last_of('.');
|
auto pos = name.find_last_of('.');
|
||||||
name.insert(pos, " - " + episodes[num]);
|
name.insert(pos, " - " + episodes[num]);
|
||||||
if( !linux ) {
|
if( !linux ) {
|
||||||
name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), "");
|
season_code.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), season_code.end());
|
||||||
name = std::regex_replace(name, std::regex("<"), "is less than");
|
size_t max{name.size()};
|
||||||
name = std::regex_replace(name, std::regex(">"), "is more than");
|
for( size_t i = 0; i < max ; i++ ) {
|
||||||
name = std::regex_replace(name, std::regex(":"), " -");
|
if( name[i] == '<' ) {
|
||||||
|
name[i] = 'i';
|
||||||
|
name.insert(i+1, "s less than");
|
||||||
|
max += 11;
|
||||||
|
} else if ( name[i] == '>' ) {
|
||||||
|
name[i] = 'i';
|
||||||
|
name.insert(i+1, "s more than");
|
||||||
|
max += 11;
|
||||||
|
} else if ( name[i] == ':' ) {
|
||||||
|
name[i] = ' ';
|
||||||
|
name.insert(i+1, 1, '-');
|
||||||
|
max++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
renamed_files.insert(dir + "/" + name);
|
renamed_files.insert(dir + "/" + name);
|
||||||
renamed_episodes[num] = name;
|
renamed_episodes[num] = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !trust ) {
|
if( !trust ) {
|
||||||
std::cout << "Does this seem ok? (y/n) ";
|
std::cout << "Does this seem ok? (y/n) ";
|
||||||
std::string response;
|
std::string response;
|
||||||
@ -253,7 +287,9 @@ void singleSeason( const std::string &path, const std::string &show, int season,
|
|||||||
if( response[0] != 'y' && response[0] != 'Y' )
|
if( response[0] != 'y' && response[0] != 'Y' )
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto orig = files.begin();
|
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(*orig, *renamed);
|
||||||
++orig;
|
++orig;
|
||||||
|
Loading…
Reference in New Issue
Block a user