From 2279f572cb2ca139058a737a800833bb64a17adf Mon Sep 17 00:00:00 2001 From: Nedko Date: Fri, 28 Jun 2024 15:17:14 +0300 Subject: [PATCH] Added Folder and filename generation --- monstercat_dl.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++ monstercat_dl.h | 3 ++ 2 files changed, 123 insertions(+) diff --git a/monstercat_dl.cpp b/monstercat_dl.cpp index 351c710..6c3929a 100644 --- a/monstercat_dl.cpp +++ b/monstercat_dl.cpp @@ -1,14 +1,17 @@ #include "monstercat_dl.h" #include "curl_dl.h" +#include #include #include using std::endl; using std::map; using std::ofstream; +using std::sort; using std::string; using std::stringstream; +using std::to_string; using nlohmann::json; @@ -110,6 +113,7 @@ bool Monstercat_DL::login(const string& user, const string& pass) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not post json" << endl; *m_log << "CURL:" << curl.get_error() << endl; } @@ -150,6 +154,7 @@ bool Monstercat_DL::logout() { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not post json" << endl; *m_log << "CURL:" << curl.get_error() << endl; } @@ -176,6 +181,7 @@ json Monstercat_DL::get_release_json(const string& catalog_id) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not download json" << endl; *m_log << "CURL:" << curl.get_error() << endl; } @@ -203,6 +209,7 @@ json Monstercat_DL::get_browse_json(const string& release_id) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not download json" << endl; *m_log << "CURL:" << curl.get_error() << endl; } @@ -281,6 +288,12 @@ Release Monstercat_DL::parse_release_json(const json& release_json) result.tracks.push_back(temp_track); } + // Sort tracks by number + sort(result.tracks.begin(), result.tracks.end(), [](const Track& t1, const Track& t2) + { + return t1.number < t2.number; + }); + return result; } @@ -327,6 +340,7 @@ void Monstercat_DL::add_extended_mixes(Release& release, const json& browse_json { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not find track number " << track_num << " for catalog id " << release.catalog_id << endl; } return; @@ -344,6 +358,7 @@ void Monstercat_DL::add_extended_mixes(Release& release, const json& browse_json { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Unknown MIME type for catalog id " << release.catalog_id << " - " << mime_type << endl; } } @@ -351,6 +366,107 @@ 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; + + // Ensure main path ends with / + result = main_folder; + if (result.empty()) + { + result = "./"; + } + + if (*(result.rbegin()) != '/') + { + result += '/'; + } + + result += release.type; + result += '/'; + result += release.release_date; + result += " - "; + result += release.catalog_id; + result += " - "; + result += release.artist; + result += " - "; + result += release.title; + + return result; +} + +string Monstercat_DL::calc_track_filename(const Release& release, int track_num) +{ + string result; + bool use_number; + bool use_artist; + const Track *track = nullptr; + + // Find the track + for (int i = 0; i < release.tracks.size(); ++i) + { + if (release.tracks[i].number == track_num) + { + track = &(release.tracks[i]); + break; + } + } + + if (nullptr == track) + { + if (nullptr != m_log) + { + *m_log << __PRETTY_FUNCTION__ << endl; + *m_log << "Cannot find track number " << track_num << " in catalog id " << release.catalog_id << endl; + } + + return ""; + } + + // Determine what we need + if (1 == release.tracks.size()) + { + use_number = false; + use_artist = true; + } + else + { + use_number = true; + if (track->artist == release.artist) + { + use_artist = false; + } + else + { + use_artist = true; + } + } + + // Build filename + result = ""; + if (use_number) + { + result += to_string(track->number); + // 2 digits always + if (result.size() < 2) + { + result.insert(result.begin(), '0'); + } + + result += " - "; + } + + if (use_artist) + { + result += track->artist; + result += " - "; + } + + result += track->title; + + return result; +} + bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { CURL_DL& curl = CURL_DL::get_handle(); @@ -370,6 +486,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not download image" << endl; *m_log << "CURL:" << curl.get_error() << endl; } @@ -381,6 +498,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "No content-type" << endl; } } @@ -398,6 +516,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Unknown Content-Type for cover - " << out_headers["content-type"] << endl; } } @@ -408,6 +527,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { if (nullptr != m_log) { + *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not open file for write" << endl; *m_log << filename << endl; } diff --git a/monstercat_dl.h b/monstercat_dl.h index 66615f1..602fb00 100644 --- a/monstercat_dl.h +++ b/monstercat_dl.h @@ -56,6 +56,9 @@ public: Release parse_release_json(const nlohmann::json& release_json); void add_extended_mixes(Release& release, const nlohmann::json& browse_json); + std::string calc_release_folder(const Release& release, const std::string& main_folder); + std::string calc_track_filename(const Release& release, int track_num); + bool download_cover(const std::string& catalog_id, const std::string& path); bool download_track(const std::string& release_id, const std::string& track_id, const std::string& path);