Spaces instead of tabs

This commit is contained in:
zvon 2019-01-04 20:19:08 +01:00
parent c014a059b6
commit 1bbddc5de9
5 changed files with 176 additions and 171 deletions

View File

@ -2,8 +2,13 @@ CC ?= clang
CXX ?= clang++
CFLAGS ?= -O2 -g -Wall -Wextra -std=c++11
.PHONY: default
default: tv_rename
.PHONY: clean
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

View File

@ -4,26 +4,26 @@
#include <linux/limits.h>
bool FSLib::exists(const std::string &path) {
struct stat path_stat;
return stat( path.c_str(), &path_stat ) == 0;
struct stat path_stat;
return stat( path.c_str(), &path_stat ) == 0;
}
bool FSLib::isDirectory(const std::string &path) {
struct stat path_stat;
stat( path.c_str(), &path_stat );
return S_ISDIR(path_stat.st_mode);
struct stat path_stat;
stat( path.c_str(), &path_stat );
return S_ISDIR(path_stat.st_mode);
}
std::string FSLib::canonical(const std::string &path) {
char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
if( realpath(path.c_str(), canonical_path) == nullptr )
return "";
std::string canonical_string{canonical_path};
free(canonical_path);
return canonical_string;
char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
if( realpath(path.c_str(), canonical_path) == nullptr )
return "";
std::string canonical_string{canonical_path};
free(canonical_path);
return canonical_string;
}
bool FSLib::rename(const std::string &file_a, const std::string &file_b) {
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
}

View File

@ -6,101 +6,101 @@
#include <string.h>
namespace FSLib{
bool exists(const std::string &path);
bool isDirectory(const std::string &path);
std::string canonical(const std::string &path);
bool rename(const std::string &file_a, const std::string &file_b);
bool exists(const std::string &path);
bool isDirectory(const std::string &path);
std::string canonical(const std::string &path);
bool rename(const std::string &file_a, const std::string &file_b);
class Directory {
public:
Directory(const std::string &path_) : dir_path(path_) {}
Directory() = delete;
Directory(const Directory &d) = default;
Directory(Directory &&d) = default;
class Directory {
public:
Directory(const std::string &path_) : dir_path(path_) {}
Directory() = delete;
Directory(const Directory &d) = default;
Directory(Directory &&d) = default;
class Iterator {
public:
Iterator( const Directory &d_ ) : d(opendir(d_.path())) {
current_entry = readdir(d);
}
class Iterator {
public:
Iterator( const Directory &d_ ) : d(opendir(d_.path())) {
current_entry = readdir(d);
}
Iterator( const Directory &d_, const struct dirent *current_entry_) : d(opendir(d_.path())), current_entry(current_entry_) {}
Iterator( const Directory &d_, const struct dirent *current_entry_) : d(opendir(d_.path())), current_entry(current_entry_) {}
Iterator() = delete;
Iterator() = delete;
Iterator(const Iterator &i) = default;
Iterator(const Iterator &i) = default;
Iterator(Iterator &&i) = default;
Iterator(Iterator &&i) = default;
~Iterator() {
closedir(d);
}
~Iterator() {
closedir(d);
}
std::string operator*() {
return current_entry->d_name;
}
std::string operator*() {
return current_entry->d_name;
}
Iterator &operator++() {
if( current_entry == nullptr )
return *this;
current_entry = readdir(d);
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
operator++();
return *this;
}
Iterator &operator++() {
if( current_entry == nullptr )
return *this;
current_entry = readdir(d);
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
operator++();
return *this;
}
Iterator operator++(int) {
Iterator ret(*this);
operator++();
return ret;
}
Iterator operator++(int) {
Iterator ret(*this);
operator++();
return ret;
}
bool operator==(const Iterator &i_other) {
return i_other.current_entry == current_entry;
}
bool operator==(const Iterator &i_other) {
return i_other.current_entry == current_entry;
}
bool operator!=(const Iterator &i_other) {
return i_other.current_entry != current_entry;
}
bool operator!=(const Iterator &i_other) {
return i_other.current_entry != current_entry;
}
private:
DIR *d;
const struct dirent *current_entry;
};
private:
DIR *d;
const struct dirent *current_entry;
};
using iterator = Iterator;
using const_iterator = Iterator;
using iterator = Iterator;
using const_iterator = Iterator;
iterator begin() {
return Iterator(dir_path);
}
iterator begin() {
return Iterator(dir_path);
}
const_iterator begin() const {
return Iterator(dir_path);
}
const_iterator begin() const {
return Iterator(dir_path);
}
const_iterator cbegin() const {
return begin();
}
const_iterator cbegin() const {
return begin();
}
iterator end() {
return Iterator(dir_path, nullptr);
}
iterator end() {
return Iterator(dir_path, nullptr);
}
const_iterator end() const {
return Iterator(dir_path, nullptr);
}
const_iterator end() const {
return Iterator(dir_path, nullptr);
}
const_iterator cend() const {
return end();
}
const_iterator cend() const {
return end();
}
const char *path() const {
return dir_path.c_str();
}
private:
std::string dir_path;
};
const char *path() const {
return dir_path.c_str();
}
private:
std::string dir_path;
};
} //end FSLib
#endif

View File

@ -6,109 +6,109 @@
#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;
*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_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();
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
}
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);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
return "";
}
return source;
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);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
return "";
}
return source;
}
void findSeason(std::set<std::string> &files, int season, const std::string &path) {
std::smatch match;
auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}");
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) {
findSeason(files, season, path + "/" + p);
continue;
}
std::smatch match;
auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}");
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) {
findSeason(files, season, path + "/" + p);
continue;
}
if( std::regex_search(p, episode) ) {
files.insert(path + "/" + p);
}
}
if( std::regex_search(p, episode) ) {
files.insert(path + "/" + p);
}
}
}
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}");
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) {
iterateFS(seasons, path + "/" + p);
continue;
}
std::smatch match;
auto episode = std::regex("[sS][0-9]{1,2000}[eE][0-9]{1,2000}");
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) {
iterateFS(seasons, path + "/" + p);
continue;
}
if( std::regex_search(p, match, episode) ) {
std::string a = match[0];
a = std::regex_replace(a, std::regex("[eE][0-9]{1,2000}"), "");
a = a.substr(1);
seasons.insert(std::stoi(a));
}
}
if( std::regex_search(p, match, episode) ) {
std::string a = match[0];
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);
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');
auto search = std::regex_replace(show, std::regex(" "), "+");
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language);
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;
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, -p 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\t\tPring available language" << std::endl;
}

View File

@ -7,11 +7,11 @@
class Curl {
public:
Curl();
~Curl();
std::string execute( const std::string &url );
Curl();
~Curl();
std::string execute( const std::string &url );
private:
CURL *curl_handle;
CURL *curl_handle;
};
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c );