Add logic to backfill scrobbled_tracks
Chokes on duplicate primary keys
This commit is contained in:
+16
-3
@@ -1,5 +1,6 @@
|
|||||||
defmodule LastFm do
|
defmodule LastFm do
|
||||||
alias LastFm.{API, Feed, Refresh, Scrobble}
|
alias LastFm.{API, Feed, Refresh, Scrobble, Track, Worker}
|
||||||
|
alias MusicLibrary.{BackgroundRepo, Repo}
|
||||||
|
|
||||||
def get_scrobbled_tracks, do: Feed.all_tracks()
|
def get_scrobbled_tracks, do: Feed.all_tracks()
|
||||||
|
|
||||||
@@ -7,9 +8,21 @@ defmodule LastFm do
|
|||||||
|
|
||||||
def refresh_scrobbled_tracks, do: Refresh.refresh()
|
def refresh_scrobbled_tracks, do: Refresh.refresh()
|
||||||
|
|
||||||
def get_tracks(to_uts) do
|
def get_tracks(opts) do
|
||||||
last_fm_config = last_fm_config()
|
last_fm_config = last_fm_config()
|
||||||
API.get_recent_tracks(to_uts, last_fm_config)
|
API.get_recent_tracks(opts, last_fm_config)
|
||||||
|
end
|
||||||
|
|
||||||
|
def lowest_scrobbled_at_uts do
|
||||||
|
Repo.aggregate(Track, :min, :scrobbled_at_uts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def backfill_scrobbled_tracks do
|
||||||
|
to_uts = lowest_scrobbled_at_uts()
|
||||||
|
|
||||||
|
%{"to_uts" => to_uts}
|
||||||
|
|> Worker.BackfillScrobbledTracks.new()
|
||||||
|
|> BackgroundRepo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_artist_info(musicbrainz_id, name) do
|
def get_artist_info(musicbrainz_id, name) do
|
||||||
|
|||||||
+5
-2
@@ -51,11 +51,14 @@ defmodule LastFm.API do
|
|||||||
|> post_request()
|
|> post_request()
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_recent_tracks(to_uts \\ nil, config) do
|
def get_recent_tracks(opts \\ [], config) do
|
||||||
|
to_uts = Keyword.get(opts, :to_uts)
|
||||||
|
limit = Keyword.get(opts, :limit, 50)
|
||||||
|
|
||||||
params =
|
params =
|
||||||
config
|
config
|
||||||
|> base_params()
|
|> base_params()
|
||||||
|> Keyword.merge(method: "user.getrecenttracks", limit: 50)
|
|> Keyword.merge(method: "user.getrecenttracks", limit: limit)
|
||||||
|
|
||||||
params = if to_uts, do: Keyword.put(params, :to, to_uts), else: params
|
params = if to_uts, do: Keyword.put(params, :to, to_uts), else: params
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ defmodule LastFm.Import do
|
|||||||
:last_fm_data
|
:last_fm_data
|
||||||
]
|
]
|
||||||
|
|
||||||
def batch(uts_to) do
|
def batch(opts) do
|
||||||
with {:ok, tracks} <- LastFm.get_tracks(uts_to) do
|
with {:ok, tracks} <- LastFm.get_tracks(opts) do
|
||||||
track_params =
|
track_params =
|
||||||
tracks
|
tracks
|
||||||
|> Enum.map(fn t -> Map.take(t, @insertable_fields) end)
|
|> Enum.map(fn t -> Map.take(t, @insertable_fields) end)
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
defmodule LastFm.Worker.BackfillScrobbledTracks do
|
||||||
|
use Oban.Worker, queue: :heavy_writes, max_attempts: 3
|
||||||
|
|
||||||
|
alias MusicLibrary.BackgroundRepo
|
||||||
|
|
||||||
|
@backfill_delay 5
|
||||||
|
@batch_size 100
|
||||||
|
|
||||||
|
@impl Oban.Worker
|
||||||
|
def perform(%{args: %{"to_uts" => to_uts}}) do
|
||||||
|
# importing is an all or nothing operation, which means that we can
|
||||||
|
# use the returning count to determine if we reached the end of the backfilling
|
||||||
|
# process.
|
||||||
|
case LastFm.Import.batch(to_uts: to_uts, limit: @batch_size) do
|
||||||
|
{@batch_size, nil} ->
|
||||||
|
next_to_uts = LastFm.lowest_scrobbled_at_uts()
|
||||||
|
|
||||||
|
%{"to_uts" => next_to_uts}
|
||||||
|
|> new(schedule_in: @backfill_delay)
|
||||||
|
|> BackgroundRepo.insert()
|
||||||
|
|
||||||
|
{other_count, nil} when is_integer(other_count) ->
|
||||||
|
:ok
|
||||||
|
|
||||||
|
error ->
|
||||||
|
error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user