Added array example. Implemented main.

This commit is contained in:
DWW 2022-08-08 17:03:00 +03:00
parent 74598813de
commit 588925cee8
4 changed files with 256 additions and 26 deletions

8
README
View File

@ -26,14 +26,16 @@ metaflac
--preserve-modtime --preserve-modtime
--no-utf8-convert --no-utf8-convert
// First Step - Remove // First Step - Remove Tags
--remove --block-type=PICTURE
--remove-tag=TITLE --remove-tag=TITLE
--remove-tag=ARTIST --remove-tag=ARTIST
--remove-tag=ALBUM --remove-tag=ALBUM
--remove-tag=TRACKNUMBER --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" --import-picture-from=3|image/jpeg|||"/path/to/cover"
"--set-tag=TITLE=..." "--set-tag=TITLE=..."
"--set-tag=ARTIST=..." "--set-tag=ARTIST=..."

1
TODO
View File

@ -1 +0,0 @@
Implement main

10
example_download2.json Normal file
View File

@ -0,0 +1,10 @@
{
"comment": "This file will attempt to download MCS1091, MCS1195, MCS1425, MCS1426",
"releases":
[
"MCS1091",
"MCS1195",
"MCS1425",
"MCS1426"
]
}

263
main.cpp
View File

@ -64,7 +64,7 @@ void usage(const string& name)
cout << " -1 <catalog release id>" << endl; cout << " -1 <catalog release id>" << endl;
cout << " Single release" << endl; cout << " Single release" << endl;
cout << " -j <json file>" << endl; cout << " -j <json file>" << 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 << " path - root path to download to" << endl;
cout << " Default: ." << endl; cout << " Default: ." << endl;
} }
@ -171,15 +171,16 @@ pair<string, string> get_artist_title(const json& obj)
pair<string, string> artist_feat; pair<string, string> artist_feat;
string str; string str;
string feat; string feat;
string version;
bool contains; bool contains;
int pos; int pos;
artist_feat = get_artist_feat(obj["Release"]["ArtistsTitle"]); artist_feat = get_artist_feat(obj["ArtistsTitle"]);
result.first = artist_feat.first; result.first = artist_feat.first;
contains = false; contains = false;
str = obj["Release"]["Title"]; str = obj["Title"];
if (!artist_feat.second.empty()) if (!artist_feat.second.empty())
{ {
str += " ("; str += " (";
@ -188,19 +189,12 @@ pair<string, string> get_artist_title(const json& obj)
contains = true; contains = true;
} }
if (!(obj["Release"]["Version"].empty())) // Empty does not work on json string
version = obj["Version"];
if (!version.empty())
{ {
if (contains) str += contains ? " [" : " (";
{ str += version;
str += " [";
}
else
{
str += " (";
}
str += obj["Release"]["Version"];
str += contains ? ']' : ')'; str += contains ? ']' : ')';
} }
@ -573,7 +567,7 @@ bool resize_cover(const string& path, bool is_jpg)
} }
cmd = MAGICK_INVOKE; cmd = MAGICK_INVOKE;
cmd += "\""; cmd += " \"";
cmd += path; cmd += path;
cmd += FOLDER_DELIM; cmd += FOLDER_DELIM;
cmd += "Cover"; cmd += "Cover";
@ -663,9 +657,8 @@ bool tag_FLAC(const string& path, const string& filename, bool is_single_dir,
string cmd; string cmd;
bool ok; bool ok;
// First command - remove metadata // First command - remove tags
cmd = "metaflac --preserve-modtime --no-utf8-convert "; cmd = "metaflac --preserve-modtime --no-utf8-convert ";
cmd += "--remove --block-type=PICTURE ";
cmd += "--remove-tag=TITLE "; cmd += "--remove-tag=TITLE ";
cmd += "--remove-tag=ARTIST "; cmd += "--remove-tag=ARTIST ";
cmd += "--remove-tag=ALBUM "; cmd += "--remove-tag=ALBUM ";
@ -686,7 +679,26 @@ bool tag_FLAC(const string& path, const string& filename, bool is_single_dir,
return false; 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 = "metaflac --preserve-modtime --no-utf8-convert --dont-use-padding ";
cmd += "\"--import-picture-from=3|image/jpeg|||"; cmd += "\"--import-picture-from=3|image/jpeg|||";
cmd += path; cmd += path;
@ -729,7 +741,7 @@ bool full_donwload(const string& path, const string& release_prefix,
catalog_release_str = release_prefix; catalog_release_str = release_prefix;
catalog_release_str += num_to_str(release_num, 3); catalog_release_str += num_to_str(release_num, 3);
catalog_release_str == release_suffix; catalog_release_str += release_suffix;
release_precision = 3; release_precision = 3;
if (release_prefix == "MCS") if (release_prefix == "MCS")
@ -788,7 +800,7 @@ bool full_donwload(const string& path, const string& release_prefix,
} }
// Download Cover // Download Cover
ok = download_cover(info["Release"]["Id"], release_dir); ok = download_cover(catalog_release_str, release_dir);
if (!ok) if (!ok)
{ {
return false; return false;
@ -860,8 +872,215 @@ bool full_donwload(const string& path, const string& release_prefix,
return true; 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) int main(int argc, char **argv)
{ {
// TODO: Implement vector<string> args(&argv[1], &argv[argc]);
pair<bool, string> single_release = {false, ""};
pair<bool, string> 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<string> releases;
vector<string>::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; return 0;
} }