From ab7a8eeb5aab9794a69ba184297b7a1d5a3fb399 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 3 Dec 2024 09:30:29 +0000 Subject: [PATCH] Add play count to LastFm.Artist --- lib/last_fm/api_impl.ex | 1 + lib/last_fm/artist.ex | 16 +++++++++++++--- test/last_fm/artist_test.exs | 3 ++- test/support/fixtures/artist.getinfo.json | 5 +++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/last_fm/api_impl.ex b/lib/last_fm/api_impl.ex index 418c3570..488ddff5 100644 --- a/lib/last_fm/api_impl.ex +++ b/lib/last_fm/api_impl.ex @@ -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 diff --git a/lib/last_fm/artist.ex b/lib/last_fm/artist.ex index 983f69ab..ead08bb6 100644 --- a/lib/last_fm/artist.ex +++ b/lib/last_fm/artist.ex @@ -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 diff --git a/test/last_fm/artist_test.exs b/test/last_fm/artist_test.exs index 55bb37f5..3f32e532 100644 --- a/test/last_fm/artist_test.exs +++ b/test/last_fm/artist_test.exs @@ -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. Read more on Last.fm", 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 diff --git a/test/support/fixtures/artist.getinfo.json b/test/support/fixtures/artist.getinfo.json index e328288a..67eb26ed 100644 --- a/test/support/fixtures/artist.getinfo.json +++ b/test/support/fixtures/artist.getinfo.json @@ -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" } -} \ No newline at end of file +}