Removed global variable

This commit is contained in:
zvon 2019-01-17 01:24:04 +01:00
parent e976ffddc1
commit 309b07d353

View File

@ -10,26 +10,22 @@
#include <vector> #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, const std::string &language, const bool &linux, const bool &trust, Curl &c);
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons ); void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c);
void allSeasons( const std::string &path, const std::string &show ); void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c);
bool findLanguage( const char *language );
std::string language{"en"}; constexpr std::array<const char *, 46> languages{
bool trust{false}; "en", "English", "sv", "Svenska", "no", "Norsk", "da", "Dansk", "fi", "Suomeksi",
bool linux{false}; "nl", "Nederlands", "de", "Deutsch", "it", "Italiano", "es", "Español", "fr", "Français",
Curl c; // global so the connection doesn't close at every function "pl", "Polski", "hu", "Magyar", "el", "Greek", "tr", "Turkish", "ru", "Russian",
"he", "Hebrew", "ja", "Japanese", "pt", "Portuguese", "zh", "Chinese", "cs", "Czech",
std::map<std::string, std::string> languages{ "sl", "Slovenian", "hr", "Croatian", "ko","Korea"
{"en", "English"}, {"sv", "Svenska"}, {"no", "Norsk"}, {"da", "Dansk"}, {"fi", "Suomeksi"},
{"nl", "Nederlands"}, {"de", "Deutsch"}, {"it", "Italiano"}, {"es", "Español"}, {"fr", "Français"},
{"pl", "Polski"}, {"hu", "Magyar"}, {"el", "Greek"}, {"tr", "Turkish"}, {"ru", "Russian"},
{"he", "Hebrew"}, {"ja", "Japanese"}, {"pt", "Portuguese"}, {"zh", "Chinese"}, {"cs", "Czech"},
{"sl", "Slovenian"}, {"hr", "Croatian"}, {"ko","Korea"}
}; };
void printLangs() { void printLangs() {
for( const auto &x : languages ) { for( size_t i = 0; i < languages.size(); i += 2 ) {
std::cout << x.first << " - " << x.second << std::endl; std::cout << languages[i] << " - " << languages[i+1] << std::endl;
} }
} }
@ -51,7 +47,7 @@ void parseSeasonNumbers(std::set<int> &seasons_num, const char *argument) {
} }
} }
int parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string &path, bool &change_dir, int argc, char **argv) { int parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string &path, bool &change_dir, std::string &language, bool &linux, bool &trust, int argc, char **argv) {
static struct option long_options[] = { static struct option long_options[] = {
{"show", required_argument, 0, 's' }, {"show", required_argument, 0, 's' },
{"season", required_argument, 0, 'n' }, {"season", required_argument, 0, 'n' },
@ -91,7 +87,7 @@ int parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string
linux = true; linux = true;
break; break;
case 'l': case 'l':
if( languages.find(optarg) != languages.end() ) { if( findLanguage(optarg) ) {
language = optarg; language = optarg;
} else { } else {
std::cerr << "Invalid language choice" << std::endl; std::cerr << "Invalid language choice" << std::endl;
@ -118,9 +114,13 @@ int main(int argc, char** argv) {
std::set<int> seasons_num; std::set<int> seasons_num;
std::string path{"."}; std::string path{"."};
bool change_dir{true}; bool change_dir{true};
std::string language{"en"};
bool linux{false};
bool trust{false};
Curl c;
{ {
auto tmp = parseCommandLine(show, seasons_num, path, change_dir, argc, argv); auto tmp = parseCommandLine(show, seasons_num, path, change_dir, language, linux, trust, argc, argv);
if( tmp == -1 ) if( tmp == -1 )
return 1; return 1;
else if ( tmp == 1 ) else if ( tmp == 1 )
@ -165,15 +165,15 @@ int main(int argc, char** argv) {
} }
if( seasons_num.size() == 1 ) { if( seasons_num.size() == 1 ) {
singleSeason(path, show, *seasons_num.begin(), ""); singleSeason(path, show, *seasons_num.begin(), "", language, linux, trust, c);
} else if ( seasons_num.size() != 0 ) { } else if ( seasons_num.size() != 0 ) {
multipleSeasons(path, show, seasons_num); multipleSeasons(path, show, seasons_num, language, linux, trust, c);
} else { } else {
allSeasons(path, show); allSeasons(path, show, language, linux, trust, c);
} }
} }
void parseEpisodeNames( const std::string &season_code, std::vector<std::string> &episodes) { void parseEpisodeNames( const std::string &season_code, std::vector<std::string> &episodes, const std::string &language) {
size_t pos = 0; size_t pos = 0;
while( true ) { while( true ) {
pos = season_code.find("ge=\"" + language + "\" ", pos); pos = season_code.find("ge=\"" + language + "\" ", pos);
@ -193,7 +193,7 @@ void parseEpisodeNames( const std::string &season_code, std::vector<std::string>
} }
} }
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, const std::string &language, const bool &linux, const bool &trust, Curl &c) {
if( url.empty() ) if( url.empty() )
url = getDefUrl(show, language, c); url = getDefUrl(show, language, c);
url += "/seasons/" + std::to_string(season); url += "/seasons/" + std::to_string(season);
@ -218,7 +218,7 @@ void singleSeason( const std::string &path, const std::string &show, int season,
std::vector<std::string> episodes; std::vector<std::string> episodes;
//get episode names in all languages //get episode names in all languages
parseEpisodeNames(season_code, episodes); parseEpisodeNames(season_code, episodes, language);
if( episodes.empty() ) if( episodes.empty() )
return; return;
@ -300,17 +300,25 @@ void singleSeason( const std::string &path, const std::string &show, int season,
} }
} }
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons) { void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c) {
auto url = getDefUrl(show, language, c); auto url = getDefUrl(show, language, c);
for( const auto &x : seasons ) { for( const auto &x : seasons ) {
singleSeason( path, show, x, url); singleSeason( path, show, x, url, language, linux, trust, c);
} }
} }
void allSeasons( const std::string &path, const std::string &show) { void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c) {
std::set<int> seasons; std::set<int> seasons;
//get all season number from this directory and subdirectories //get all season number from this directory and subdirectories
iterateFS(seasons, path); iterateFS(seasons, path);
multipleSeasons( path, show, seasons); multipleSeasons( path, show, seasons, language, linux, trust, c);
}
bool findLanguage( const char *language ) {
for( size_t i = 0; i < languages.size(); i += 2 ) {
if( !strcmp(language, languages[i]) )
return true;
}
return false;
} }