Discman
Loading...
Searching...
No Matches
last_fm.cc
Go to the documentation of this file.
1
6
7#include "last_fm.h"
8
9std::string LastFM::_api_key;
10
12 if (!std::getenv("LASTFM_API_KEY")) {
14 }
15}
16
18 LastFM::_api_key = std::getenv("LASTFM_API_KEY");
19}
20
21std::vector<AlbumArtProvider::AlbumArt> LastFM::album_art(const std::string& artist, const std::string& title, const int width, const int height) {
22 cURLpp::Cleanup cleanup;
23 cURLpp::Easy easyhandle;
24
25 std::string request_url = url(Method::AlbumGetInfo, {
26 {"artist", artist},
27 {"album", title}
28 });
29
30 std::stringstream ss;
31 easyhandle.setOpt(cURLpp::Options::Url(request_url));
32 easyhandle.setOpt(cURLpp::Options::WriteStream(&ss));
33 easyhandle.perform();
34
35 Json::Value root;
36 ss >> root;
37
38 const Json::Value image = root["album"]["image"];
39
40 const std::vector<std::string> sizes = {{
41 "mega",
42 "extralarge",
43 "large",
44 "medium",
45 "small",
46 ""
47 }
48 };
49
50 std::vector<AlbumArtProvider::AlbumArt> albumArts;
51
52 for (unsigned int i = 0; i < sizes.size(); i++) {
53 for (unsigned int j = 0; j < image.size(); j++) {
54 const Json::Value element = image[j];
55 if (element["size"] == sizes[i]) {
56 request_url = element["#text"].asString();
57 if (!request_url.empty()) {
58 easyhandle.setOpt(cURLpp::Options::Url(request_url));
59
60 ss.str("");
61 easyhandle.perform();
62
63 std::string data = ss.str();
64 Glib::RefPtr<Glib::Bytes> bytes = Glib::Bytes::create(data.c_str(), data.size());
65 Glib::RefPtr<Gio::MemoryInputStream> is = Gio::MemoryInputStream::create();
66 is->add_bytes(bytes);
67
69 .art = Gdk::Pixbuf::create_from_stream_at_scale(is, width, height, true),
70 .url = request_url
71 };
72
73 albumArts.push_back(art);
74 }
75 }
76 }
77 }
78
79 if (albumArts.empty()) {
80 throw NotFoundException();
81 }
82
83 return albumArts;
84}
85
86std::string LastFM::method_name(const Method method) {
87 switch (method) {
88 default:
89 case AlbumGetInfo:
90 return "album.getinfo";
91 }
92}
93
94std::string LastFM::url(const Method method, const std::map<std::string, std::string>& params) {
95 std::map<std::string, std::string> full_params(params);
96 full_params["method"] = method_name(method);
97 full_params["api_key"] = _api_key;
98 full_params["format"] = "json";
99
100 return url_with_params(BASE_URL, full_params);
101}
static std::string url_with_params(const std::string &url, const std::map< std::string, std::string > &params)
Returns the URL of the API endpoint with the given parameters encoded as query parameters.
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.
Definition last_fm.cc:21
static std::string method_name(const Method method)
Conversion method to convert a LastFM::Method to a string.
Definition last_fm.cc:86
static std::string _api_key
last.fm API key.
Definition last_fm.h:70
Method
last.fm API methods.
Definition last_fm.h:35
@ AlbumGetInfo
Definition last_fm.h:36
void init() override
Retrieves the last.fm API key as an environment variable.
Definition last_fm.cc:17
static constexpr const char * BASE_URL
The last.fm API base URL.
Definition last_fm.h:40
LastFM()
LastFM constructor.
Definition last_fm.cc:11
static std::string url(const Method method, const std::map< std::string, std::string > &params)
Calls url_with_params() after adding additional parameters in params. These additional parameters set...
Definition last_fm.cc:94
An album art image and its URL.
Thrown when the provider cannot be reached.
Thrown when the provider cannot find album art images.