Spaces instead of tabs
This commit is contained in:
parent
c014a059b6
commit
1bbddc5de9
5
Makefile
5
Makefile
@ -2,8 +2,13 @@ CC ?= clang
|
|||||||
CXX ?= clang++
|
CXX ?= clang++
|
||||||
CFLAGS ?= -O2 -g -Wall -Wextra -std=c++11
|
CFLAGS ?= -O2 -g -Wall -Wextra -std=c++11
|
||||||
|
|
||||||
|
.PHONY: default
|
||||||
default: tv_rename
|
default: tv_rename
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -f *.o tv_rename
|
||||||
|
|
||||||
tv_rename: functions.o filesystem.o tv_rename.cpp
|
tv_rename: functions.o filesystem.o tv_rename.cpp
|
||||||
$(CXX) $(CFLAGS) -o tv_rename tv_rename.cpp functions.o filesystem.o -lcurl
|
$(CXX) $(CFLAGS) -o tv_rename tv_rename.cpp functions.o filesystem.o -lcurl
|
||||||
|
|
||||||
|
@ -4,26 +4,26 @@
|
|||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
|
|
||||||
bool FSLib::exists(const std::string &path) {
|
bool FSLib::exists(const std::string &path) {
|
||||||
struct stat path_stat;
|
struct stat path_stat;
|
||||||
return stat( path.c_str(), &path_stat ) == 0;
|
return stat( path.c_str(), &path_stat ) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FSLib::isDirectory(const std::string &path) {
|
bool FSLib::isDirectory(const std::string &path) {
|
||||||
struct stat path_stat;
|
struct stat path_stat;
|
||||||
stat( path.c_str(), &path_stat );
|
stat( path.c_str(), &path_stat );
|
||||||
return S_ISDIR(path_stat.st_mode);
|
return S_ISDIR(path_stat.st_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FSLib::canonical(const std::string &path) {
|
std::string FSLib::canonical(const std::string &path) {
|
||||||
char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
|
char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
|
||||||
if( realpath(path.c_str(), canonical_path) == nullptr )
|
if( realpath(path.c_str(), canonical_path) == nullptr )
|
||||||
return "";
|
return "";
|
||||||
std::string canonical_string{canonical_path};
|
std::string canonical_string{canonical_path};
|
||||||
free(canonical_path);
|
free(canonical_path);
|
||||||
return canonical_string;
|
return canonical_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FSLib::rename(const std::string &file_a, const std::string &file_b) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
148
filesystem.hpp
148
filesystem.hpp
@ -6,101 +6,101 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
namespace FSLib{
|
namespace FSLib{
|
||||||
bool exists(const std::string &path);
|
bool exists(const std::string &path);
|
||||||
bool isDirectory(const std::string &path);
|
bool isDirectory(const std::string &path);
|
||||||
std::string canonical(const std::string &path);
|
std::string canonical(const std::string &path);
|
||||||
bool rename(const std::string &file_a, const std::string &file_b);
|
bool rename(const std::string &file_a, const std::string &file_b);
|
||||||
|
|
||||||
class Directory {
|
class Directory {
|
||||||
public:
|
public:
|
||||||
Directory(const std::string &path_) : dir_path(path_) {}
|
Directory(const std::string &path_) : dir_path(path_) {}
|
||||||
Directory() = delete;
|
Directory() = delete;
|
||||||
Directory(const Directory &d) = default;
|
Directory(const Directory &d) = default;
|
||||||
Directory(Directory &&d) = default;
|
Directory(Directory &&d) = default;
|
||||||
|
|
||||||
class Iterator {
|
class Iterator {
|
||||||
public:
|
public:
|
||||||
Iterator( const Directory &d_ ) : d(opendir(d_.path())) {
|
Iterator( const Directory &d_ ) : d(opendir(d_.path())) {
|
||||||
current_entry = readdir(d);
|
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() {
|
~Iterator() {
|
||||||
closedir(d);
|
closedir(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string operator*() {
|
std::string operator*() {
|
||||||
return current_entry->d_name;
|
return current_entry->d_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator &operator++() {
|
Iterator &operator++() {
|
||||||
if( current_entry == nullptr )
|
if( current_entry == nullptr )
|
||||||
return *this;
|
return *this;
|
||||||
current_entry = readdir(d);
|
current_entry = readdir(d);
|
||||||
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
|
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
|
||||||
operator++();
|
operator++();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator operator++(int) {
|
Iterator operator++(int) {
|
||||||
Iterator ret(*this);
|
Iterator ret(*this);
|
||||||
operator++();
|
operator++();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Iterator &i_other) {
|
bool operator==(const Iterator &i_other) {
|
||||||
return i_other.current_entry == current_entry;
|
return i_other.current_entry == current_entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const Iterator &i_other) {
|
bool operator!=(const Iterator &i_other) {
|
||||||
return i_other.current_entry != current_entry;
|
return i_other.current_entry != current_entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DIR *d;
|
DIR *d;
|
||||||
const struct dirent *current_entry;
|
const struct dirent *current_entry;
|
||||||
};
|
};
|
||||||
|
|
||||||
using iterator = Iterator;
|
using iterator = Iterator;
|
||||||
using const_iterator = Iterator;
|
using const_iterator = Iterator;
|
||||||
|
|
||||||
iterator begin() {
|
iterator begin() {
|
||||||
return Iterator(dir_path);
|
return Iterator(dir_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator begin() const {
|
const_iterator begin() const {
|
||||||
return Iterator(dir_path);
|
return Iterator(dir_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator cbegin() const {
|
const_iterator cbegin() const {
|
||||||
return begin();
|
return begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator end() {
|
iterator end() {
|
||||||
return Iterator(dir_path, nullptr);
|
return Iterator(dir_path, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator end() const {
|
const_iterator end() const {
|
||||||
return Iterator(dir_path, nullptr);
|
return Iterator(dir_path, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator cend() const {
|
const_iterator cend() const {
|
||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *path() const {
|
const char *path() const {
|
||||||
return dir_path.c_str();
|
return dir_path.c_str();
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
std::string dir_path;
|
std::string dir_path;
|
||||||
};
|
};
|
||||||
} //end FSLib
|
} //end FSLib
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
160
functions.cpp
160
functions.cpp
@ -6,109 +6,109 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) {
|
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);
|
*static_cast<std::string*>(target) += std::string(static_cast<char*>(contents), size*nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
Curl::Curl() {
|
Curl::Curl() {
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
curl_handle = curl_easy_init();
|
curl_handle = curl_easy_init();
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback);
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback);
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
Curl::~Curl() {
|
Curl::~Curl() {
|
||||||
curl_easy_cleanup(curl_handle);
|
curl_easy_cleanup(curl_handle);
|
||||||
curl_global_cleanup();
|
curl_global_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Curl::execute(const std::string &url) {
|
std::string Curl::execute(const std::string &url) {
|
||||||
std::string source;
|
std::string source;
|
||||||
source.resize(100000);
|
source.resize(100000);
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source));
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source));
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
|
||||||
auto res = curl_easy_perform(curl_handle);
|
auto res = curl_easy_perform(curl_handle);
|
||||||
if(res != CURLE_OK) {
|
if(res != CURLE_OK) {
|
||||||
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
|
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}");
|
auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}");
|
||||||
for( const auto& p: FSLib::Directory(path) ) {
|
for( const auto& p: FSLib::Directory(path) ) {
|
||||||
if(FSLib::isDirectory(path + "/" + p)) {
|
if(FSLib::isDirectory(path + "/" + p)) {
|
||||||
findSeason(files, season, path + "/" + p);
|
findSeason(files, season, path + "/" + p);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( std::regex_search(p, episode) ) {
|
if( std::regex_search(p, episode) ) {
|
||||||
files.insert(path + "/" + p);
|
files.insert(path + "/" + p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void iterateFS(std::set<int> &seasons, const std::string &path) {
|
void iterateFS(std::set<int> &seasons, const std::string &path) {
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
auto episode = std::regex("[sS][0-9]{1,2000}[eE][0-9]{1,2000}");
|
auto episode = std::regex("[sS][0-9]{1,2000}[eE][0-9]{1,2000}");
|
||||||
for( const auto& p: FSLib::Directory(path) ) {
|
for( const auto& p: FSLib::Directory(path) ) {
|
||||||
if(FSLib::isDirectory(path + "/" + p)) {
|
if(FSLib::isDirectory(path + "/" + p)) {
|
||||||
iterateFS(seasons, path + "/" + p);
|
iterateFS(seasons, path + "/" + p);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( std::regex_search(p, match, episode) ) {
|
if( std::regex_search(p, match, episode) ) {
|
||||||
std::string a = match[0];
|
std::string a = match[0];
|
||||||
a = std::regex_replace(a, std::regex("[eE][0-9]{1,2000}"), "");
|
a = std::regex_replace(a, std::regex("[eE][0-9]{1,2000}"), "");
|
||||||
a = a.substr(1);
|
a = a.substr(1);
|
||||||
seasons.insert(std::stoi(a));
|
seasons.insert(std::stoi(a));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c ) {
|
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c ) {
|
||||||
auto search = std::regex_replace(show, std::regex(" "), "+");
|
auto search = std::regex_replace(show, std::regex(" "), "+");
|
||||||
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language);
|
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language);
|
||||||
auto series = std::regex("<td><a href=\"/series.*?</td>");
|
auto series = std::regex("<td><a href=\"/series.*?</td>");
|
||||||
int pos = 1;
|
int pos = 1;
|
||||||
std::vector<std::string> urls;
|
std::vector<std::string> urls;
|
||||||
for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) {
|
for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) {
|
||||||
auto input = (*it).str();
|
auto input = (*it).str();
|
||||||
auto text = std::regex_replace(input, std::regex(".*series.*?>"), "");
|
auto text = std::regex_replace(input, std::regex(".*series.*?>"), "");
|
||||||
text = text.substr(0, text.size() - 9);
|
text = text.substr(0, text.size() - 9);
|
||||||
std::cout << pos << ". " << text << std::endl;
|
std::cout << pos << ". " << text << std::endl;
|
||||||
pos++;
|
pos++;
|
||||||
urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13));
|
urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13));
|
||||||
}
|
}
|
||||||
std::cout << "Which TV Show is the right one? ";
|
std::cout << "Which TV Show is the right one? ";
|
||||||
std::cin >> pos;
|
std::cin >> pos;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
std::cin.ignore(1, '\n');
|
std::cin.ignore(1, '\n');
|
||||||
return "https://www.thetvdb.com" + urls[pos-1];
|
return "https://www.thetvdb.com" + urls[pos-1];
|
||||||
}
|
}
|
||||||
|
|
||||||
void printHelp() {
|
void printHelp() {
|
||||||
std::cout << "usage: tv_rename [--help] [--show show name] [--season season number]" << 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 << " [--correct-path] [--show-path show path] [--trust]" << std::endl;
|
||||||
std::cout << " [--linux] [--lang language] [--print-langs]" << 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 << 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 << " -h, --help\t\tshow this help message and exit" << std::endl;
|
||||||
std::cout << " --show show name, -s show name" << 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\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 << "\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 << " --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\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\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 << "\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 << " --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 << " --show-path show path, -p show path" << std::endl;
|
||||||
std::cout << "\t\t\tPath of the directory with episodes" << 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 << " --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 << " --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 << " --lang language, -l language" << std::endl;
|
||||||
std::cout << "\t\t\tSelect which language the episode names shoud be in" << 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 << " --print-langs\t\tPring available language" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,11 +7,11 @@
|
|||||||
|
|
||||||
class Curl {
|
class Curl {
|
||||||
public:
|
public:
|
||||||
Curl();
|
Curl();
|
||||||
~Curl();
|
~Curl();
|
||||||
std::string execute( const std::string &url );
|
std::string execute( const std::string &url );
|
||||||
private:
|
private:
|
||||||
CURL *curl_handle;
|
CURL *curl_handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c );
|
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c );
|
||||||
|
Loading…
Reference in New Issue
Block a user