Add logic to import a batch of Last.fm tracks

This commit is contained in:
Claudio Ortolina
2025-05-31 16:16:24 +01:00
parent d01d16f1ae
commit a27b38f192
3 changed files with 31 additions and 1 deletions
+3 -1
View File
@@ -51,12 +51,14 @@ defmodule LastFm.API do
|> post_request()
end
def get_recent_tracks(config) do
def get_recent_tracks(to_uts \\ nil, config) do
params =
config
|> base_params()
|> Keyword.merge(method: "user.getrecenttracks", limit: 50)
params = if to_uts, do: Keyword.put(params, :to, to_uts), else: params
config
|> new_request()
|> Req.merge(url: "/", params: params)
+23
View File
@@ -0,0 +1,23 @@
defmodule LastFm.Import do
@insertable_fields [
:musicbrainz_id,
:title,
:artist,
:album,
:cover_url,
:scrobbled_at_uts,
:scrobbled_at_label,
:last_fm_data
]
def batch(uts_to) do
with {:ok, tracks} <- LastFm.get_tracks(uts_to) do
track_params =
tracks
|> Enum.map(fn t -> Map.take(t, @insertable_fields) end)
|> Enum.map(&Map.to_list/1)
MusicLibrary.Repo.insert_all(LastFm.Track, track_params)
end
end
end