Extend import to support choosing format

This commit is contained in:
Claudio Ortolina
2024-09-28 20:23:22 +01:00
parent 82e5962c74
commit 64ea8fe3fa
4 changed files with 25 additions and 8 deletions
+6 -4
View File
@@ -60,17 +60,18 @@ defmodule MusicLibrary.Records do
MusicBrainz.search_release_group(query, limit: limit, offset: offset)
end
def import_from_musicbrainz(musicbrainz_id) do
with {:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id),
def import_from_musicbrainz(musicbrainz_id, opts \\ []) do
with format = Keyword.get(opts, :format, "cd"),
{:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id),
{:ok, image_data} <- MusicBrainz.get_cover_art(musicbrainz_id),
record_params = build_record_params(release_group, image_data) do
record_params = build_record_params(release_group, image_data, format) do
create_record(record_params)
else
error -> error
end
end
defp build_record_params(release_group, image_data) do
defp build_record_params(release_group, image_data, format) do
musicbrainz_id = release_group["id"]
artists_attrs =
@@ -91,6 +92,7 @@ defmodule MusicLibrary.Records do
"artists" => artists_attrs,
"year" => parse_year(release_group["first-release-date"]),
"type" => parse_subtype(release_group["primary-type"]),
"format" => format,
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
"image_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
"image_data" => image_data
+5 -1
View File
@@ -2,11 +2,13 @@ defmodule MusicLibrary.Records.Record do
use Ecto.Schema
import Ecto.Changeset
@formats [:cd, :vinyl, :blu_ray, :dvd, :multi]
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "records" do
field :type, Ecto.Enum, values: [:album, :ep, :live, :compilation, :single, :other]
field :format, Ecto.Enum, values: [:cd, :vinyl, :blu_ray, :dvd, :multi]
field :format, Ecto.Enum, values: @formats
field :title, :string
field :image_url, :string
field :image_data, :binary
@@ -58,6 +60,8 @@ defmodule MusicLibrary.Records.Record do
change(record, image_data: image_data)
end
def formats, do: @formats
def format_short_label(:cd), do: "CD"
def format_short_label(:vinyl), do: "V"
def format_short_label(:blu_ray), do: "BR"