From 90929bd6584ad4d884b82f4fac0480c2df039105 Mon Sep 17 00:00:00 2001 From: Jacob Ewald Date: Tue, 5 Nov 2024 10:21:40 -0700 Subject: [PATCH] When scrobbling, use the time the track started rather than when it finished --- Jellyfin.Plugin.Lastfm/Api/LastfmApiClient.cs | 4 ++-- .../Models/Requests/ScrobbleRequest.cs | 2 +- Jellyfin.Plugin.Lastfm/ServerEntryPoint.cs | 8 +++++++- Jellyfin.Plugin.Lastfm/Utils/Helpers.cs | 17 ----------------- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/Jellyfin.Plugin.Lastfm/Api/LastfmApiClient.cs b/Jellyfin.Plugin.Lastfm/Api/LastfmApiClient.cs index 1d9bc9f..fedb361 100644 --- a/Jellyfin.Plugin.Lastfm/Api/LastfmApiClient.cs +++ b/Jellyfin.Plugin.Lastfm/Api/LastfmApiClient.cs @@ -44,14 +44,14 @@ public async Task RequestSession(string username, string return response; } - public async Task Scrobble(Audio item, LastfmUser user) + public async Task Scrobble(Audio item, LastfmUser user, long timestamp) { // API docs -> https://www.last.fm/api/show/track.scrobble var request = new ScrobbleRequest { Track = item.Name, Artist = item.Artists.First(), - Timestamp = Helpers.CurrentTimestamp(), + Timestamp = timestamp, ApiKey = Strings.Keys.LastfmApiKey, Method = Strings.Methods.Scrobble, diff --git a/Jellyfin.Plugin.Lastfm/Models/Requests/ScrobbleRequest.cs b/Jellyfin.Plugin.Lastfm/Models/Requests/ScrobbleRequest.cs index 9ddd0d3..63d06f6 100644 --- a/Jellyfin.Plugin.Lastfm/Models/Requests/ScrobbleRequest.cs +++ b/Jellyfin.Plugin.Lastfm/Models/Requests/ScrobbleRequest.cs @@ -11,7 +11,7 @@ public class ScrobbleRequest : BaseAuthedRequest // Album, ArtistAlbum, and MusicBrainzid are optional. public string Track { get; set; } public string Artist { get; set; } - public int Timestamp { get; set; } + public long Timestamp { get; set; } public string Album { get; set; } public string AlbumArtist { get; set; } public string MbId { get; set; } diff --git a/Jellyfin.Plugin.Lastfm/ServerEntryPoint.cs b/Jellyfin.Plugin.Lastfm/ServerEntryPoint.cs index c471cde..f1ea41e 100644 --- a/Jellyfin.Plugin.Lastfm/ServerEntryPoint.cs +++ b/Jellyfin.Plugin.Lastfm/ServerEntryPoint.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Hosting; using System.Threading; + using Jellyfin.Plugin.Lastfm.Utils; /// /// Class ServerEntryPoint @@ -31,6 +32,7 @@ public class ServerEntryPoint : IHostedService, IDisposable private LastfmApiClient _apiClient; private readonly ILogger _logger; + private long _playbackTimestamp; /// /// Gets the instance. @@ -156,7 +158,8 @@ private async void PlaybackStopped(object sender, PlaybackStopEventArgs e) _logger.LogInformation("track {0} is missing artist ({1}) or track name ({2}) metadata. Not submitting", item.Path, item.Artists.FirstOrDefault(), item.Name); return; } - await _apiClient.Scrobble(item, lastfmUser).ConfigureAwait(false); + + await _apiClient.Scrobble(item, lastfmUser, _playbackTimestamp).ConfigureAwait(false); } /// @@ -200,6 +203,9 @@ private async void PlaybackStart(object sender, PlaybackProgressEventArgs e) _logger.LogInformation("track {0} is missing artist ({1}) or track name ({2}) metadata. Not submitting", item.Path, item.Artists.FirstOrDefault(), item.Name); return; } + + _playbackTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + await _apiClient.NowPlaying(item, lastfmUser).ConfigureAwait(false); } diff --git a/Jellyfin.Plugin.Lastfm/Utils/Helpers.cs b/Jellyfin.Plugin.Lastfm/Utils/Helpers.cs index 87c161e..d442e98 100644 --- a/Jellyfin.Plugin.Lastfm/Utils/Helpers.cs +++ b/Jellyfin.Plugin.Lastfm/Utils/Helpers.cs @@ -33,23 +33,6 @@ public static void AppendSignature(ref Dictionary data) data.Add("api_sig", CreateSignature(data)); } - public static int ToTimestamp(DateTime date) - { - return Convert.ToInt32((date - new DateTime(1970, 1, 1)).TotalSeconds); - } - - public static DateTime FromTimestamp(double timestamp) - { - var date = new DateTime(1970, 1, 1, 0, 0, 0, 0); - - return date.AddSeconds(timestamp).ToLocalTime(); - } - - public static int CurrentTimestamp() - { - return ToTimestamp(DateTime.UtcNow); - } - public static string DictionaryToQueryString(Dictionary data) { return String.Join("&", data.Where(k => !String.IsNullOrWhiteSpace(k.Value)).Select(kvp => String.Format("{0}={1}", Uri.EscapeDataString(kvp.Key), Uri.EscapeDataString(kvp.Value))));