diff --git a/monstercat_dl.cpp b/monstercat_dl.cpp index 22c6301..351c710 100644 --- a/monstercat_dl.cpp +++ b/monstercat_dl.cpp @@ -78,10 +78,12 @@ string Monstercat_DL::calc_proper_title(const string& artist_raw, result += trim_whitespace(version_raw); result += ")"; } + + return result; } Monstercat_DL::Monstercat_DL() -: log(&std::cout), m_base_url("https://player.monstercat.app/api/"), +: m_log(&std::cout), m_base_url("https://player.monstercat.app/api/"), m_is_logged_in(false) {} @@ -106,10 +108,10 @@ bool Monstercat_DL::login(const string& user, const string& pass) ok = curl.post_json(url, data, &out); if (!ok) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Could not post json" << endl; - *log << "CURL:" << curl.get_error() << endl; + *m_log << "Could not post json" << endl; + *m_log << "CURL:" << curl.get_error() << endl; } return false; } @@ -146,10 +148,10 @@ bool Monstercat_DL::logout() ok = curl.post_json(url, data, nullptr); if (!ok) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Could not post json" << endl; - *log << "CURL:" << curl.get_error() << endl; + *m_log << "Could not post json" << endl; + *m_log << "CURL:" << curl.get_error() << endl; } return false; } @@ -172,10 +174,10 @@ json Monstercat_DL::get_release_json(const string& catalog_id) ok = curl.download(url, &out); if (!ok) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Could not download json" << endl; - *log << "CURL:" << curl.get_error() << endl; + *m_log << "Could not download json" << endl; + *m_log << "CURL:" << curl.get_error() << endl; } return result; } @@ -199,10 +201,10 @@ json Monstercat_DL::get_browse_json(const string& release_id) ok = curl.download(url, &out); if (!ok) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Could not download json" << endl; - *log << "CURL:" << curl.get_error() << endl; + *m_log << "Could not download json" << endl; + *m_log << "CURL:" << curl.get_error() << endl; } return result; } @@ -231,6 +233,7 @@ Release Monstercat_DL::parse_release_json(const json& release_json) } // Parse Release + json_extract(release_object, "CatalogId", result.catalog_id); json_extract(release_object, "Id", result.id); json_extract(release_object, "Type", result.type); json_extract(release_object, "ReleaseDate", result.release_date); @@ -281,6 +284,73 @@ Release Monstercat_DL::parse_release_json(const json& release_json) return result; } +void Monstercat_DL::add_extended_mixes(Release& release, const json& browse_json) +{ + int track_num; + json data_obj; + json file_obj; + Track *track; + string mime_type; + + if (browse_json.contains("Data") && browse_json["Data"].is_array()) + { + data_obj = browse_json["Data"]; + } + + for (json& track_json : data_obj) + { + // Get the track number + track_num = 0; + if (track_json.contains("TrackNumber") && track_json["TrackNumber"].is_number_integer()) + { + track_num = track_json["TrackNumber"]; + } + + // File must exist and be an object + if (!track_json.contains("File") || !track_json["File"].is_object()) + { + continue; + } + + // Find track + track = nullptr; + 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 << "Could not find track number " << track_num << " for catalog id " << release.catalog_id << endl; + } + return; + } + + file_obj = track_json["File"]; + json_extract(file_obj, "Id", track->extended_mix_file_id); + json_extract(file_obj, "MimeType", mime_type); + + if ("audio/wav" == mime_type) + { + track->extended_mix_extension = ".wav"; + } + else + { + if (nullptr != m_log) + { + *m_log << "Unknown MIME type for catalog id " << release.catalog_id << " - " << mime_type << endl; + } + } + return; + } +} + bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { CURL_DL& curl = CURL_DL::get_handle(); @@ -298,10 +368,10 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) ok = curl.download(url, &out, nullptr, &out_headers); if (!ok) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Could not download image" << endl; - *log << "CURL:" << curl.get_error() << endl; + *m_log << "Could not download image" << endl; + *m_log << "CURL:" << curl.get_error() << endl; } return false; } @@ -309,9 +379,9 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) filename = path; if (0 == out_headers.count("content-type")) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "No content-type" << endl; + *m_log << "No content-type" << endl; } } else @@ -326,9 +396,9 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) } else { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Unknown Content-Type for cover - " << out_headers["content-type"] << endl; + *m_log << "Unknown Content-Type for cover - " << out_headers["content-type"] << endl; } } } @@ -336,10 +406,10 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) out_file.open(filename, std::ios::binary); if (!out_file.is_open()) { - if (nullptr != log) + if (nullptr != m_log) { - *log << "Could not open file for write" << endl; - *log << filename << endl; + *m_log << "Could not open file for write" << endl; + *m_log << filename << endl; } return false; } diff --git a/monstercat_dl.h b/monstercat_dl.h index ebdf96e..66615f1 100644 --- a/monstercat_dl.h +++ b/monstercat_dl.h @@ -27,12 +27,13 @@ struct Release std::vector tracks; std::string id; + std::string catalog_id; }; class Monstercat_DL { private: - std::ostream *log; + std::ostream *m_log; std::string m_base_url; @@ -53,6 +54,7 @@ public: nlohmann::json get_browse_json(const std::string& release_id); Release parse_release_json(const nlohmann::json& release_json); + void add_extended_mixes(Release& release, const nlohmann::json& browse_json); bool download_cover(const std::string& catalog_id, const std::string& path); bool download_track(const std::string& release_id,