Break up into more files

This commit is contained in:
zvon 2019-01-19 13:40:10 +01:00
parent c8067ddaf1
commit 81bf7e3588
8 changed files with 246 additions and 218 deletions

View File

@ -9,8 +9,8 @@ default: tv_rename
clean: clean:
rm -f *.o tv_rename rm -f *.o tv_rename
tv_rename: functions.o filesystem.o tv_rename.cpp tv_rename: functions.o filesystem.o network.o tv_rename.o main.cpp
$(CXX) $(CFLAGS) -o tv_rename tv_rename.cpp functions.o filesystem.o -lcurl $(CXX) $(CFLAGS) -o tv_rename main.cpp tv_rename.o functions.o filesystem.o network.o -lcurl
filesystem.o: filesystem.cpp filesystem.o: filesystem.cpp
$(CXX) $(CFLAGS) -c filesystem.cpp $(CXX) $(CFLAGS) -c filesystem.cpp
@ -18,3 +18,9 @@ filesystem.o: filesystem.cpp
functions.o: functions.cpp functions.o: functions.cpp
$(CXX) $(CFLAGS) -c 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

View File

@ -4,37 +4,16 @@
#include <iostream> #include <iostream>
#include <curl/curl.h> #include <curl/curl.h>
#include <stdlib.h> #include <stdlib.h>
#include <sstream>
#include <vector> #include <vector>
size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) { constexpr std::array<const char *, 46> languages{
*static_cast<std::string*>(target) += std::string(static_cast<char*>(contents), size*nmemb); "en", "English", "sv", "Svenska", "no", "Norsk", "da", "Dansk", "fi", "Suomeksi",
return size * nmemb; "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",
Curl::Curl() { "sl", "Slovenian", "hr", "Croatian", "ko","Korea"
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<void*>(&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;
}
// return true if filename has specified season // return true if filename has specified season
// set ep_pos to position where episode number starts // 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; std::cout << " --print-langs\t\tPring available language" << std::endl;
} }
void parseSeasonNumbers(std::set<int> &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;
}

View File

@ -3,17 +3,8 @@
#include <string> #include <string>
#include <set> #include <set>
#include <curl/curl.h>
#include <map> #include <map>
#include "network.hpp"
class Curl {
public:
Curl();
~Curl();
std::string execute( const std::string &url );
private:
CURL *curl_handle;
};
std::string getDefUrl( std::string show, const std::string &language, Curl &c ); std::string getDefUrl( std::string show, const std::string &language, Curl &c );
void findSeason(std::set<std::string> &files, int season, const std::string &path); void findSeason(std::set<std::string> &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, size_t &season_pos);
bool searchSeason(const char *const p); bool searchSeason(const char *const p);
void parseSeasonNumbers(std::set<int> &seasons_num, const char *argument);
void printLangs();
bool findLanguage( const char *language );
#endif #endif

132
main.cpp Normal file
View File

@ -0,0 +1,132 @@
#include <getopt.h>
#include <iostream>
#include "filesystem.hpp"
#include "functions.hpp"
#include "tv_rename.hpp"
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[] = {
{"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<int> 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);
}
}

32
network.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include "network.hpp"
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);
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<void*>(&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;
}

16
network.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef NETWORK_HPP
#define NETWORK_HPP
#include <curl/curl.h>
#include <string>
class Curl {
public:
Curl();
~Curl();
std::string execute( const std::string &url );
private:
CURL *curl_handle;
};
#endif

View File

@ -1,177 +1,10 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <string.h>
#include "functions.hpp"
#include <map>
#include <set>
#include <string>
#include <sstream> #include <sstream>
#include <getopt.h>
#include <vector> #include <vector>
#include "filesystem.hpp" #include "filesystem.hpp"
#include "functions.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<std::string> const *files=nullptr); #include "tv_rename.hpp"
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, const std::string &language, const bool &linux, const bool &trust, Curl &c);
bool findLanguage( const char *language );
constexpr std::array<const char *, 46> 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<int> &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<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[] = {
{"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<int> 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);
}
}
std::vector<std::string> parseEpisodeNames( const std::string &season_code, const std::string &language) { std::vector<std::string> parseEpisodeNames( const std::string &season_code, const std::string &language) {
std::vector<std::string> episodes; std::vector<std::string> 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); 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;
}

11
tv_rename.hpp Normal file
View File

@ -0,0 +1,11 @@
#ifndef TV_RENAME_HPP
#define TV_RENAME_HPP
#include <set>
#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<std::string> const *files=nullptr);
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, const std::string &language, const bool &linux, const bool &trust, Curl &c);
#endif