diff --git a/README b/README index f910e78..a2bd982 100644 --- a/README +++ b/README @@ -26,14 +26,16 @@ metaflac --preserve-modtime --no-utf8-convert - // First Step - Remove - --remove --block-type=PICTURE + // First Step - Remove Tags --remove-tag=TITLE --remove-tag=ARTIST --remove-tag=ALBUM --remove-tag=TRACKNUMBER - // Second Step - Add + // Second Step - Remove Pictures + --remove --block-type=PICTURE + + // Third Step - Add --import-picture-from=3|image/jpeg|||"/path/to/cover" "--set-tag=TITLE=..." "--set-tag=ARTIST=..." diff --git a/TODO b/TODO index 7debbcb..e69de29 100644 --- a/TODO +++ b/TODO @@ -1 +0,0 @@ -Implement main diff --git a/example_download2.json b/example_download2.json new file mode 100644 index 0000000..87ef15d --- /dev/null +++ b/example_download2.json @@ -0,0 +1,10 @@ +{ + "comment": "This file will attempt to download MCS1091, MCS1195, MCS1425, MCS1426", + "releases": + [ + "MCS1091", + "MCS1195", + "MCS1425", + "MCS1426" + ] +} diff --git a/main.cpp b/main.cpp index 72d694f..1e7b0fb 100644 --- a/main.cpp +++ b/main.cpp @@ -64,7 +64,7 @@ void usage(const string& name) cout << " -1 " << endl; cout << " Single release" << endl; cout << " -j " << endl; - cout << " JSON file denoting release range" << endl; + cout << " JSON file denoting release range or releases array" << endl; cout << " path - root path to download to" << endl; cout << " Default: ." << endl; } @@ -171,15 +171,16 @@ pair get_artist_title(const json& obj) pair artist_feat; string str; string feat; + string version; bool contains; int pos; - artist_feat = get_artist_feat(obj["Release"]["ArtistsTitle"]); + artist_feat = get_artist_feat(obj["ArtistsTitle"]); result.first = artist_feat.first; contains = false; - str = obj["Release"]["Title"]; + str = obj["Title"]; if (!artist_feat.second.empty()) { str += " ("; @@ -188,19 +189,12 @@ pair get_artist_title(const json& obj) contains = true; } - if (!(obj["Release"]["Version"].empty())) + // Empty does not work on json string + version = obj["Version"]; + if (!version.empty()) { - if (contains) - { - str += " ["; - } - else - { - str += " ("; - } - - str += obj["Release"]["Version"]; - + str += contains ? " [" : " ("; + str += version; str += contains ? ']' : ')'; } @@ -573,7 +567,7 @@ bool resize_cover(const string& path, bool is_jpg) } cmd = MAGICK_INVOKE; - cmd += "\""; + cmd += " \""; cmd += path; cmd += FOLDER_DELIM; cmd += "Cover"; @@ -663,9 +657,8 @@ bool tag_FLAC(const string& path, const string& filename, bool is_single_dir, string cmd; bool ok; - // First command - remove metadata + // First command - remove tags cmd = "metaflac --preserve-modtime --no-utf8-convert "; - cmd += "--remove --block-type=PICTURE "; cmd += "--remove-tag=TITLE "; cmd += "--remove-tag=ARTIST "; cmd += "--remove-tag=ALBUM "; @@ -686,7 +679,26 @@ bool tag_FLAC(const string& path, const string& filename, bool is_single_dir, return false; } - // Second command - set metadata + // Second command - remove pictures + cmd = "metaflac --preserve-modtime --no-utf8-convert "; + cmd += "--remove --block-type=PICTURE \""; + cmd += path; + cmd += FOLDER_DELIM; + if (!is_single_dir) + { + cmd += "FLAC"; + cmd += FOLDER_DELIM; + } + cmd += filename; + cmd += ".flac\""; + + ok = system_command(cmd); + if (!ok) + { + return false; + } + + // Third command - set metadata cmd = "metaflac --preserve-modtime --no-utf8-convert --dont-use-padding "; cmd += "\"--import-picture-from=3|image/jpeg|||"; cmd += path; @@ -729,7 +741,7 @@ bool full_donwload(const string& path, const string& release_prefix, catalog_release_str = release_prefix; catalog_release_str += num_to_str(release_num, 3); - catalog_release_str == release_suffix; + catalog_release_str += release_suffix; release_precision = 3; if (release_prefix == "MCS") @@ -788,7 +800,7 @@ bool full_donwload(const string& path, const string& release_prefix, } // Download Cover - ok = download_cover(info["Release"]["Id"], release_dir); + ok = download_cover(catalog_release_str, release_dir); if (!ok) { return false; @@ -860,8 +872,215 @@ bool full_donwload(const string& path, const string& release_prefix, return true; } +bool break_down_release_str(const string& release, string& prefix, int& num, string& suffix) +{ + int pos; + int pos2; + string digits = "0123456789"; + + prefix = ""; + num = 0; + suffix = ""; + + pos = release.find_first_of(digits); + if (string::npos == pos) + { + return false; + } + + prefix = release.substr(0, pos); + pos2 = release.find_first_not_of(digits, pos); + if (string::npos == pos2) + { + try + { + num = stod(release.substr(pos)); + } + catch(const std::exception& e) + { + return false; + } + } + else + { + try + { + num = stod(release.substr(pos, pos2 - pos)); + } + catch(const std::exception& e) + { + return false; + } + suffix = release.substr(pos2); + } + + return true; +} + int main(int argc, char **argv) { - // TODO: Implement + vector args(&argv[1], &argv[argc]); + + pair single_release = {false, ""}; + pair json_option = {false, ""}; + string path = "."; + + if (argc < 3) + { + cout << "Too few arguments" << endl; + usage(argv[0]); + return -1; + } + + // Ugly getopt + for (int i = 0; i < args.size(); ++i) + { + if ((args[i] == "-1") && (i + 1 < args.size())) + { + single_release.first = true; + single_release.second = args[i + 1]; + args.erase(args.begin() + i); + args.erase(args.begin() + i); + --i; + continue; + } + + if ((args[i] == "-j") && (i + 1 < args.size())) + { + json_option.first = true; + json_option.second = args[i + 1]; + args.erase(args.begin() + i); + args.erase(args.begin() + i); + --i; + continue; + } + } + + // Both selected + if (single_release.first && json_option.first) + { + cout << "Both single release and json file are selected." << endl; + usage(argv[0]); + return -1; + } + + // None selected + if (!single_release.first && !json_option.first) + { + cout << "Neither single release nor json file are selected." << endl; + usage(argv[0]); + return -1; + } + + // Add path from arguments + if (!args.empty()) + { + path = args[0]; + } + + // Remove trailing slash + if (path[path.size() - 1] == FOLDER_DELIM) + { + path.erase(path.size() - 1); + } + +#if 1 + // BEGIN TEST PRINTS + if (single_release.first) + { + cout << "SINGLE: " << single_release.second << endl; + } + if (json_option.first) + { + cout << "JSON: " << json_option.second << endl; + } + cout << "PATH: " << path << endl; + // END TEST PRINTS +#endif + + vector releases; + vector::iterator it; + + // Add single release + if (single_release.first) + { + releases.push_back(single_release.second); + } + + // Parse JSON + if (json_option.first) + { + json j = parse_json_file(json_option.second); + + if (j.contains("releases") && j["releases"].is_array()) + { + for (int i = 0; i < j["releases"].size(); ++i) + { + releases.push_back(j["releases"][i]); + } + } + else if (j.contains("prefix") && + j.contains("start") && j["start"].is_number_integer() && + j.contains("end") && j["end"].is_number_integer()) + { + string tmp; + for (int i = j["start"]; i <= j["end"]; ++i) + { + // Add release + tmp = j["prefix"]; + tmp += num_to_str(i, 3); + releases.push_back(tmp); + + // Add release with suffix + if (j.contains("suffix_try") && !j["suffix_try"].empty()) + { + tmp += j["suffix_try"]; + releases.push_back(tmp); + } + } + } + else + { + cout << "JSON not recognized" << endl; + return -1; + } + } + + string release_prefix; + int release_num; + string release_suffix; + bool ok; + + + ok = login(); + if (!ok) + { + cout << "Failed to login" << endl; + return -1; + } + + // Break down release string + for (it = releases.begin(); it != releases.end(); ++it) + { + ok = break_down_release_str(*it, release_prefix, release_num, release_suffix); + if (!ok) + { + cout << "Failed to break down - " << *it << endl; + continue; + } + + ok = full_donwload(path, release_prefix, release_num, release_suffix); + if (!ok) + { + cout << "Could not download - " << *it << endl; + } + } + + ok = logout(); + if (!ok) + { + cout << "Failed to logout" << endl; + } + return 0; }