diff --git a/common.cpp b/common.cpp new file mode 100644 index 0000000..77d5966 --- /dev/null +++ b/common.cpp @@ -0,0 +1,157 @@ +#include "common.h" + +#include + +#include + +using std::endl; +using std::ifstream; +using std::istream; +using std::ostream; +using std::set; +using std::string; +using std::to_string; +using nlohmann::json; + +bool read_config(json& cfg, ostream* log) +{ + ifstream cfg_file("config.json"); + + if (!cfg_file.is_open()) + { + if (nullptr != log) + { + *log << "Could not open config.json" << endl; + } + return false; + } + + // Parse with comments + try + { + cfg = json::parse(cfg_file, nullptr, true, true); + } + catch (const std::exception &e) + { + if (nullptr != log) + { + *log << e.what() << endl; + } + return false; + } + + return true; +} + +bool path_exists(const string& path) +{ + struct stat info; + int error; + + error = stat(path.c_str(), &info); + if (0 != error) + { + return false; + } + + if (S_ISDIR(info.st_mode)) + { + return true; + } + + return false; +} + +string build_fname(const string& main_path, const string& folder, const string& fname) +{ + string path_to_file; + + path_to_file = main_path; + if (!path_to_file.empty() && path_to_file[path_to_file.size() - 1] != '/') + { + path_to_file += '/'; + } + + path_to_file += folder; + if (!path_to_file.empty() && path_to_file[path_to_file.size() - 1] != '/') + { + path_to_file += '/'; + } + + path_to_file += fname; + + return path_to_file; +} + +string i_to_str(int i, int size, char fill) +{ + string str; + + str = to_string(i); + str.insert(str.begin(), size - str.size(), fill); + + return str; +} + +void json_extract(const json& j, const string& key, string& out) +{ + if (j.contains(key) && j[key].is_string()) + { + out = j[key]; + } +} + +void json_extract(const json& j, const string& key, uint16_t& out) +{ + if (j.contains(key) && j[key].is_number_unsigned()) + { + out = j[key]; + } +} + +void json_extract(const json& j, const string& key, bool& out) +{ + if (j.contains(key) && j[key].is_boolean()) + { + out = j[key]; + } +} + +bool ensure_folder(const string& main_path, const string& folder) +{ + string full_path; + int error; + + if (!path_exists(main_path)) + { + return false; + } + + full_path = build_fname(main_path, folder, ""); + if (path_exists(full_path)) + { + return true; + } + + error = mkdir(full_path.c_str(), 0775); + if (0 != error) + { + return false; + } + + return true; +} + +string trim_whitespace(const string& s) +{ + const string whitespace = " \t\r\n\f\v"; + string result(s); + size_t pos; + + pos = result.find_first_not_of(whitespace); + result.erase(0, pos); + pos = result.find_last_not_of(whitespace); + result.erase(pos + 1); + + return result; +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..12c21db --- /dev/null +++ b/common.h @@ -0,0 +1,22 @@ +#ifndef COMMON_H_ +#define COMMON_H_ + +#include +#include +#include +#include "json.hpp" + +bool read_config(nlohmann::json& cfg, std::ostream* log = &std::cout); +bool path_exists(const std::string& path); +std::string build_fname(const std::string& main_path, const std::string& folder, + const std::string& fname); +std::string i_to_str(int i, int size, char fill = '0'); +void json_extract(const nlohmann::json& j, const std::string& key, + std::string& out); +void json_extract(const nlohmann::json& j, const std::string& key, + uint16_t& out); +void json_extract(const nlohmann::json& j, const std::string& key, bool& out); +bool ensure_folder(const std::string& main_path, const std::string& folder); +std::string trim_whitespace(const std::string& s); + +#endif // COMMON_H_ diff --git a/config_example.json b/config_example.json new file mode 100644 index 0000000..730f21b --- /dev/null +++ b/config_example.json @@ -0,0 +1,8 @@ +{ + "Email": "user@example.ccom", + "Pass": "password", + "download_path": "/path/to/monstercat", + "convert_exec": "convert", + "eyed3_exec": "eyeD3", + "metaflac_exec": "metaflac" +} diff --git a/monstercat_dl.cpp b/monstercat_dl.cpp index 8591820..cc20f6d 100644 --- a/monstercat_dl.cpp +++ b/monstercat_dl.cpp @@ -1,4 +1,5 @@ #include "monstercat_dl.h" +#include "common.h" #include "curl_dl.h" #include @@ -15,33 +16,6 @@ using std::to_string; using nlohmann::json; -static bool json_extract(const json& j, const string& key, string& value) -{ - bool result = false; - - if (j.contains(key) && j[key].is_string()) - { - value = j[key]; - result = true; - } - - return result; -} - -static string trim_whitespace(const string& s) -{ - const string whitespace = " \t\r\n\f\v"; - string result(s); - size_t pos; - - pos = result.find_first_not_of(whitespace); - result.erase(0, pos); - pos = result.find_last_not_of(whitespace); - result.erase(pos + 1); - - return result; -} - string Monstercat_DL::calc_proper_artist(const string& artist_raw) { size_t pos; @@ -369,28 +343,21 @@ void Monstercat_DL::add_extended_mixes(Release& release, const json& browse_json string Monstercat_DL::calc_release_folder(const Release& release, const string& main_folder) { string result; + string folder_name; + string temp; - // Ensure main path ends with / - result = main_folder; - if (result.empty()) - { - result = "./"; - } + ensure_folder(main_folder, release.type); - if (*(result.rbegin()) != '/') - { - result += '/'; - } + folder_name = release.release_date; + folder_name += " - "; + folder_name += release.catalog_id; + folder_name += " - "; + folder_name += release.artist; + folder_name += " - "; + folder_name += release.title; - result += release.type; - result += '/'; - result += release.release_date; - result += " - "; - result += release.catalog_id; - result += " - "; - result += release.artist; - result += " - "; - result += release.title; + result = build_fname(main_folder, release.type, folder_name); + ensure_folder(result, ""); return result; } @@ -446,13 +413,8 @@ string Monstercat_DL::calc_track_filename(const Release& release, int track_num) result = ""; if (use_number) { - result += to_string(track->number); - // 2 digits always - if (result.size() < 2) - { - result.insert(result.begin(), '0'); - } - + // Always use 2 digits + result += i_to_str(track->number, 2, '0'); result += " - "; } @@ -494,6 +456,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) } filepath = path; + filepath += "Cover"; if (0 == out_headers.count("content-type")) { if (nullptr != m_log)