Add endpoint to display an artist image

This commit is contained in:
Claudio Ortolina
2025-04-27 21:18:08 +01:00
parent cacdfbaee0
commit 3d3e758a7a
4 changed files with 90 additions and 0 deletions
+31
View File
@@ -73,6 +73,24 @@ defmodule MusicLibrary.Artists do
end
end
def get_artist_info!(artist_id) do
Repo.get!(ArtistInfo, artist_id)
end
def fetch_image(artist_id) do
artist_info = get_artist_info!(artist_id)
with {:ok, image} <- ArtistInfo.extract_image(artist_info),
{:ok, image_data} <- Discogs.get_artist_image(image.url) do
artist_info
|> ArtistInfo.changeset(%{
image_data: image_data,
image_data_width: image.width
})
|> Repo.update()
end
end
def fetch_artist_info_async(artist_id) do
meta = %{}
params = %{"id" => artist_id}
@@ -82,6 +100,19 @@ defmodule MusicLibrary.Artists do
|> BackgroundRepo.insert()
end
def get_image(artist_id) do
q =
from ai in ArtistInfo,
where: ai.id == ^artist_id,
select: %{
image_data: ai.image_data,
image_data_width: ai.image_data_width,
image_data_hash: ai.image_data_hash
}
Repo.one(q)
end
defp get_collected_artist_ids do
q =
from ar in ArtistRecord,
+17
View File
@@ -41,4 +41,21 @@ defmodule MusicLibrary.Records.ArtistInfo do
put_change(changeset, :image_data_hash, Cover.hash(image_data))
end
end
def extract_image(artist_info) when is_nil(artist_info.discogs_data) do
{:error, :no_discogs_data}
end
def extract_image(artist_info) do
primary_image =
Enum.find(artist_info.discogs_data["images"], fn image ->
image["type"] == "primary"
end)
if primary_image do
{:ok, %{url: primary_image["resource_url"], width: primary_image["width"]}}
else
{:error, :image_not_found}
end
end
end