179 lines
2.9 KiB
C++
179 lines
2.9 KiB
C++
#include "monstercat_dl.h"
|
|
#include "curl_dl.h"
|
|
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
using std::endl;
|
|
using std::map;
|
|
using std::ofstream;
|
|
using std::string;
|
|
using std::stringstream;
|
|
|
|
using nlohmann::json;
|
|
|
|
Monstercat_DL::Monstercat_DL()
|
|
: log(&std::cout), m_base_url("https://player.monstercat.app/api/"),
|
|
m_is_logged_in(false)
|
|
{}
|
|
|
|
Monstercat_DL::~Monstercat_DL()
|
|
{
|
|
}
|
|
|
|
bool Monstercat_DL::login(const string& user, const string& pass)
|
|
{
|
|
CURL_DL& curl = CURL_DL::get_handle();
|
|
bool ok;
|
|
json data;
|
|
string url;
|
|
stringstream out;
|
|
|
|
data["Email"] = user;
|
|
data["Password"] = pass;
|
|
|
|
url = m_base_url;
|
|
url += "sign-in";
|
|
|
|
ok = curl.post_json(url, data, &out);
|
|
if (!ok)
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Could not post json" << endl;
|
|
*log << "CURL:" << curl.get_error() << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
data.clear();
|
|
out >> data;
|
|
|
|
if (data.contains("Needs2FA") &&
|
|
data["Needs2FA"].is_boolean() &&
|
|
data["Needs2FA"] == false)
|
|
{
|
|
m_is_logged_in = true;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Monstercat_DL::logout()
|
|
{
|
|
CURL_DL& curl = CURL_DL::get_handle();
|
|
bool ok;
|
|
json data;
|
|
string url;
|
|
|
|
if (!m_is_logged_in)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
url = m_base_url;
|
|
url += "sign-out";
|
|
|
|
ok = curl.post_json(url, data, nullptr);
|
|
if (!ok)
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Could not post json" << endl;
|
|
*log << "CURL:" << curl.get_error() << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
return ok;
|
|
}
|
|
|
|
json Monstercat_DL::get_release_json(const string& catalog_id)
|
|
{
|
|
CURL_DL& curl = CURL_DL::get_handle();
|
|
bool ok;
|
|
json result;
|
|
string url;
|
|
stringstream out;
|
|
|
|
url = m_base_url;
|
|
url += "catalog/release/";
|
|
url += catalog_id;
|
|
|
|
ok = curl.download(url, &out);
|
|
if (!ok)
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Could not download json" << endl;
|
|
*log << "CURL:" << curl.get_error() << endl;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
out >> result;
|
|
return result;
|
|
}
|
|
|
|
bool Monstercat_DL::download_cover(const string& catalog_id, const string& path)
|
|
{
|
|
CURL_DL& curl = CURL_DL::get_handle();
|
|
bool ok;
|
|
string url;
|
|
stringstream out;
|
|
ofstream out_file;
|
|
map<string, string> out_headers;
|
|
string filename;
|
|
|
|
url = "https://www.monstercat.com/release/";
|
|
url += catalog_id;
|
|
url += "/cover";
|
|
|
|
ok = curl.download(url, &out, nullptr, &out_headers);
|
|
if (!ok)
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Could not download image" << endl;
|
|
*log << "CURL:" << curl.get_error() << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
if (0 == out_headers.count("content-type"))
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Unknown content-type" << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
filename = path;
|
|
if (out_headers["content-type"] == "image/jpeg")
|
|
{
|
|
filename += ".jpg";
|
|
}
|
|
else if (out_headers["content-type"] == "image/png")
|
|
{
|
|
filename += ".png";
|
|
}
|
|
|
|
out_file.open(filename, std::ios::binary);
|
|
if (!out_file.is_open())
|
|
{
|
|
if (nullptr != log)
|
|
{
|
|
*log << "Could not open file for write" << endl;
|
|
*log << filename << endl;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
out_file << out.rdbuf();
|
|
out_file.close();
|
|
|
|
return true;
|
|
}
|