Add context function to scrobble a release

This commit is contained in:
Claudio Ortolina
2025-05-07 09:04:03 +01:00
parent 9aecd4cc94
commit a94c349781
2 changed files with 36 additions and 0 deletions
+32
View File
@@ -1,6 +1,38 @@
defmodule MusicLibrary.ScrobbleActivity do
alias LastFm.Scrobble
alias MusicLibrary.{Artists, Collection, Wishlist}
def scrobble(release_with_tracks, started_time \\ DateTime.utc_now()) do
session_key = MusicLibrary.Secrets.get!("last_fm_session_key").value
{scrobbles, _finished_time} =
release_with_tracks
|> MusicBrainz.Release.tracks()
|> Enum.map_reduce(started_time, fn track, time ->
album_artist =
if release_with_tracks.artists !== track.artists do
main_artist_name(release_with_tracks.artists)
end
time = time |> DateTime.add(track.length, :millisecond)
scrobble = %Scrobble{
artist: main_artist_name(track.artists),
album: release_with_tracks.title,
album_artist: album_artist,
track: track.title,
timestamp: DateTime.to_unix(time)
}
{scrobble, time}
end)
LastFm.scrobble(scrobbles, session_key)
end
defp main_artist_name([]), do: nil
defp main_artist_name([artist | _rest]), do: artist.name
def from_recent_tracks(recent_tracks, timezone) do
all_artist_pairs = Artists.get_all_artist_pairs()
recent_release_ids = recent_release_ids(recent_tracks)