1 #include "lib/spotify/playback.hpp"
2
from_json(const nlohmann::json & j,playback & p)3 void lib::spt::from_json(const nlohmann::json &j, playback &p)
4 {
5 if (!j.is_object() || !j.contains("item"))
6 {
7 return;
8 }
9
10 j.at("progress_ms").get_to(p.progress_ms);
11 j.at("item").get_to(p.item);
12 j.at("is_playing").get_to(p.is_playing);
13 j.at("shuffle_state").get_to(p.shuffle);
14 j.at("context").get_to(p.context);
15 j.at("device").get_to(p.device);
16
17 auto repeat_state = j.at("repeat_state").get<std::string>();
18 p.repeat = repeat_state == "track"
19 ? lib::repeat_state::track
20 : repeat_state == "context"
21 ? lib::repeat_state::context
22 : lib::repeat_state::off;
23 }
24
to_json(nlohmann::json & j,const playback & p)25 void lib::spt::to_json(nlohmann::json &j, const playback &p)
26 {
27 j = nlohmann::json{
28 {"progress_ms", p.progress_ms},
29 {"item", p.item},
30 {"is_playing", p.is_playing},
31 {"repeat", p.repeat},
32 {"shuffle", p.shuffle}
33 };
34 }
35
metadata() const36 auto lib::spt::playback::metadata() const -> nlohmann::json
37 {
38 auto artist_names = entity::combine_names(item.artists);
39
40 return {
41 {"xesam:title", item.name},
42 {"xesam:artist", artist_names},
43 {"xesam:album", item.album},
44 {"xesam:albumArtist", artist_names},
45 {"xesam:url", lib::fmt::format("https://open.spotify.com/track/{}", item.id)},
46 {"mpris:length", item.duration * 1000},
47 {"mpris:artUrl", item.image},
48 {"mpris:trackid", lib::fmt::format("spotify:track:{}", item.id)},
49 };
50 }
51
volume() const52 auto lib::spt::playback::volume() const -> int
53 {
54 return device.volume_percent;
55 }
56