Files
music_library/lib/last_fm/import.ex
T
Claudio Ortolina b191d87dfd Fix scrobble tracks schema
Remove the primary key, and just set a unique index on scrobbled time
and title, which allows skipping duplicates.
2025-05-31 20:54:37 +01:00

29 lines
685 B
Elixir

defmodule LastFm.Import do
@insertable_fields [
:musicbrainz_id,
:title,
:artist,
:album,
:cover_url,
:scrobbled_at_uts,
:scrobbled_at_label,
:last_fm_data
]
def batch(opts) do
with {:ok, tracks} <- LastFm.get_tracks(opts) do
track_params =
tracks
|> Enum.map(fn t -> Map.take(t, @insertable_fields) end)
|> Enum.map(&Map.to_list/1)
# HACK: if two tracks happen to have the exact same scrobbled_at_uts,
# we move it by a sec.
MusicLibrary.Repo.insert_all(LastFm.Track, track_params,
on_conflict: :nothing,
conflict_target: [:scrobbled_at_uts, :title]
)
end
end
end