1 #include "lib/lyrics.hpp"
2
lyrics(const lib::http_client & http_client)3 lib::lyrics::lyrics(const lib::http_client &http_client)
4 : http(http_client)
5 {
6 }
7
get(const spt::track & track,lib::callback<lib::spt::track_info> & callback)8 void lib::lyrics::get(const spt::track &track, lib::callback<lib::spt::track_info> &callback)
9 {
10 std::string url = "https://spotify-lyrics.azurewebsites.net/lyrics";
11 nlohmann::json body{
12 {"artist", track.artists.front().name},
13 {"name", track.name},
14 };
15 lib::headers headers{
16 {"Content-Type", "application/json"},
17 };
18
19 try
20 {
21 http.post(url, body.dump(), headers, [url, callback]
22 (const std::string &result)
23 {
24 if (result.empty())
25 {
26 callback(lib::spt::track_info());
27 return;
28 }
29 callback(nlohmann::json::parse(result));
30 });
31 }
32 catch (const std::exception &e)
33 {
34 lib::log::error("Failed to get lyrics: {}", e.what());
35 callback(lib::spt::track_info());
36 }
37 }
38