Add logic to import a batch of Last.fm tracks
This commit is contained in:
@@ -7,6 +7,11 @@ defmodule LastFm do
|
||||
|
||||
def refresh_scrobbled_tracks, do: Refresh.refresh()
|
||||
|
||||
def get_tracks(to_uts) do
|
||||
last_fm_config = last_fm_config()
|
||||
API.get_recent_tracks(to_uts, last_fm_config)
|
||||
end
|
||||
|
||||
def get_artist_info(musicbrainz_id, name) do
|
||||
last_fm_config = last_fm_config()
|
||||
|
||||
|
||||
+3
-1
@@ -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)
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user