Support searching for artist images
This commit is contained in:
+2
-2
@@ -124,7 +124,7 @@ Last.fm schemas (separate, not Ecto-persisted to main DB):
|
||||
| `LastFm` / `LastFm.API` | last.fm | Scrobbling, listening history, artist info |
|
||||
| `Discogs` / `Discogs.API` | discogs.com | Artist profiles, images |
|
||||
| `Wikipedia` / `Wikipedia.API` | wikipedia.org | Artist biographies |
|
||||
| `BraveSearch` / `BraveSearch.API` | search.brave.com | Cover art image search |
|
||||
| `BraveSearch` / `BraveSearch.API` | search.brave.com | Cover art and artist image search |
|
||||
| `OpenAI` / `OpenAI.API` | api.openai.com | Text embeddings for similarity |
|
||||
|
||||
Each has a `Config` module reading from application env. In tests, all HTTP calls are
|
||||
@@ -215,7 +215,7 @@ All authenticated routes live inside a single `live_session` with three `on_moun
|
||||
| Component | Used In | Purpose |
|
||||
|-----------|---------|---------|
|
||||
| `RecordForm` | Collection/Wishlist (edit) | Record editing: cover search, genre autocomplete, color picker, file upload |
|
||||
| `ArtistLive.Form` | ArtistLive.Show | Edit artist image/bio |
|
||||
| `ArtistLive.Form` | ArtistLive.Show | Edit artist image (upload + Brave image search) |
|
||||
| `RecordSetLive.Form` | RecordSetLive.Index | Create/edit record set |
|
||||
| `RecordSetLive.RecordPicker` | RecordSetLive.Show | Search and add records to set |
|
||||
| `ScrobbledTracksLive.Form` | ScrobbledTracksLive.Index | Edit scrobbled track |
|
||||
|
||||
@@ -5,6 +5,7 @@ defmodule MusicLibraryWeb.ArtistLive.Form do
|
||||
|
||||
alias MusicLibrary.Artists
|
||||
alias MusicLibrary.Assets
|
||||
alias MusicLibrary.Assets.Image
|
||||
|
||||
@impl true
|
||||
def mount(socket) do
|
||||
@@ -80,6 +81,86 @@ defmodule MusicLibraryWeb.ArtistLive.Form do
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-span-full space-y-4">
|
||||
<.label>{gettext("Search for artist image online")}</.label>
|
||||
<div class="flex gap-2">
|
||||
<div class="flex-1">
|
||||
<.input
|
||||
type="text"
|
||||
id="image-search-query"
|
||||
name="image_search_query"
|
||||
value={@image_search_query}
|
||||
phx-keyup="update_image_search_query"
|
||||
phx-target={@myself}
|
||||
/>
|
||||
</div>
|
||||
<.button
|
||||
type="button"
|
||||
size="sm"
|
||||
id="image-search-button"
|
||||
phx-click="search_images"
|
||||
phx-target={@myself}
|
||||
disabled={@image_search_loading}
|
||||
>
|
||||
<.icon
|
||||
:if={@image_search_loading}
|
||||
name="hero-arrow-path"
|
||||
class="icon animate-spin"
|
||||
aria-hidden="true"
|
||||
data-slot="icon"
|
||||
/>
|
||||
<.icon
|
||||
:if={!@image_search_loading}
|
||||
name="hero-magnifying-glass"
|
||||
class="icon"
|
||||
aria-hidden="true"
|
||||
data-slot="icon"
|
||||
/>
|
||||
{gettext("Search")}
|
||||
</.button>
|
||||
</div>
|
||||
<p :if={@image_search_error} class="text-sm text-red-600 dark:text-red-400">
|
||||
{@image_search_error}
|
||||
</p>
|
||||
<div
|
||||
:if={@image_search_results != []}
|
||||
id="image-search-results"
|
||||
class="grid grid-cols-3 sm:grid-cols-4 gap-2"
|
||||
>
|
||||
<button
|
||||
:for={result <- @image_search_results}
|
||||
type="button"
|
||||
phx-click="select_image"
|
||||
phx-value-url={result.image_url}
|
||||
phx-target={@myself}
|
||||
disabled={@image_search_loading}
|
||||
class={[
|
||||
"group relative overflow-hidden rounded-md",
|
||||
"border border-zinc-200 dark:border-zinc-700",
|
||||
"hover:ring-2 hover:ring-indigo-500",
|
||||
"focus:outline-none focus:ring-2 focus:ring-indigo-500",
|
||||
"disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
]}
|
||||
>
|
||||
<img
|
||||
src={result.thumbnail_url}
|
||||
alt={result.title}
|
||||
class="aspect-square w-full object-cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
<span
|
||||
:if={result.width && result.height}
|
||||
class={[
|
||||
"absolute bottom-0 inset-x-0",
|
||||
"bg-black/60 text-white text-xs text-center",
|
||||
"py-0.5"
|
||||
]}
|
||||
>
|
||||
{result.width}×{result.height}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<:actions>
|
||||
<div class="w-full md:flex md:justify-center">
|
||||
<.button variant="solid" class="w-full md:w-auto" phx-disable-with={gettext("Saving...")}>
|
||||
@@ -99,7 +180,11 @@ defmodule MusicLibraryWeb.ArtistLive.Form do
|
||||
|> assign(assigns)
|
||||
|> assign_new(:form, fn ->
|
||||
to_form(Artists.change_artist_info(artist_info))
|
||||
end)}
|
||||
end)
|
||||
|> assign_new(:image_search_query, fn -> "#{assigns.artist.name} artist" end)
|
||||
|> assign_new(:image_search_results, fn -> [] end)
|
||||
|> assign_new(:image_search_loading, fn -> false end)
|
||||
|> assign_new(:image_search_error, fn -> nil end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@@ -125,6 +210,89 @@ defmodule MusicLibraryWeb.ArtistLive.Form do
|
||||
handle_event("validate", params, socket)
|
||||
end
|
||||
|
||||
def handle_event("update_image_search_query", %{"value" => query}, socket) do
|
||||
{:noreply, assign(socket, :image_search_query, query)}
|
||||
end
|
||||
|
||||
def handle_event("search_images", _params, socket) do
|
||||
query = socket.assigns.image_search_query
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_loading, true)
|
||||
|> assign(:image_search_error, nil)
|
||||
|> start_async(:image_search, fn -> BraveSearch.search_images(query, count: 20) end)}
|
||||
end
|
||||
|
||||
def handle_event("select_image", %{"url" => url}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_loading, true)
|
||||
|> assign(:image_search_error, nil)
|
||||
|> start_async(:image_download, fn ->
|
||||
with {:ok, data} <- BraveSearch.download_image(url),
|
||||
{:ok, resized} <- Image.resize(data),
|
||||
{:ok, asset} <- Assets.store_image(%{content: resized, format: "image/jpeg"}) do
|
||||
{:ok, asset.hash}
|
||||
end
|
||||
end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_async(:image_search, {:ok, {:ok, results}}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_results, results)
|
||||
|> assign(:image_search_loading, false)}
|
||||
end
|
||||
|
||||
def handle_async(:image_search, {:ok, {:error, reason}}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_error, "Search failed: #{inspect(reason)}")
|
||||
|> assign(:image_search_loading, false)}
|
||||
end
|
||||
|
||||
def handle_async(:image_search, {:exit, reason}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_error, "Search failed: #{inspect(reason)}")
|
||||
|> assign(:image_search_loading, false)}
|
||||
end
|
||||
|
||||
def handle_async(:image_download, {:ok, {:ok, image_hash}}, socket) do
|
||||
case Artists.update_artist_info(socket.assigns.artist_info, %{"image_data_hash" => image_hash}) do
|
||||
{:ok, artist_info} ->
|
||||
notify_parent({:saved, artist_info})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_loading, false)
|
||||
|> put_toast(:info, gettext("Artist image updated successfully"))
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, _changeset} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_error, gettext("Failed to save artist image"))
|
||||
|> assign(:image_search_loading, false)}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_async(:image_download, {:ok, {:error, reason}}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_error, "Download failed: #{inspect(reason)}")
|
||||
|> assign(:image_search_loading, false)}
|
||||
end
|
||||
|
||||
def handle_async(:image_download, {:exit, reason}, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:image_search_error, "Download failed: #{inspect(reason)}")
|
||||
|> assign(:image_search_loading, false)}
|
||||
end
|
||||
|
||||
defp save_artist_info(socket, artist_info_params, uploaded_images) do
|
||||
params =
|
||||
case uploaded_images do
|
||||
|
||||
@@ -192,6 +192,7 @@ msgstr ""
|
||||
|
||||
#: lib/music_library_web/components/core_components.ex
|
||||
#: lib/music_library_web/components/record_form.ex
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#: lib/music_library_web/live/record_set_live/record_picker.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Search"
|
||||
@@ -1893,3 +1894,18 @@ msgstr ""
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Search for cover art online"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Artist image updated successfully"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Failed to save artist image"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Search for artist image online"
|
||||
msgstr ""
|
||||
|
||||
@@ -192,6 +192,7 @@ msgstr ""
|
||||
|
||||
#: lib/music_library_web/components/core_components.ex
|
||||
#: lib/music_library_web/components/record_form.ex
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#: lib/music_library_web/live/record_set_live/record_picker.ex
|
||||
#, elixir-autogen, elixir-format
|
||||
msgid "Search"
|
||||
@@ -1893,3 +1894,18 @@ msgstr ""
|
||||
#, elixir-autogen, elixir-format, fuzzy
|
||||
msgid "Search for cover art online"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#, elixir-autogen, elixir-format, fuzzy
|
||||
msgid "Artist image updated successfully"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#, elixir-autogen, elixir-format, fuzzy
|
||||
msgid "Failed to save artist image"
|
||||
msgstr ""
|
||||
|
||||
#: lib/music_library_web/live/artist_live/form.ex
|
||||
#, elixir-autogen, elixir-format, fuzzy
|
||||
msgid "Search for artist image online"
|
||||
msgstr ""
|
||||
|
||||
Reference in New Issue
Block a user