From cfe37f20a5ceb3e954ef4375b6c6145a5d3a2ce0 Mon Sep 17 00:00:00 2001 From: DWW Date: Wed, 20 Jul 2022 16:52:52 +0300 Subject: [PATCH] Added Cover renaming --- .gitignore | 1 + TODO | 3 +- main.cpp | 257 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 249 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index c116647..30b87c6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ login.json release.json cookies.txt mcat_dl* +.vscode/* diff --git a/TODO b/TODO index fbd12d8..0a504d0 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ -Add Cover Resize via stb - https://github.com/nothings/stb -Add Cover rename to match extension +Add Cover Resize via lib or command ??? Add MP3 Tagging Add FLAC Tagging Implement main diff --git a/main.cpp b/main.cpp index 0bd916e..7705435 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,13 @@ // C++ Libs #include #include +#include #include #include #include // C Libs +#include #include // File Libs @@ -21,6 +23,9 @@ struct tag constexpr const char RELEASE_JSON[] = "release.json"; constexpr bool IS_MP3 = true; constexpr bool IS_FLAC = false; +constexpr bool IS_JPG = true; +constexpr bool IS_PNG = false; +constexpr uint32_t COVER_SIZE = 750; #ifdef _WIN32 constexpr const char FOLDER_DELIM = '\\'; @@ -63,12 +68,6 @@ void usage(const string& name) cout << " Default: ." << endl; } -// string get_track_name(const json& info, int track_num) -// { - -// return ""; -// } - bool system_command(const string& cmd) { int result = system(cmd.c_str()); @@ -350,6 +349,229 @@ string get_track_filename(int track_num, const string& artist, return filename; } +bool should_resize_JPG(ifstream& file) +{ + // Do a resize by default + bool result = true; + int marker = 0; + int symbol; + + // Check Header 1 + symbol = file.get(); + if (symbol != 0xFF) + { + return result; + } + + // Check Header 2 + symbol = file.get(); + if (symbol != 0xD8) + { + return result; + } + + while(1) + { + int discarded_bytes = 0; + + if (file.eof()) + { + return result; + } + marker = file.get(); + + // Seek to 0xFF + while (marker != 0xFF) + { + discarded_bytes++; + if (file.eof()) + { + return result; + } + marker = file.get(); + } + + // Find end of 0xFF + do + { + if (file.eof()) + { + return result; + } + marker = file.get(); + } + while (marker == 0xFF); + + // ??? + if (0 == discarded_bytes) + { + return result; + } + + switch(marker) + { + // Read width & height + case 0xC0: + case 0xC1: + case 0xC2: + case 0xC3: + case 0xC5: + case 0xC6: + case 0xC7: + case 0xC9: + case 0xCA: + case 0xCB: + case 0xCD: + case 0xCE: + case 0xCF: + int16_t width; + int16_t height; + + // Skip 3 bytes + file.get(); + file.get(); + file.get(); + + file.read(reinterpret_cast(&height), 2); + file.read(reinterpret_cast(&width), 2); + + if ((width > COVER_SIZE) || (height > COVER_SIZE)) + { + return true; + } + else + { + return false; + } + break; + + // ??? + case 0xDA: + case 0xD9: + return result; + break; + + // Skip length-2 bytes + default: + int16_t length; + + file.read(reinterpret_cast(&length), 2); + if (length < 2) + { + return 0; + } + + length -= 2; + for (int i = 0; i < length; ++i) + { + file.get(); + } + + if (file.eof()) + { + return result; + } + break; + } + } +} + +bool should_resize_PNG(ifstream& file) +{ + uint8_t buf[4]; + uint32_t width; + uint32_t height; + + // Seek + file.read(reinterpret_cast(&buf), 4); + file.read(reinterpret_cast(&buf), 4); + file.read(reinterpret_cast(&buf), 4); + file.read(reinterpret_cast(&buf), 4); + + // Read width + width = 0; + file.read(reinterpret_cast(&buf), 4); + for (int i = 0; i < 4; ++i) + { + width <<= 8; + width += buf[i]; + } + + // Read height + height = 0; + file.read(reinterpret_cast(&buf), 4); + for (int i = 0; i < 4; ++i) + { + height <<= 8; + height += buf[i]; + } + + if ((width > COVER_SIZE) || (height > COVER_SIZE)) + { + return true; + } + else + { + return false; + } +} + +bool get_cover_type(const string& path) +{ + string filename; + ifstream file; + int header; + + filename = path; + filename += FOLDER_DELIM; + filename += "Cover"; + + file.open(filename); + + header = file.peek(); + switch (header) + { + // JPG + case 0xFF: + return IS_JPG; + break; + case 0x89: + return IS_PNG; + break; + } + + throw invalid_argument(filename); +} + +bool rename_cover(const string& path, bool is_jpg) +{ + string filename; + string filename_new; + int error; + + filename = path; + filename += FOLDER_DELIM; + filename += "Cover"; + + filename_new = filename; + if (is_jpg == IS_JPG) + { + filename_new += ".jpg"; + } + else + { + filename_new += ".png"; + } + + error = rename(filename.c_str(), filename_new.c_str()); + if (0 != error) + { + return false; + } + + return true; +} + bool full_donwload(const string& path, const string& release_prefix, int release_num, const string& release_suffix) { @@ -361,6 +583,7 @@ bool full_donwload(const string& path, const string& release_prefix, pair release_artist_title; int release_precision; bool is_single_dir; + bool cover_is_jpg; vector tags; catalog_release_str = release_prefix; @@ -416,9 +639,7 @@ bool full_donwload(const string& path, const string& release_prefix, tmp += FOLDER_DELIM; tmp += "FLAC"; - ok = make_dir(tmp); - if (!ok) - { + ok = make_dir(tmp);--dont-use-padding return false; } } @@ -430,8 +651,24 @@ bool full_donwload(const string& path, const string& release_prefix, return false; } + // Try to find out Cover type + try + { + cover_is_jpg = get_cover_type(release_dir); + } + catch (const std::exception& e) + { + return false; + } + // TODO: Make 750x750 Cover JPEG Image - // TODO: Rename Cover Image (Proper Extension) + + // Rename Cover Image (Proper Extension) + ok = rename_cover(release_dir, cover_is_jpg); + if (!ok) + { + return false; + } tags = get_tags_from_release(info); for (int i = 0; i < info["Tracks"].size(); ++i)