Extract Artists context
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
defmodule MusicLibrary.Artists do
|
||||
import Ecto.Query, warn: false
|
||||
alias MusicLibrary.Repo
|
||||
|
||||
alias MusicLibrary.Records.ArtistRecord
|
||||
|
||||
def get_artist!(musicbrainz_id) do
|
||||
q =
|
||||
from ar in ArtistRecord,
|
||||
where: ar.musicbrainz_id == ^musicbrainz_id,
|
||||
limit: 1,
|
||||
select: ar.artist
|
||||
|
||||
Repo.one!(q)
|
||||
end
|
||||
|
||||
def get_all_artist_ids do
|
||||
q = from ar in ArtistRecord, distinct: true, select: ar.musicbrainz_id
|
||||
|
||||
q |> Repo.all() |> MapSet.new()
|
||||
end
|
||||
|
||||
def get_artist_info(artist) do
|
||||
last_fm_config = last_fm_config()
|
||||
# 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.
|
||||
case last_fm_config.api.get_artist_info(
|
||||
{:musicbrainz_id, artist.musicbrainz_id},
|
||||
last_fm_config
|
||||
) do
|
||||
{:ok, info} ->
|
||||
{:ok, info}
|
||||
|
||||
# TODO: remap error codes
|
||||
{:error, %{"error" => 6}} ->
|
||||
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()
|
||||
# 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.
|
||||
case last_fm_config.api.get_similar_artists(
|
||||
{:musicbrainz_id, artist.musicbrainz_id},
|
||||
last_fm_config
|
||||
) do
|
||||
{:ok, info} ->
|
||||
{:ok, info}
|
||||
|
||||
# TODO: remap error codes
|
||||
{:error, %{"error" => 6}} ->
|
||||
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
|
||||
@@ -90,22 +90,6 @@ defmodule MusicLibrary.Records do
|
||||
|
||||
def get_record!(id), do: Repo.get!(Record, id)
|
||||
|
||||
def get_artist!(musicbrainz_id) do
|
||||
q =
|
||||
from ar in ArtistRecord,
|
||||
where: ar.musicbrainz_id == ^musicbrainz_id,
|
||||
limit: 1,
|
||||
select: ar.artist
|
||||
|
||||
Repo.one!(q)
|
||||
end
|
||||
|
||||
def get_all_artist_ids do
|
||||
q = from ar in ArtistRecord, distinct: true, select: ar.musicbrainz_id
|
||||
|
||||
q |> Repo.all() |> MapSet.new()
|
||||
end
|
||||
|
||||
def get_artist_records(musicbrainz_id) do
|
||||
q =
|
||||
from r in Record,
|
||||
@@ -116,46 +100,6 @@ defmodule MusicLibrary.Records do
|
||||
Repo.all(q)
|
||||
end
|
||||
|
||||
def get_artist_info(artist) do
|
||||
last_fm_config = last_fm_config()
|
||||
# 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.
|
||||
case last_fm_config.api.get_artist_info(
|
||||
{:musicbrainz_id, artist.musicbrainz_id},
|
||||
last_fm_config
|
||||
) do
|
||||
{:ok, info} ->
|
||||
{:ok, info}
|
||||
|
||||
# TODO: remap error codes
|
||||
{:error, %{"error" => 6}} ->
|
||||
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()
|
||||
# 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.
|
||||
case last_fm_config.api.get_similar_artists(
|
||||
{:musicbrainz_id, artist.musicbrainz_id},
|
||||
last_fm_config
|
||||
) do
|
||||
{:ok, info} ->
|
||||
{:ok, info}
|
||||
|
||||
# TODO: remap error codes
|
||||
{:error, %{"error" => 6}} ->
|
||||
last_fm_config.api.get_similar_artists({:name, artist.name}, last_fm_config)
|
||||
|
||||
error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
def get_cover(id) do
|
||||
q =
|
||||
from r in Record,
|
||||
@@ -299,6 +243,4 @@ defmodule MusicLibrary.Records do
|
||||
end
|
||||
|
||||
defp music_brainz_config, do: MusicBrainz.Config.resolve(:music_library)
|
||||
|
||||
defp last_fm_config, do: LastFm.Config.resolve(:music_library)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user