18 std::string clientId = std::getenv(
"SPOTIFY_CLIENT_ID");
19 std::string clientSecret = std::getenv(
"SPOTIFY_CLIENT_SECRET");
21 cURLpp::Cleanup cleanup;
22 cURLpp::Easy easyhandle;
24 std::string request_url =
"https://accounts.spotify.com/api/token";
26 std::list<std::string> headers;
27 headers.push_back(
"Authorization: Basic "+
base64_encode(clientId+
":"+clientSecret));
28 headers.push_back(
"Content-Type: application/x-www-form-urlencoded");
30 std::string form =
"grant_type=client_credentials";
33 easyhandle.setOpt(cURLpp::Options::Url(request_url));
34 easyhandle.setOpt(cURLpp::Options::HttpHeader(headers));
35 easyhandle.setOpt(
new curlpp::options::PostFields(form));
36 easyhandle.setOpt(
new curlpp::options::PostFieldSize(form.size()));
37 easyhandle.setOpt(cURLpp::Options::WriteStream(&ss));
46std::vector<AlbumArtProvider::AlbumArt>
Spotify::album_art(
const std::string& artist,
const std::string& title,
const int width,
const int height) {
47 cURLpp::Cleanup cleanup;
48 cURLpp::Easy easyhandle;
51 {
"q",
"artist:"+artist+
" album:"+title},
56 std::list<std::string> headers;
57 headers.push_back(
"Authorization: Bearer "+
_accessToken);
60 easyhandle.setOpt(cURLpp::Options::Url(request_url));
61 easyhandle.setOpt(cURLpp::Options::HttpHeader(headers));
62 easyhandle.setOpt(cURLpp::Options::WriteStream(&ss));
65 int httpCode = curlpp::infos::ResponseCode::get(easyhandle);
67 if (httpCode == 401) {
77 std::map<int, std::vector<std::string>> urlsBySize;
79 Json::Value items = root[
"albums"][
"items"];
81 for (
unsigned int i = 0; i < items.size(); i++) {
82 Json::Value images = items[i][
"images"];
84 for (
unsigned int j = 0; j < images.size(); j++) {
85 int size = images[j][
"width"].asInt() * images[j][
"height"].asInt();
86 std::string
url = images[j][
"url"].asString();
88 if (urlsBySize.contains(size)) {
89 urlsBySize[size].push_back(
url);
92 urlsBySize[size] = {{
url }};
97 std::vector<AlbumArt> arts;
99 for (
auto it = urlsBySize.rbegin(); it != urlsBySize.rend(); ++it) {
100 for (
const std::string&
url : it->second) {
101 easyhandle.setOpt(cURLpp::Options::Url(
url));
104 easyhandle.perform();
106 std::string data = ss.str();
107 Glib::RefPtr<Glib::Bytes> bytes = Glib::Bytes::create(data.c_str(), data.size());
108 Glib::RefPtr<Gio::MemoryInputStream> is = Gio::MemoryInputStream::create();
109 is->add_bytes(bytes);
112 .art = Gdk::Pixbuf::create_from_stream_at_scale(is, width, height,
true),
128 BIO* b64 = BIO_new(BIO_f_base64());
129 BIO* bio = BIO_new(BIO_s_mem());
130 bio = BIO_push(b64, bio);
132 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
134 BIO_write(bio, input.c_str(), input.length());
138 BIO_get_mem_ptr(bio, &bufferPtr);
140 std::string output(bufferPtr->data, bufferPtr->length);
static std::string url_with_params(const std::string &url, const std::map< std::string, std::string > ¶ms)
Returns the URL of the API endpoint with the given parameters encoded as query parameters.
static std::string url(const Method method, const std::map< std::string, std::string > ¶ms)
Calls url_with_params() with the Spotify API method URL.
std::vector< AlbumArt > album_art(const std::string &artist, const std::string &title, const int width, const int height) override
Retrieves all album art matching the artist and title.