tv_rename/functions.cpp

115 lines
4.5 KiB
C++
Raw Normal View History

2018-09-22 22:50:42 +00:00
#include "functions.hpp"
2019-01-03 18:01:43 +00:00
#include "filesystem.hpp"
2018-09-22 22:50:42 +00:00
#include <iostream>
#include <curl/curl.h>
#include <stdlib.h>
#include <regex>
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() {
2018-09-22 22:50:42 +00:00
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");
}
2018-09-22 22:50:42 +00:00
Curl::~Curl() {
2018-09-22 22:50:42 +00:00
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
}
2018-09-22 22:50:42 +00:00
std::string Curl::execute(const std::string &url) {
std::string source;
source.resize(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);
2018-09-22 22:50:42 +00:00
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
return "";
}
return source;
}
2019-01-03 18:01:43 +00:00
void findSeason(std::set<std::string> &files, int season, const std::string &path) {
2018-09-22 22:50:42 +00:00
std::smatch match;
auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}");
2019-01-03 18:01:43 +00:00
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) {
findSeason(files, season, path + "/" + p);
2018-09-22 22:50:42 +00:00
continue;
}
2019-01-03 18:01:43 +00:00
if( std::regex_search(p, episode) ) {
files.insert(path + "/" + p);
2018-09-22 22:50:42 +00:00
}
}
}
void iterateFS(std::set<int> &seasons, const std::string &path) {
std::smatch match;
auto episode = std::regex("[sS][0-9]{1,2000}[eE][0-9]{1,2000}");
2019-01-03 18:01:43 +00:00
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) {
iterateFS(seasons, path + "/" + p);
2018-09-22 22:50:42 +00:00
continue;
}
2019-01-03 18:01:43 +00:00
if( std::regex_search(p, match, episode) ) {
std::string a = match[0];
2018-09-22 22:50:42 +00:00
a = std::regex_replace(a, std::regex("[eE][0-9]{1,2000}"), "");
a = a.substr(1);
seasons.insert(std::stoi(a));
}
}
}
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c ) {
auto search = std::regex_replace(show, std::regex(" "), "+");
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language);
2018-09-22 22:50:42 +00:00
auto series = std::regex("<td><a href=\"/series.*?</td>");
int pos = 1;
std::vector<std::string> urls;
for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) {
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;
pos++;
urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13));
}
std::cout << "Which TV Show is the right one? ";
std::cin >> pos;
std::cin.clear();
std::cin.ignore(1, '\n');
return "https://www.thetvdb.com" + urls[pos-1];
}
void printHelp() {
std::cout << "usage: tv_rename [--help] [--show show name] [--season season number]" << std::endl;
std::cout << " [--correct-path] [--show-path show path] [--trust]" << std::endl;
std::cout << " [--linux] [--lang language] [--print-langs]" << std::endl;
std::cout << std::endl << "Rename TV episodes" << std::endl << std::endl << "optional arguments:" << std::endl;
std::cout << " -h, --help\t\tshow this help message and exit" << std::endl;
std::cout << " --show show name, -s show name" << std::endl;
std::cout << "\t\t\tTV show from which you want episode names (needs to be" << std::endl;
std::cout << "\t\t\tin quotation marks if it has more than one word)" << std::endl;
std::cout << " --season season number, -n season number" << std::endl;
std::cout << "\t\t\tSeason number/s (if multiple seasons, put them in" << std::endl;
std::cout << "\t\t\tquotation marks and seperate by one space)" << std::endl;
std::cout << "\t\t\tor 'all' for all seasons in selected subdirectory" << std::endl;
std::cout << " --correct-path, -c\tThis is the correct path, stop asking me!" << std::endl;
std::cout << " --show-path show path, -sp show path" << std::endl;
std::cout << "\t\t\tPath of the directory with episodes" << std::endl;
std::cout << " --trust, -t\t\tDon't ask whether the names are correct" << std::endl;
std::cout << " --linux, -x\t\tDon't replace characters characters that are illegal in Windows" << std::endl;
std::cout << " --lang language, -l language" << std::endl;
std::cout << "\t\t\tSelect which language the episode names shoud be in" << std::endl;
std::cout << " --print-langs, -p\tPring available language" << std::endl;
}