neiam
/final_track
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
bin/repair-artists: move plays recorded under 'Artist • Album • YouTube Music' onto the real artist
5051b6e · gmorell · 2026-11-19 04:51
Files changed
modified
README.md
+1
−0
@@ -140,6 +140,7 @@ as repo secrets; GHCR uses the workflow's own token. The release
| 140 | 140 | | `invite [--email E] [--note N] [--days D] [--by E]` | print a single-use registration link | |
| 141 | 141 | | `invite-link EMAIL` | a fresh link for an invite addressed to EMAIL | |
| 142 | 142 | | `admin [--revoke] EMAIL` | make an account an admin (or not) | |
| 143 | +| `repair-artists [--apply]` | move plays recorded under a player's subtitle as the artist ("Artist • Album • YouTube Music", before that was untangled on arrival) onto the real artist and album; a dry run without `--apply` | | |
| 143 | 144 | | `final_track rpc 'FinalTrack.Covers.retry_misses()'` | look up covers again for albums that never matched | |
| 144 | 145 | |
| 145 | 146 | The About page has copy-ready Podman/Docker commands for a single box. |
added
lib/final_track/catalog/repair.ex
+181
−0
@@ -0,0 +1,181 @@
| 1 | +defmodule FinalTrack.Catalog.Repair do | |
| 2 | + @moduledoc """ | |
| 3 | + Moves plays recorded under a tangled artist name — a player's subtitle | |
| 4 | + such as "Cage The Elephant • Tell Me I'm Pretty • YouTube Music", from | |
| 5 | + before `FinalTrack.Scrobbles.validate_play/1` untangled them — onto the | |
| 6 | + artist, album and track they should have had, then deletes the tangled | |
| 7 | + entries. From a release (`bin/repair-artists`, a dry run unless | |
| 8 | + `--apply`). | |
| 9 | + | |
| 10 | + Per tangled artist, in one transaction: | |
| 11 | + | |
| 12 | + * each of its tracks maps to the same title under the real artist; its | |
| 13 | + plays move there (a play the real track already has at the same | |
| 14 | + moment, for the same user, is a duplicate and is deleted); | |
| 15 | + * each of its albums maps to the same title under the real artist, | |
| 16 | + bringing its cover if the real album has none; plays that had no | |
| 17 | + album get the one named in the subtitle; | |
| 18 | + * private marks follow (or are dropped as duplicates); now-playing rows | |
| 19 | + on it are cleared; | |
| 20 | + * the tangled artist is deleted, with its now-empty albums and tracks. | |
| 21 | + | |
| 22 | + Fediverse posts already delivered keep their old text. | |
| 23 | + """ | |
| 24 | + import Ecto.Query | |
| 25 | + | |
| 26 | + alias FinalTrack.Catalog | |
| 27 | + alias FinalTrack.Catalog.{Album, Artist, Track} | |
| 28 | + alias FinalTrack.Repo | |
| 29 | + alias FinalTrack.Scrobbles | |
| 30 | + alias FinalTrack.Scrobbles.{NowPlaying, PrivateMark, Scrobble} | |
| 31 | + | |
| 32 | + @doc """ | |
| 33 | + The tangled artists and what each would become: `[{artist, name, album}]`. | |
| 34 | + """ | |
| 35 | + def tangled do | |
| 36 | + from(a in Artist, where: like(a.name, "%•%") or like(a.name, "%·%"), order_by: a.name) | |
| 37 | + |> Repo.all() | |
| 38 | + |> Enum.flat_map(fn artist -> | |
| 39 | + case Scrobbles.untangle_artist(artist.name, nil) do | |
| 40 | + {name, _} when name == artist.name -> [] | |
| 41 | + {name, album} -> [{artist, name, album}] | |
| 42 | + end | |
| 43 | + end) | |
| 44 | + end | |
| 45 | + | |
| 46 | + @doc """ | |
| 47 | + Reports, and with `apply: true` repairs, every tangled artist. Returns a | |
| 48 | + list of lines describing what happened (or would). | |
| 49 | + """ | |
| 50 | + def run(opts \\ []) do | |
| 51 | + apply? = Keyword.get(opts, :apply, false) | |
| 52 | + | |
| 53 | + case tangled() do | |
| 54 | + [] -> | |
| 55 | + ["nothing to repair"] | |
| 56 | + | |
| 57 | + found -> | |
| 58 | + Enum.flat_map(found, fn {artist, name, album} -> | |
| 59 | + plays = Repo.aggregate(from(s in Scrobble, where: s.artist_id == ^artist.id), :count) | |
| 60 | + | |
| 61 | + head = | |
| 62 | + "#{inspect(artist.name)} → #{inspect(name)}#{album && ", album #{inspect(album)}"} (#{plays} plays)" | |
| 63 | + | |
| 64 | + if apply? do | |
| 65 | + {:ok, stats} = Repo.transaction(fn -> repair(artist, name, album) end) | |
| 66 | + [head <> ": moved #{stats.moved}, dropped #{stats.dropped} duplicates"] | |
| 67 | + else | |
| 68 | + [head] | |
| 69 | + end | |
| 70 | + end) | |
| 71 | + end | |
| 72 | + end | |
| 73 | + | |
| 74 | + defp repair(%Artist{} = bad, name, subtitle_album) do | |
| 75 | + target = Catalog.ensure_artist!(name) | |
| 76 | + | |
| 77 | + albums = | |
| 78 | + from(a in Album, where: a.artist_id == ^bad.id) | |
| 79 | + |> Repo.all() | |
| 80 | + |> Map.new(fn album -> {album.id, move_album(album, target)} end) | |
| 81 | + | |
| 82 | + subtitle_album = subtitle_album && Catalog.ensure_album!(target, subtitle_album) | |
| 83 | + | |
| 84 | + tracks = | |
| 85 | + from(t in Track, where: t.artist_id == ^bad.id) | |
| 86 | + |> Repo.all() | |
| 87 | + |> Map.new(fn t -> {t.id, Catalog.ensure_track!(target, t.title, t.duration_sec)} end) | |
| 88 | + | |
| 89 | + # Plays of the tangled tracks, and plays of other artists' tracks filed | |
| 90 | + # under one of the tangled albums (as album artist). | |
| 91 | + plays = | |
| 92 | + from(s in Scrobble, | |
| 93 | + where: s.artist_id == ^bad.id or s.album_id in ^Map.keys(albums) | |
| 94 | + ) | |
| 95 | + |> Repo.all() | |
| 96 | + | |
| 97 | + stats = | |
| 98 | + Enum.reduce(plays, %{moved: 0, dropped: 0}, fn play, stats -> | |
| 99 | + track = Map.get(tracks, play.track_id) | |
| 100 | + | |
| 101 | + album_id = | |
| 102 | + cond do | |
| 103 | + play.album_id && Map.has_key?(albums, play.album_id) -> albums[play.album_id].id | |
| 104 | + is_nil(play.album_id) && track && subtitle_album -> subtitle_album.id | |
| 105 | + true -> play.album_id | |
| 106 | + end | |
| 107 | + | |
| 108 | + changes = | |
| 109 | + if track, | |
| 110 | + do: [track_id: track.id, artist_id: target.id, album_id: album_id], | |
| 111 | + else: [album_id: album_id] | |
| 112 | + | |
| 113 | + duplicate? = | |
| 114 | + track && | |
| 115 | + Repo.exists?( | |
| 116 | + from(s in Scrobble, | |
| 117 | + where: | |
| 118 | + s.user_id == ^play.user_id and s.track_id == ^track.id and | |
| 119 | + s.played_at == ^play.played_at and s.id != ^play.id | |
| 120 | + ) | |
| 121 | + ) | |
| 122 | + | |
| 123 | + if duplicate? do | |
| 124 | + Repo.delete!(play) | |
| 125 | + %{stats | dropped: stats.dropped + 1} | |
| 126 | + else | |
| 127 | + play |> Ecto.Changeset.change(changes) |> Repo.update!() | |
| 128 | + %{stats | moved: stats.moved + 1} | |
| 129 | + end | |
| 130 | + end) | |
| 131 | + | |
| 132 | + move_marks(:artist_id, %{bad.id => target}) | |
| 133 | + move_marks(:album_id, albums) | |
| 134 | + move_marks(:track_id, tracks) | |
| 135 | + | |
| 136 | + from(n in NowPlaying, | |
| 137 | + where: n.track_id in ^Map.keys(tracks) or n.album_id in ^Map.keys(albums) | |
| 138 | + ) | |
| 139 | + |> Repo.delete_all() | |
| 140 | + | |
| 141 | + Repo.delete!(bad) | |
| 142 | + stats | |
| 143 | + end | |
| 144 | + | |
| 145 | + # The same title under the real artist, with the tangled one's cover and | |
| 146 | + # MusicBrainz id when it has none of its own. | |
| 147 | + defp move_album(%Album{} = album, %Artist{} = target) do | |
| 148 | + real = Catalog.ensure_album!(target, album.title) | |
| 149 | + | |
| 150 | + if is_nil(real.cover_key) and album.cover_key do | |
| 151 | + real | |
| 152 | + |> Ecto.Changeset.change( | |
| 153 | + cover_key: album.cover_key, | |
| 154 | + cover_source: album.cover_source, | |
| 155 | + mbid: real.mbid || album.mbid, | |
| 156 | + cover_checked_at: album.cover_checked_at | |
| 157 | + ) | |
| 158 | + |> Repo.update!() | |
| 159 | + else | |
| 160 | + real | |
| 161 | + end | |
| 162 | + end | |
| 163 | + | |
| 164 | + defp move_marks(field, mapping) do | |
| 165 | + for {old_id, new} <- mapping, | |
| 166 | + mark <- Repo.all(from(m in PrivateMark, where: field(m, ^field) == ^old_id)) do | |
| 167 | + exists? = | |
| 168 | + Repo.exists?( | |
| 169 | + from(m in PrivateMark, | |
| 170 | + where: m.user_id == ^mark.user_id and field(m, ^field) == ^new.id | |
| 171 | + ) | |
| 172 | + ) | |
| 173 | + | |
| 174 | + if exists?, | |
| 175 | + do: Repo.delete!(mark), | |
| 176 | + else: mark |> Ecto.Changeset.change([{field, new.id}]) |> Repo.update!() | |
| 177 | + end | |
| 178 | + | |
| 179 | + :ok | |
| 180 | + end | |
| 181 | +end |
modified
lib/final_track/release.ex
+20
−0
@@ -5,6 +5,8 @@ defmodule FinalTrack.Release do
| 5 | 5 | bin/migrate # FinalTrack.Release.migrate/0 |
| 6 | 6 | bin/invite [--email sam@example.com] [--note "for Sam"] [--days 7] [--by you@example.com] |
| 7 | 7 | bin/invite-link sam@example.com |
| 8 | + bin/admin [--revoke] you@example.com | |
| 9 | + bin/repair-artists [--apply] # FinalTrack.Catalog.Repair | |
| 8 | 10 | bin/final_track eval 'FinalTrack.Release.rollback(FinalTrack.Repo, 20261118195445)' |
| 9 | 11 | |
| 10 | 12 | In dev, the same invites are `mix final_track.invite` and |
@@ -132,6 +134,24 @@ defmodule FinalTrack.Release do
| 132 | 134 | end |
| 133 | 135 | end |
| 134 | 136 | |
| 137 | + @doc """ | |
| 138 | + Lists artists recorded under a player's subtitle ("Artist • Album • | |
| 139 | + YouTube Music") and, with REPAIR_APPLY=true, moves their plays onto the | |
| 140 | + real artist (`FinalTrack.Catalog.Repair`). | |
| 141 | + """ | |
| 142 | + def repair_artists(apply? \\ nil) do | |
| 143 | + load_app() | |
| 144 | + apply? = if is_nil(apply?), do: env("REPAIR_APPLY") == "true", else: apply? | |
| 145 | + | |
| 146 | + {:ok, lines, _apps} = | |
| 147 | + Ecto.Migrator.with_repo(FinalTrack.Repo, fn _repo -> | |
| 148 | + FinalTrack.Catalog.Repair.run(apply: apply?) | |
| 149 | + end) | |
| 150 | + | |
| 151 | + Enum.each(lines, &IO.puts/1) | |
| 152 | + unless apply?, do: IO.puts("(dry run: --apply to repair)") | |
| 153 | + end | |
| 154 | + | |
| 135 | 155 | defp env(name) do |
| 136 | 156 | case System.get_env(name) do |
| 137 | 157 | nil -> nil |
modified
lib/final_track/scrobbles.ex
+2
−1
@@ -121,7 +121,8 @@ defmodule FinalTrack.Scrobbles do
| 121 | 121 | # YouTube Music". When the last "•" segment is a known service, drop it; |
| 122 | 122 | # what's left is the artist, then the album (kept unless one was sent). |
| 123 | 123 | @services ~w(youtubemusic) |
| 124 | − defp untangle_artist(artist, album) do | |
| 124 | + @doc false | |
| 125 | + def untangle_artist(artist, album) do | |
| 125 | 126 | parts = artist |> String.split(~r/\s+[•·]\s+/u) |> Enum.map(&String.trim/1) |
| 126 | 127 | |
| 127 | 128 | with [_, _ | _] <- parts, |
added
rel/overlays/bin/repair-artists
+17
−0
@@ -0,0 +1,17 @@
| 1 | +#!/bin/sh | |
| 2 | +# Move plays recorded under a player's subtitle as the artist | |
| 3 | +# ("Cage The Elephant • Tell Me I'm Pretty • YouTube Music") onto the real | |
| 4 | +# artist and album. A dry run unless --apply. | |
| 5 | +# bin/repair-artists | |
| 6 | +# bin/repair-artists --apply | |
| 7 | +set -eu | |
| 8 | + | |
| 9 | +export REPAIR_APPLY=false | |
| 10 | +case "${1:-}" in | |
| 11 | + --apply) export REPAIR_APPLY=true ;; | |
| 12 | + "") ;; | |
| 13 | + *) echo "usage: $0 [--apply]" >&2; exit 2 ;; | |
| 14 | +esac | |
| 15 | + | |
| 16 | +cd -P -- "$(dirname -- "$0")" | |
| 17 | +exec ./final_track eval FinalTrack.Release.repair_artists |
added
rel/overlays/bin/repair-artists.bat
+1
−0
@@ -0,0 +1 @@
| 1 | +call "%~dp0\final_track" eval FinalTrack.Release.repair_artists |
added
test/final_track/catalog/repair_test.exs
+71
−0
@@ -0,0 +1,71 @@
| 1 | +defmodule FinalTrack.Catalog.RepairTest do | |
| 2 | + use FinalTrack.DataCase, async: true | |
| 3 | + | |
| 4 | + import FinalTrack.AccountsFixtures | |
| 5 | + | |
| 6 | + alias FinalTrack.Catalog.{Album, Artist, Repair, Track} | |
| 7 | + alias FinalTrack.{Repo, Scrobbles} | |
| 8 | + alias FinalTrack.Scrobbles.{PrivateMark, Scrobble} | |
| 9 | + | |
| 10 | + @tangled "Cage The Elephant • Tell Me I'm Pretty • YouTube Music" | |
| 11 | + | |
| 12 | + # Plays as they were recorded before validate_play untangled the name. | |
| 13 | + defp play(artist, track, at, album \\ nil) do | |
| 14 | + %{ | |
| 15 | + artist: artist, | |
| 16 | + track: track, | |
| 17 | + album: album, | |
| 18 | + album_artist: nil, | |
| 19 | + duration_sec: 200, | |
| 20 | + played_at: DateTime.from_unix!(at) | |
| 21 | + } | |
| 22 | + end | |
| 23 | + | |
| 24 | + test "moves plays, albums and marks onto the real artist and deletes the tangled one" do | |
| 25 | + user = user_fixture() | |
| 26 | + other = user_fixture() | |
| 27 | + | |
| 28 | + Scrobbles.record_plays(user, [ | |
| 29 | + play(@tangled, "Cry Baby", 1_000), | |
| 30 | + play(@tangled, "Mess Around", 2_000), | |
| 31 | + # Already recorded correctly at the same moment: a duplicate. | |
| 32 | + play("Cage The Elephant", "Mess Around", 2_000, "Tell Me I'm Pretty") | |
| 33 | + ]) | |
| 34 | + | |
| 35 | + Scrobbles.record_plays(other, [play(@tangled, "Cry Baby", 3_000, "Sent Album")]) | |
| 36 | + | |
| 37 | + bad = Repo.get_by!(Artist, name: @tangled) | |
| 38 | + bad_track = Repo.get_by!(Track, artist_id: bad.id, title: "Cry Baby") | |
| 39 | + Repo.insert!(%PrivateMark{user_id: user.id, track_id: bad_track.id}) | |
| 40 | + | |
| 41 | + assert [ | |
| 42 | + "\"Cage The Elephant • Tell Me I'm Pretty • YouTube Music\" → \"Cage The Elephant\"" <> | |
| 43 | + _ | |
| 44 | + ] = | |
| 45 | + Repair.run() | |
| 46 | + | |
| 47 | + assert Repo.get(Artist, bad.id), "a dry run changes nothing" | |
| 48 | + | |
| 49 | + assert [line] = Repair.run(apply: true) | |
| 50 | + assert line =~ "moved 2, dropped 1" | |
| 51 | + | |
| 52 | + refute Repo.get(Artist, bad.id) | |
| 53 | + real = Repo.get_by!(Artist, name: "Cage The Elephant") | |
| 54 | + album = Repo.get_by!(Album, artist_id: real.id, title: "Tell Me I'm Pretty") | |
| 55 | + | |
| 56 | + plays = Repo.all(from s in Scrobble, preload: [:track, :album]) | |
| 57 | + assert length(plays) == 3 | |
| 58 | + assert Enum.all?(plays, &(&1.artist_id == real.id)) | |
| 59 | + | |
| 60 | + by_user = Enum.filter(plays, &(&1.user_id == user.id)) | |
| 61 | + assert Enum.all?(by_user, &(&1.album_id == album.id)) | |
| 62 | + # A play that named its own album keeps it (moved under the real artist). | |
| 63 | + [theirs] = Enum.filter(plays, &(&1.user_id == other.id)) | |
| 64 | + assert theirs.album.title == "Sent Album" and theirs.album.artist_id == real.id | |
| 65 | + | |
| 66 | + real_track = Repo.get_by!(Track, artist_id: real.id, title: "Cry Baby") | |
| 67 | + assert Repo.get_by!(PrivateMark, user_id: user.id).track_id == real_track.id | |
| 68 | + | |
| 69 | + assert Repair.run(apply: true) == ["nothing to repair"] | |
| 70 | + end | |
| 71 | +end |
Parents: 2355fcd