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;
ofstream out_file;
map<string, string> 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;
}

View File

@ -6,6 +6,9 @@
#include <vector>
#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);
};