Add play count to LastFm.Artist

This commit is contained in:
Claudio Ortolina
2024-12-03 09:30:29 +00:00
parent 2094c1cf7b
commit ab7a8eeb5a
4 changed files with 19 additions and 6 deletions
+1
View File
@@ -51,6 +51,7 @@ defmodule LastFm.APIImpl do
options = [
method: "artist.getInfo",
api_key: config.api_key,
user: config.user,
mbid: artist_mbid,
format: "json",
limit: 50
+13 -3
View File
@@ -1,11 +1,12 @@
defmodule LastFm.Artist do
defstruct [:musicbrainz_id, :name, :bio, :image]
defstruct [:musicbrainz_id, :name, :bio, :image, :play_count]
@type t :: %__MODULE__{
musicbrainz_id: String.t(),
name: String.t(),
bio: String.t(),
image: String.t()
image: String.t(),
play_count: non_neg_integer()
}
def from_api_response(api_response) do
@@ -13,7 +14,8 @@ defmodule LastFm.Artist do
musicbrainz_id: api_response["mbid"],
name: api_response["name"],
bio: api_response["bio"]["summary"],
image: get_image(api_response)
image: get_image(api_response),
play_count: get_play_count(api_response)
}
end
@@ -22,4 +24,12 @@ defmodule LastFm.Artist do
|> Enum.find(%{"#text" => nil}, fn i -> i["size"] == "medium" end)
|> Map.get("#text")
end
defp get_play_count(api_response) do
if play_count = get_in(api_response, ["stats", "userplaycount"]) do
String.to_integer(play_count)
else
0
end
end
end
+2 -1
View File
@@ -17,7 +17,8 @@ defmodule LastFm.ArtistTest do
bio:
"Steven Wilson (born Steven John Wilson on November 3, 1967, in Hemel Hempstead, Hertfordshire, England) is an English musician, singer, songwriter and record producer, most closely associated with the progressive rock genre. Currently a solo artist, he became known as the founder, lead guitarist, lead vocalist and songwriter of the British rock band Porcupine Tree, as well as being a member of several other bands.\n\nWilson is self-taught as a producer, audio engineer, multi-instrumentalist and singer-songwriter. <a href=\"https://www.last.fm/music/Steven+Wilson\">Read more on Last.fm</a>",
image:
"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png"
"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",
play_count: 123
} ==
LastFm.Artist.from_api_response(api_response)
end
+3 -2
View File
@@ -197,7 +197,8 @@
},
"stats": {
"listeners": "293020",
"playcount": "20567544"
"playcount": "20567544",
"userplaycount": "123"
},
"streamable": "0",
"tags": {
@@ -226,4 +227,4 @@
},
"url": "https://www.last.fm/music/Steven+Wilson"
}
}
}