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