From 81bf7e3588665a3a8e47e6c5dffbfe2e41996da2 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 19 Jan 2019 13:40:10 +0100 Subject: [PATCH] Break up into more files --- Makefile | 10 ++- functions.cpp | 69 +++++++++++-------- functions.hpp | 15 ++--- main.cpp | 132 +++++++++++++++++++++++++++++++++++++ network.cpp | 32 +++++++++ network.hpp | 16 +++++ tv_rename.cpp | 179 +------------------------------------------------- tv_rename.hpp | 11 ++++ 8 files changed, 246 insertions(+), 218 deletions(-) create mode 100644 main.cpp create mode 100644 network.cpp create mode 100644 network.hpp create mode 100644 tv_rename.hpp diff --git a/Makefile b/Makefile index ee33df9..aae4cb4 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,8 @@ default: tv_rename clean: rm -f *.o tv_rename -tv_rename: functions.o filesystem.o tv_rename.cpp - $(CXX) $(CFLAGS) -o tv_rename tv_rename.cpp functions.o filesystem.o -lcurl +tv_rename: functions.o filesystem.o network.o tv_rename.o main.cpp + $(CXX) $(CFLAGS) -o tv_rename main.cpp tv_rename.o functions.o filesystem.o network.o -lcurl filesystem.o: filesystem.cpp $(CXX) $(CFLAGS) -c filesystem.cpp @@ -18,3 +18,9 @@ filesystem.o: filesystem.cpp functions.o: functions.cpp $(CXX) $(CFLAGS) -c functions.cpp +network.o: network.cpp + $(CXX) $(CFLAGS) -c network.cpp + +tv_rename.o: tv_rename.cpp + $(CXX) $(CFLAGS) -c tv_rename.cpp + diff --git a/functions.cpp b/functions.cpp index 53c57ed..ab45278 100644 --- a/functions.cpp +++ b/functions.cpp @@ -4,37 +4,16 @@ #include #include #include +#include #include -size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) { - *static_cast(target) += std::string(static_cast(contents), size*nmemb); - return size * nmemb; -} - -Curl::Curl() { - curl_global_init(CURL_GLOBAL_ALL); - curl_handle = curl_easy_init(); - curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback); - curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); -} - -Curl::~Curl() { - curl_easy_cleanup(curl_handle); - curl_global_cleanup(); -} - -std::string Curl::execute(const std::string &url) { - std::string source; - source.reserve(100000); - curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast(&source)); - curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); - auto res = curl_easy_perform(curl_handle); - if(res != CURLE_OK) { - std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; - return ""; - } - return source; -} +constexpr std::array languages{ + "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" +}; // return true if filename has specified season // set ep_pos to position where episode number starts @@ -183,3 +162,35 @@ void printHelp() { std::cout << " --print-langs\t\tPring available language" << std::endl; } +void parseSeasonNumbers(std::set &seasons_num, const char *argument) { + size_t pos{0}; + + while(!isdigit(argument[pos]) && argument[pos] != '\0') + pos++; + + if( argument[pos] == '\0' ) { + seasons_num.clear(); + return; + } + + int temp; + std::istringstream iss(argument + pos); + while(iss >> temp) { + seasons_num.insert(temp); + } +} + +void printLangs() { + for( size_t i = 0; i < languages.size(); i += 2 ) { + std::cout << languages[i] << " - " << languages[i+1] << std::endl; + } +} + +bool findLanguage( const char *language ) { + for( size_t i = 0; i < languages.size(); i += 2 ) { + if( !strcmp(language, languages[i]) ) + return true; + } + return false; +} + diff --git a/functions.hpp b/functions.hpp index 0030806..01a75ab 100644 --- a/functions.hpp +++ b/functions.hpp @@ -3,17 +3,8 @@ #include #include -#include #include - -class Curl { -public: - Curl(); - ~Curl(); - std::string execute( const std::string &url ); -private: - CURL *curl_handle; -}; +#include "network.hpp" std::string getDefUrl( std::string show, const std::string &language, Curl &c ); void findSeason(std::set &files, int season, const std::string &path); @@ -26,4 +17,8 @@ bool searchSpecificSeason(const char *const p, const std::string &number); bool searchSeason(const char *const p, size_t &season_pos); bool searchSeason(const char *const p); +void parseSeasonNumbers(std::set &seasons_num, const char *argument); +void printLangs(); +bool findLanguage( const char *language ); + #endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..22d88e5 --- /dev/null +++ b/main.cpp @@ -0,0 +1,132 @@ +#include +#include +#include "filesystem.hpp" +#include "functions.hpp" +#include "tv_rename.hpp" + +int parseCommandLine(std::string &show, std::set &seasons_num, std::string &path, bool &change_dir, std::string &language, bool &linux, bool &trust, int argc, char **argv) { + static struct option long_options[] = { + {"show", required_argument, 0, 's' }, + {"season", required_argument, 0, 'n' }, + {"correct-path", no_argument, 0, 'c' }, + {"show-path", required_argument, 0, 'p' }, + {"trust", no_argument, 0, 't' }, + {"linux", no_argument, 0, 'x' }, + {"lang", required_argument, 0, 'l' }, + {"print-langs", no_argument, 0, '0' }, + {"help", no_argument, 0, 'h' } + }; + + while(1) { + int option_index{0}; + auto c = getopt_long(argc, argv, "s:n:cp:txl:0h", long_options, &option_index); + if( c == -1 ) + break; + switch(c) { + case 's': + show = optarg; + break; + case 'n': + parseSeasonNumbers(seasons_num, optarg); + break; + case 'c': + change_dir = false; + break; + case 'p': + path = std::string(optarg); + // if path provided, assume it's correct + change_dir = false; + break; + case 't': + trust = true; + break; + case 'x': + linux = true; + break; + case 'l': + if( findLanguage(optarg) ) { + language = optarg; + } else { + std::cerr << "Invalid language choice" << std::endl; + printLangs(); + return -1; + } + break; + case '0': + printLangs(); + return 1; + case 'h': + printHelp(); + return 1; + default: + return -1; + } + } + + return 0; +} + +int main(int argc, char** argv) { + std::string show{}; + std::set seasons_num; + std::string path{"."}; + 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, language, linux, trust, argc, argv); + if( tmp == -1 ) + return 1; + else if ( tmp == 1 ) + return 0; + } + + if( !FSLib::isDirectory(path) ) + change_dir = true; + + while( change_dir ) { + if( !FSLib::isDirectory(path) ) { + std::cout << "This directory doesn't exist, please insert a correct path: " << std::endl; + std::getline(std::cin, path); + continue; + } + std::cout << "Is this the right directory? " << FSLib::canonical(path) << std::endl; + std::string response; + std::cin >> response; + std::cin.ignore(1,'\n'); + std::cin.clear(); + if ( response[0] == 'y' || response[0] == 'Y' ) { + change_dir = false; + } else { + std::cout << "Insert correct path:" << std::endl; + std::getline(std::cin, path); + } + } + + if( show.empty() ) { + auto pos = show.find_last_of('/'); + if( pos != std::string::npos ) + show = show.substr(++pos); + std::cout << "Is this the right show name? " << show << std::endl; + std::string response; + std::cin >> response; + std::cin.ignore(1, '\n'); + std::cin.clear(); + if( response[0] != 'y' && response[0] != 'Y' ) { + std::cout << "Insert the correct show name: " << std::endl; + std::getline(std::cin, show); + } + } + + if( seasons_num.size() == 1 ) { + singleSeason(path, show, *seasons_num.begin(), "", language, linux, trust, c); + } else if ( seasons_num.size() != 0 ) { + multipleSeasons(path, show, seasons_num, language, linux, trust, c); + } else { + allSeasons(path, show, language, linux, trust, c); + } +} + diff --git a/network.cpp b/network.cpp new file mode 100644 index 0000000..3b2038d --- /dev/null +++ b/network.cpp @@ -0,0 +1,32 @@ +#include +#include "network.hpp" + +size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) { + *static_cast(target) += std::string(static_cast(contents), size*nmemb); + return size * nmemb; +} + +Curl::Curl() { + curl_global_init(CURL_GLOBAL_ALL); + curl_handle = curl_easy_init(); + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); +} + +Curl::~Curl() { + curl_easy_cleanup(curl_handle); + curl_global_cleanup(); +} + +std::string Curl::execute(const std::string &url) { + std::string source; + source.reserve(100000); + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast(&source)); + curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); + auto res = curl_easy_perform(curl_handle); + if(res != CURLE_OK) { + std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; + return ""; + } + return source; +} diff --git a/network.hpp b/network.hpp new file mode 100644 index 0000000..47ed15b --- /dev/null +++ b/network.hpp @@ -0,0 +1,16 @@ +#ifndef NETWORK_HPP +#define NETWORK_HPP + +#include +#include + +class Curl { +public: + Curl(); + ~Curl(); + std::string execute( const std::string &url ); +private: + CURL *curl_handle; +}; + +#endif diff --git a/tv_rename.cpp b/tv_rename.cpp index 025daf2..538eef9 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -1,177 +1,10 @@ #include #include -#include -#include "functions.hpp" -#include -#include -#include #include -#include #include #include "filesystem.hpp" - -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, std::set const *files=nullptr); -void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c); -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 ); - -constexpr std::array languages{ - "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() { - for( size_t i = 0; i < languages.size(); i += 2 ) { - std::cout << languages[i] << " - " << languages[i+1] << std::endl; - } -} - -void parseSeasonNumbers(std::set &seasons_num, const char *argument) { - size_t pos{0}; - - while(!isdigit(argument[pos]) && argument[pos] != '\0') - pos++; - - if( argument[pos] == '\0' ) { - seasons_num.clear(); - return; - } - - int temp; - std::istringstream iss(&optarg[pos]); - while(iss >> temp) { - seasons_num.insert(temp); - } -} - -int parseCommandLine(std::string &show, std::set &seasons_num, std::string &path, bool &change_dir, std::string &language, bool &linux, bool &trust, int argc, char **argv) { - static struct option long_options[] = { - {"show", required_argument, 0, 's' }, - {"season", required_argument, 0, 'n' }, - {"correct-path", no_argument, 0, 'c' }, - {"show-path", required_argument, 0, 'p' }, - {"trust", no_argument, 0, 't' }, - {"linux", no_argument, 0, 'x' }, - {"lang", required_argument, 0, 'l' }, - {"print-langs", no_argument, 0, '0' }, - {"help", no_argument, 0, 'h' } - }; - - while(1) { - int option_index{0}; - auto c = getopt_long(argc, argv, "s:n:cp:txl:0h", long_options, &option_index); - if( c == -1 ) - break; - switch(c) { - case 's': - show = optarg; - break; - case 'n': - parseSeasonNumbers(seasons_num, optarg); - break; - case 'c': - change_dir = false; - break; - case 'p': - path = std::string(optarg); - // if path provided, assume it's correct - change_dir = false; - break; - case 't': - trust = true; - break; - case 'x': - linux = true; - break; - case 'l': - if( findLanguage(optarg) ) { - language = optarg; - } else { - std::cerr << "Invalid language choice" << std::endl; - printLangs(); - return -1; - } - break; - case '0': - printLangs(); - return 1; - case 'h': - printHelp(); - return 1; - default: - return -1; - } - } - - return 0; -} - -int main(int argc, char** argv) { - std::string show{}; - std::set seasons_num; - std::string path{"."}; - 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, language, linux, trust, argc, argv); - if( tmp == -1 ) - return 1; - else if ( tmp == 1 ) - return 0; - } - - if( !FSLib::isDirectory(path) ) - change_dir = true; - - while( change_dir ) { - if( !FSLib::isDirectory(path) ) { - std::cout << "This directory doesn't exist, please insert a correct path: " << std::endl; - std::getline(std::cin, path); - continue; - } - std::cout << "Is this the right directory? " << FSLib::canonical(path) << std::endl; - std::string response; - std::cin >> response; - std::cin.ignore(1,'\n'); - std::cin.clear(); - if ( response[0] == 'y' || response[0] == 'Y' ) { - change_dir = false; - } else { - std::cout << "Insert correct path:" << std::endl; - std::getline(std::cin, path); - } - } - - if( show.empty() ) { - auto pos = show.find_last_of('/'); - if( pos != std::string::npos ) - show = show.substr(++pos); - std::cout << "Is this the right show name? " << show << std::endl; - std::string response; - std::cin >> response; - std::cin.ignore(1, '\n'); - std::cin.clear(); - if( response[0] != 'y' && response[0] != 'Y' ) { - std::cout << "Insert the correct show name: " << std::endl; - std::getline(std::cin, show); - } - } - - if( seasons_num.size() == 1 ) { - singleSeason(path, show, *seasons_num.begin(), "", language, linux, trust, c); - } else if ( seasons_num.size() != 0 ) { - multipleSeasons(path, show, seasons_num, language, linux, trust, c); - } else { - allSeasons(path, show, language, linux, trust, c); - } -} +#include "functions.hpp" +#include "tv_rename.hpp" std::vector parseEpisodeNames( const std::string &season_code, const std::string &language) { std::vector episodes; @@ -324,11 +157,3 @@ void allSeasons( const std::string &path, const std::string &show, const std::st 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; -} - diff --git a/tv_rename.hpp b/tv_rename.hpp new file mode 100644 index 0000000..06bc62c --- /dev/null +++ b/tv_rename.hpp @@ -0,0 +1,11 @@ +#ifndef TV_RENAME_HPP +#define TV_RENAME_HPP + +#include +#include "network.hpp" + +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, std::set const *files=nullptr); +void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c); +void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c); + +#endif