No more regex, yay!

This commit is contained in:
zvon 2019-01-07 23:24:28 +01:00
parent c2fd4cd106
commit 3bfa1e586e
2 changed files with 65 additions and 24 deletions

View File

@ -1,9 +1,10 @@
#include "functions.hpp"
#include "filesystem.hpp"
#include <algorithm>
#include <iostream>
#include <curl/curl.h>
#include <stdlib.h>
#include <regex>
#include <vector>
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);
@ -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::replace(show.begin(), show.end(), ' ', '+');
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + show + "&l=" + language);
auto series = std::regex("<td><a href=\"/series.*?</td>");
int pos = 0;
size_t order{}, pos{};
std::vector<std::string> urls;
for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) {
// get show's name from link
auto input = (*it).str();
auto text = std::regex_replace(input, std::regex(".*series.*?>"), "");
text = text.substr(0, text.size() - 9);
std::cout << ++pos << ". " << text << std::endl;
urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13));
while( true ) {
pos = source_code.find("/ser", pos);
if( pos != std::string::npos ) {
auto end = source_code.find(">", pos);
end--;
urls.push_back(source_code.substr(pos, end - pos));
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::cin >> pos;

View File

@ -1,12 +1,13 @@
#include <algorithm>
#include <iostream>
#include <string.h>
#include "functions.hpp"
#include <map>
#include <regex>
#include <set>
#include <string>
#include <sstream>
#include <getopt.h>
#include <vector>
#include "filesystem.hpp"
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) {
if( url.empty() )
url = getDefUrl(show, language, c);
@ -190,26 +211,24 @@ void singleSeason( const std::string &path, const std::string &show, int season,
} else {
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;
//get episode names in all languages
for( std::sregex_iterator it(season_code.begin(), season_code.end(), episode_link); it != std::sregex_iterator(); ++it) {
auto input = (*it).str();
//only get the selected language
if( std::regex_search( input, title ) )
episodes.push_back(std::regex_replace(input, title, "$1"));
}
parseEpisodeNames(season_code, episodes);
if( episodes.empty() )
return;
std::set<std::string> files;
std::set<std::string> renamed_files;
std::vector<std::string> renamed_episodes;
renamed_episodes.resize(episodes.size());
findSeason(files, season, path);
if( files.empty() )
return;
for( const auto &x : files ) {
auto last = x.find_last_of("/");
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('.');
name.insert(pos, " - " + episodes[num]);
if( !linux ) {
name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), "");
name = std::regex_replace(name, std::regex("<"), "is less than");
name = std::regex_replace(name, std::regex(">"), "is more than");
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());
size_t max{name.size()};
for( size_t i = 0; i < max ; i++ ) {
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_episodes[num] = name;
}
}
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
std::cout << *renamed << std::endl;
}
if( !trust ) {
std::cout << "Does this seem ok? (y/n) ";
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' )
return;
}
auto orig = files.begin();
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(*orig, *renamed);
++orig;