Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use start time when scrobbling #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.Lastfm/Api/LastfmApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public async Task<MobileSessionResponse> 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,
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Lastfm/Models/Requests/ScrobbleRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
8 changes: 7 additions & 1 deletion Jellyfin.Plugin.Lastfm/ServerEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
using System.Threading;
using Jellyfin.Plugin.Lastfm.Utils;

/// <summary>
/// Class ServerEntryPoint
Expand All @@ -31,6 +32,7 @@ public class ServerEntryPoint : IHostedService, IDisposable

private LastfmApiClient _apiClient;
private readonly ILogger<ServerEntryPoint> _logger;
private long _playbackTimestamp;

/// <summary>
/// Gets the instance.
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 0 additions & 17 deletions Jellyfin.Plugin.Lastfm/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,6 @@ public static void AppendSignature(ref Dictionary<string, string> 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<string, string> 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))));
Expand Down