Added donwloading tracks and files

This commit is contained in:
Nedko 2024-07-01 11:28:18 +03:00
parent 2279f572cb
commit 0b61461039
2 changed files with 96 additions and 11 deletions

View File

@ -475,7 +475,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path)
stringstream out; stringstream out;
ofstream out_file; ofstream out_file;
map<string, string> out_headers; map<string, string> out_headers;
string filename; string filepath;
url = "https://www.monstercat.com/release/"; url = "https://www.monstercat.com/release/";
url += catalog_id; url += catalog_id;
@ -493,7 +493,7 @@ bool Monstercat_DL::download_cover(const string& catalog_id, const string& path)
return false; return false;
} }
filename = path; filepath = path;
if (0 == out_headers.count("content-type")) if (0 == out_headers.count("content-type"))
{ {
if (nullptr != m_log) 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") if (out_headers["content-type"] == "image/jpeg")
{ {
filename += ".jpg"; filepath += ".jpg";
} }
else if (out_headers["content-type"] == "image/png") else if (out_headers["content-type"] == "image/png")
{ {
filename += ".png"; filepath += ".png";
} }
else 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 (!out_file.is_open())
{ {
if (nullptr != m_log) if (nullptr != m_log)
{ {
*m_log << __PRETTY_FUNCTION__ << endl; *m_log << __PRETTY_FUNCTION__ << endl;
*m_log << "Could not open file for write" << endl; *m_log << "Could not open file for write" << endl;
*m_log << filename << endl; *m_log << filepath << endl;
} }
return false; 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, 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 url;
string filepath;
stringstream out_data;
ofstream out_file;
url = m_base_url; url = m_base_url;
url += "release/"; url += "release/";
url += release_id; url += release_id;
url += "/track-download/"; url += "/track-download/";
url += track_id; url += track_id;
if (is_mp3)
{
url += "?format=mp3_320";
}
else
{
url += "?format=flac";
}
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; 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 url;
string filepath;
stringstream out_data;
ofstream out_file;
url = m_base_url; url = m_base_url;
url += "file/"; url += "file/";
url += file_id; url += file_id;
url += "/open?download=true"; 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; return false;
} }

View File

@ -6,6 +6,9 @@
#include <vector> #include <vector>
#include "json.hpp" #include "json.hpp"
constexpr bool FORMAT_MP3 = true;
constexpr bool FORMAT_FLAC = false;
struct Track struct Track
{ {
public: public:
@ -61,8 +64,8 @@ public:
bool download_cover(const std::string& catalog_id, const std::string& path); bool download_cover(const std::string& catalog_id, const std::string& path);
bool download_track(const std::string& release_id, bool download_track(const std::string& release_id,
const std::string& track_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& path); bool download_file(const std::string& file_id, const std::string& filepath);
}; };