From 0b61461039cc8fe7649824bd1ecdf9062f3cdab0 Mon Sep 17 00:00:00 2001 From: Nedko Date: Mon, 1 Jul 2024 11:28:18 +0300 Subject: [PATCH] Added donwloading tracks and files --- monstercat_dl.cpp | 100 +++++++++++++++++++++++++++++++++++++++++----- monstercat_dl.h | 7 +++- 2 files changed, 96 insertions(+), 11 deletions(-) diff --git a/monstercat_dl.cpp b/monstercat_dl.cpp index 6c3929a..8591820 100644 --- a/monstercat_dl.cpp +++ b/monstercat_dl.cpp @@ -475,7 +475,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) stringstream out; ofstream out_file; map out_headers; - string filename; + string filepath; url = "https://www.monstercat.com/release/"; url += catalog_id; @@ -493,7 +493,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) return false; } - filename = path; + filepath = path; if (0 == out_headers.count("content-type")) { if (nullptr != m_log) @@ -506,11 +506,11 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) { if (out_headers["content-type"] == "image/jpeg") { - filename += ".jpg"; + filepath += ".jpg"; } else if (out_headers["content-type"] == "image/png") { - filename += ".png"; + filepath += ".png"; } else { @@ -522,14 +522,14 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) } } - out_file.open(filename, std::ios::binary); + out_file.open(filepath, std::ios::binary); if (!out_file.is_open()) { if (nullptr != m_log) { *m_log << __PRETTY_FUNCTION__ << endl; *m_log << "Could not open file for write" << endl; - *m_log << filename << endl; + *m_log << filepath << endl; } return false; } @@ -541,27 +541,109 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path) } bool Monstercat_DL::download_track(const string& release_id, - const string& track_id, const string& path) + const string& track_id, const string& path, bool is_mp3) { + CURL_DL& curl = CURL_DL::get_handle(); + bool ok; string url; + string filepath; + stringstream out_data; + ofstream out_file; url = m_base_url; url += "release/"; url += release_id; url += "/track-download/"; url += track_id; + if (is_mp3) + { + url += "?format=mp3_320"; + } + else + { + url += "?format=flac"; + } - return false; + ok = curl.download(url, &out_data); + if (!ok) + { + if (nullptr != m_log) + { + *m_log << __PRETTY_FUNCTION__ << endl; + *m_log << "Could not download track" << endl; + *m_log << "CURL:" << curl.get_error() << endl; + } + return false; + } + + filepath = path; + if (is_mp3) + { + filepath += ".mp3"; + } + else + { + filepath += ".flac"; + } + + out_file.open(filepath, std::ios::binary); + if (!out_file.is_open()) + { + if (nullptr != m_log) + { + *m_log << __PRETTY_FUNCTION__ << endl; + *m_log << "Could not open file for write" << endl; + *m_log << filepath << endl; + } + return false; + } + + out_file << out_data.rdbuf(); + out_file.close(); + + return true; } -bool Monstercat_DL::download_file(const string& file_id, const string& path) +bool Monstercat_DL::download_file(const string& file_id, const string& filepath) { + CURL_DL& curl = CURL_DL::get_handle(); + bool ok; string url; + string filepath; + stringstream out_data; + ofstream out_file; url = m_base_url; url += "file/"; url += file_id; url += "/open?download=true"; + ok = curl.download(url, &out_data); + if (!ok) + { + if (nullptr != m_log) + { + *m_log << __PRETTY_FUNCTION__ << endl; + *m_log << "Could not download track" << endl; + *m_log << "CURL:" << curl.get_error() << endl; + } + return false; + } + + out_file.open(filepath, std::ios::binary); + if (!out_file.is_open()) + { + if (nullptr != m_log) + { + *m_log << __PRETTY_FUNCTION__ << endl; + *m_log << "Could not open file for write" << endl; + *m_log << filepath << endl; + } + return false; + } + + out_file << out_data.rdbuf(); + out_file.close(); + return false; } diff --git a/monstercat_dl.h b/monstercat_dl.h index 602fb00..710dcaa 100644 --- a/monstercat_dl.h +++ b/monstercat_dl.h @@ -6,6 +6,9 @@ #include #include "json.hpp" +constexpr bool FORMAT_MP3 = true; +constexpr bool FORMAT_FLAC = false; + struct Track { public: @@ -61,8 +64,8 @@ public: 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); - bool download_file(const std::string& file_id, const std::string& path); + const std::string& track_id, const std::string& filepath, bool is_mp3); + bool download_file(const std::string& file_id, const std::string& filepath); };