Extract musicbrainz behaviour and impl

This commit is contained in:
Claudio Ortolina
2024-10-05 20:30:18 +01:00
parent ea6a856f30
commit a72236449d
6 changed files with 33 additions and 41 deletions
+8 -4
View File
@@ -2,7 +2,7 @@ defmodule MusicLibrary.Records do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias MusicLibrary.Repo alias MusicLibrary.Repo
alias MusicLibrary.Records.{MusicBrainz, Record} alias MusicLibrary.Records.Record
@fields [:id, :type, :artists, :format, :title, :release, :genres, :musicbrainz_id, :cover_hash] @fields [:id, :type, :artists, :format, :title, :release, :genres, :musicbrainz_id, :cover_hash]
@@ -80,13 +80,13 @@ defmodule MusicLibrary.Records do
def search_release_group(query, opts \\ []) do def search_release_group(query, opts \\ []) do
limit = Keyword.get(opts, :limit, 20) limit = Keyword.get(opts, :limit, 20)
offset = Keyword.get(opts, :offset, 0) offset = Keyword.get(opts, :offset, 0)
MusicBrainz.search_release_group(query, limit: limit, offset: offset) musicbrainz().search_release_group(query, limit: limit, offset: offset)
end end
def import_from_musicbrainz(musicbrainz_id, opts \\ []) do def import_from_musicbrainz(musicbrainz_id, opts \\ []) do
with format = Keyword.get(opts, :format, "cd"), with format = Keyword.get(opts, :format, "cd"),
{:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id), {:ok, release_group} <- musicbrainz().get_release_group(musicbrainz_id),
{:ok, cover_data} <- MusicBrainz.get_cover_art(musicbrainz_id), {:ok, cover_data} <- musicbrainz().get_cover_art(musicbrainz_id),
record_params = build_record_params(release_group, cover_data, format) do record_params = build_record_params(release_group, cover_data, format) do
create_record(record_params) create_record(record_params)
else else
@@ -148,4 +148,8 @@ defmodule MusicLibrary.Records do
def change_record(%Record{} = record, attrs \\ %{}) do def change_record(%Record{} = record, attrs \\ %{}) do
Record.changeset(record, attrs) Record.changeset(record, attrs)
end end
defp musicbrainz do
Application.get_env(:music_library, :music_brainz, MusicLibrary.Records.MusicBrainz.APIImpl)
end
end end
+5 -36
View File
@@ -3,7 +3,6 @@ defmodule MusicLibrary.Records.Importer do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias MusicLibrary.Records.Record, as: Rec alias MusicLibrary.Records.Record, as: Rec
alias MusicLibrary.Records.MusicBrainz
alias MusicLibrary.Repo alias MusicLibrary.Repo
def import_all_artists do def import_all_artists do
@@ -26,42 +25,8 @@ defmodule MusicLibrary.Records.Importer do
end) end)
end end
@doc """
The original data from Obsidian maps records to release groups, so to find artists for a record we can
use the [lookup](https://musicbrainz.org/doc/MusicBrainz_API#Lookups) endpoint with the release group id and include the
artist credits.
Example request: https://musicbrainz.org/ws/2/release-group/ae504fd6-8498-463e-8d96-14f9e11d1863?fmt=json&inc=artist-credits
Example response:
{
"primary-type-id": "f529b476-6e62-324f-b0aa-1f3e33d313fc",
"id": "ae504fd6-8498-463e-8d96-14f9e11d1863",
"primary-type": "Album",
"secondary-types": [],
"disambiguation": "",
"title": "Dwellers of the Deep",
"secondary-type-ids": [],
"first-release-date": "2020-10-23",
"artist-credit": [
{
"artist": {
"type-id": "e431f5f6-b5d2-343d-8b36-72607fffb74b",
"sort-name": "Wobbler",
"id": "923b9160-251f-4ebe-8af2-ae670c425e55",
"type": "Group",
"name": "Wobbler",
"disambiguation": "Symphonic Prog, Norway"
},
"name": "Wobbler",
"joinphrase": ""
}
]
}
"""
def import_artists(record) do def import_artists(record) do
with {:ok, data} <- MusicBrainz.get_release_group(record.musicbrainz_id) do with {:ok, data} <- musicbrainz().get_release_group(record.musicbrainz_id) do
artists_attrs = artists_attrs =
data data
|> get_in(["artist-credit", Access.all(), "artist"]) |> get_in(["artist-credit", Access.all(), "artist"])
@@ -169,4 +134,8 @@ defmodule MusicLibrary.Records.Importer do
{:error, msg} {:error, msg}
end end
end end
defp musicbrainz do
Application.get_env(:music_library, :music_brainz, MusicLibrary.Records.MusicBrainz.APIImpl)
end
end end
@@ -0,0 +1,9 @@
defmodule MusicLibrary.Records.MusicBrainz.APIBehaviour do
@type musicbrainz_id :: String.t()
@callback get_release_group(musicbrainz_id) :: {:ok, map()} | {:error, String.t()}
@callback search_release_group(String.t(), Keyword.t()) :: {:ok, [map()]} | {:error, String.t()}
@callback get_cover_art(musicbrainz_id) :: {:ok, binary()} | {:error, String.t()}
end
@@ -1,4 +1,4 @@
defmodule MusicLibrary.Records.MusicBrainz do defmodule MusicLibrary.Records.MusicBrainz.APIImpl do
@moduledoc """ @moduledoc """
The original data from Obsidian maps records to MusicBrainz release groups, so we can leverage the MusicBrainz API to: The original data from Obsidian maps records to MusicBrainz release groups, so we can leverage the MusicBrainz API to:
@@ -6,6 +6,8 @@ defmodule MusicLibrary.Records.MusicBrainz do
- Extend the metadata associated with existing records - Extend the metadata associated with existing records
""" """
@behaviour MusicLibrary.Records.MusicBrainz.APIBehaviour
require Logger require Logger
@doc """ @doc """
@@ -41,6 +43,7 @@ defmodule MusicLibrary.Records.MusicBrainz do
] ]
} }
""" """
@impl true
def get_release_group(id) do def get_release_group(id) do
url = url =
"https://musicbrainz.org/ws/2/release-group/#{id}?fmt=json&inc=artist-credits+genres" "https://musicbrainz.org/ws/2/release-group/#{id}?fmt=json&inc=artist-credits+genres"
@@ -148,6 +151,7 @@ defmodule MusicLibrary.Records.MusicBrainz do
] ]
} }
""" """
@impl true
def search_release_group(query, opts) do def search_release_group(query, opts) do
limit = Keyword.fetch!(opts, :limit) limit = Keyword.fetch!(opts, :limit)
offset = Keyword.fetch!(opts, :offset) offset = Keyword.fetch!(opts, :offset)
@@ -179,6 +183,10 @@ defmodule MusicLibrary.Records.MusicBrainz do
end end
end end
@doc """
Uses the [cover art](https://musicbrainz.org/doc/Cover_Art_Archive/API) endpoint with the release group id to get the cover image.
"""
@impl true
def get_cover_art(musicbrainz_id) do def get_cover_art(musicbrainz_id) do
url = "https://coverartarchive.org/release-group/#{musicbrainz_id}/front" url = "https://coverartarchive.org/release-group/#{musicbrainz_id}/front"
+1
View File
@@ -44,6 +44,7 @@ defmodule MusicLibrary.MixProject do
# TODO bump on release to {:phoenix_live_view, "~> 1.0.0"}, # TODO bump on release to {:phoenix_live_view, "~> 1.0.0"},
{:phoenix_live_view, "~> 1.0.0-rc.1", override: true}, {:phoenix_live_view, "~> 1.0.0-rc.1", override: true},
{:floki, ">= 0.30.0", only: :test}, {:floki, ">= 0.30.0", only: :test},
{:mox, "~> 1.2"},
{:phoenix_live_dashboard, "~> 0.8.3"}, {:phoenix_live_dashboard, "~> 0.8.3"},
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev}, {:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2", runtime: Mix.env() == :dev}, {:tailwind, "~> 0.2", runtime: Mix.env() == :dev},
+1
View File
@@ -26,6 +26,7 @@
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"}, "mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
"mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"}, "mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"},
"mint": {:hex, :mint, "1.6.2", "af6d97a4051eee4f05b5500671d47c3a67dac7386045d87a904126fd4bbcea2e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "5ee441dffc1892f1ae59127f74afe8fd82fda6587794278d924e4d90ea3d63f9"}, "mint": {:hex, :mint, "1.6.2", "af6d97a4051eee4f05b5500671d47c3a67dac7386045d87a904126fd4bbcea2e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "5ee441dffc1892f1ae59127f74afe8fd82fda6587794278d924e4d90ea3d63f9"},
"mox": {:hex, :mox, "1.2.0", "a2cd96b4b80a3883e3100a221e8adc1b98e4c3a332a8fc434c39526babafd5b3", [:mix], [{:nimble_ownership, "~> 1.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}], "hexpm", "c7b92b3cc69ee24a7eeeaf944cd7be22013c52fcb580c1f33f50845ec821089a"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_ownership": {:hex, :nimble_ownership, "1.0.0", "3f87744d42c21b2042a0aa1d48c83c77e6dd9dd357e425a038dd4b49ba8b79a1", [:mix], [], "hexpm", "7c16cc74f4e952464220a73055b557a273e8b1b7ace8489ec9d86e9ad56cb2cc"}, "nimble_ownership": {:hex, :nimble_ownership, "1.0.0", "3f87744d42c21b2042a0aa1d48c83c77e6dd9dd357e425a038dd4b49ba8b79a1", [:mix], [], "hexpm", "7c16cc74f4e952464220a73055b557a273e8b1b7ace8489ec9d86e9ad56cb2cc"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},