Added Folder and filename generation

This commit is contained in:
Nedko 2024-06-28 15:17:14 +03:00
parent ee703e6fde
commit 2279f572cb
2 changed files with 123 additions and 0 deletions

View File

@ -1,14 +1,17 @@
#include "monstercat_dl.h"
#include "curl_dl.h"
#include <algorithm>
#include <fstream>
#include <sstream>
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;
}

View File

@ -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);