Extract LastFm module

This commit is contained in:
Claudio Ortolina
2025-02-21 16:02:18 +00:00
parent 4835d4bdee
commit 889ba61e4c
5 changed files with 67 additions and 69 deletions
+43
View File
@@ -0,0 +1,43 @@
defmodule LastFm do
def get_artist_info(musicbrainz_id, name) do
last_fm_config = last_fm_config()
case last_fm_config.api.get_artist_info(
{:musicbrainz_id, musicbrainz_id},
last_fm_config
) do
{:ok, info} ->
{:ok, info}
{:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
last_fm_config.api.get_artist_info({:name, name}, last_fm_config)
error ->
error
end
end
def get_similar_artists(musicbrainz_id, name) do
last_fm_config = last_fm_config()
case last_fm_config.api.get_similar_artists(
{:musicbrainz_id, musicbrainz_id},
last_fm_config
) do
{:ok, info} ->
{:ok, info}
{:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
last_fm_config.api.get_similar_artists({:name, name}, last_fm_config)
error ->
error
end
end
defp last_fm_config, do: LastFm.Config.resolve(:music_library)
end
-42
View File
@@ -19,46 +19,4 @@ defmodule MusicLibrary.Artists do
q |> Repo.all() |> MapSet.new()
end
def get_artist_info(artist) do
last_fm_config = last_fm_config()
case last_fm_config.api.get_artist_info(
{:musicbrainz_id, artist.musicbrainz_id},
last_fm_config
) do
{:ok, info} ->
{:ok, info}
{:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
last_fm_config.api.get_artist_info({:name, artist.name}, last_fm_config)
error ->
error
end
end
def get_similar_artists(artist) do
last_fm_config = last_fm_config()
case last_fm_config.api.get_similar_artists(
{:musicbrainz_id, artist.musicbrainz_id},
last_fm_config
) do
{:ok, info} ->
{:ok, info}
{:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name.
last_fm_config.api.get_similar_artists({:name, artist.name}, last_fm_config)
error ->
error
end
end
defp last_fm_config, do: LastFm.Config.resolve(:music_library)
end
@@ -27,7 +27,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|> assign(:collection_records_count, Enum.count(grouped_artist_records.collection))
|> assign(:wishlist_records_count, Enum.count(grouped_artist_records.wishlist))
|> assign_async(:artist_info, fn ->
with {:ok, artist_info} <- Artists.get_artist_info(artist) do
with {:ok, artist_info} <- LastFm.get_artist_info(artist.musicbrainz_id, artist.name) do
{:ok, %{artist_info: artist_info}}
end
end)
+23
View File
@@ -0,0 +1,23 @@
defmodule LastFmTest do
use ExUnit.Case, async: true
alias LastFm.APIMock
alias LastFm.Fixtures.Artist
import Mox
setup :verify_on_exit!
describe "get_artist_info/1" do
test "it returns the artist info" do
name = "Steven Wilson"
musicbrainz_id = Ecto.UUID.generate()
expected_info = Artist.get_info()
expect(APIMock, :get_artist_info, fn {:musicbrainz_id, ^musicbrainz_id}, _config ->
{:ok, expected_info}
end)
assert {:ok, expected_info} == LastFm.get_artist_info(musicbrainz_id, name)
end
end
end
-26
View File
@@ -1,13 +1,8 @@
defmodule MusicLibrary.ArtistsTest do
use MusicLibrary.DataCase
alias LastFm.APIMock
alias MusicLibrary.Artists
import MusicLibrary.Fixtures.Records
import LastFm.Fixtures.Artist
import Mox
setup :verify_on_exit!
describe "get_artist/1" do
test "it returns records with essential data" do
@@ -34,25 +29,4 @@ defmodule MusicLibrary.ArtistsTest do
assert expected == Artists.get_all_artist_ids()
end
end
describe "get_artist_info/1" do
test "it returns the artist info" do
collection_record =
record_with_artist("Steven Wilson", %{
title: "The Raven that refused to sing",
purchased_at: DateTime.utc_now()
})
[artist] = collection_record.artists
artist_musicbrainz_id = artist.musicbrainz_id
expected_info = get_info()
expect(APIMock, :get_artist_info, fn {:musicbrainz_id, ^artist_musicbrainz_id}, _config ->
{:ok, expected_info}
end)
assert {:ok, expected_info} == Artists.get_artist_info(artist)
end
end
end