From 1fde9b6d5cfc380a0ca5b87542bf2d3fbf286b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20J=C4=99drzejczyk?= Date: Sun, 13 Oct 2024 14:59:14 +0200 Subject: [PATCH] 2.0.1 (#21) * Submit listen timestamps at the beginning of the track instead of the end (#19) * fix listen timestamps * forgot to convert to unix timestamp here * and shouldnt need to cast that to int * Update download link * Bump version nr --------- Co-authored-by: ConfiG --- Properties/AssemblyInfo.cs | 4 ++-- README.md | 2 +- ScrobblerBrainz.cs | 22 +++++++++++++--------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 36397af..f457851 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.0.0.0")] -[assembly: AssemblyFileVersion("2.0.0.0")] +[assembly: AssemblyVersion("2.0.1")] +[assembly: AssemblyFileVersion("2.0.1")] diff --git a/README.md b/README.md index ce1c832..02a1b0f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A MusicBee plug-in to scrobble tracks to ListenBrainz. 1. No feature to disable podcast scrobbling. # Download -https://github.com/karaluh/ScrobblerBrainz/releases/download/v2.0.0/mb_ScrobblerBrainz.zip +https://github.com/karaluh/ScrobblerBrainz/releases/download/v2.0.1/mb_ScrobblerBrainz.zip # Usage 1. Get MusicBee https://getmusicbee.com/downloads/ diff --git a/ScrobblerBrainz.cs b/ScrobblerBrainz.cs index 21f1627..1a6e0cd 100644 --- a/ScrobblerBrainz.cs +++ b/ScrobblerBrainz.cs @@ -26,7 +26,7 @@ public partial class Plugin public string settingsFile = "usertoken"; // Old plugin settings file. // Scrobble metadata: - TimeSpan timestamp; + DateTimeOffset timestamp; public string artist = ""; public string track = ""; public string release = ""; @@ -43,7 +43,7 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) // Plugin version: about.VersionMajor = 2; about.VersionMinor = 0; - about.Revision = 0; + about.Revision = 1; about.Name = "ScrobblerBrainz"; about.Description = "A scrobbler for ListenBrainz service, version " + about.VersionMajor + "." + about.VersionMinor + "." + about.Revision; @@ -167,14 +167,14 @@ public void ReceiveNotification(string sourceFileUrl, NotificationType type) if (submitListenResponse.Result.StatusCode.ToString() == "BadRequest") { // Save the scrobble to a file and exit the loop. - SaveScrobble(timestamp.TotalSeconds.ToString(), json); + SaveScrobble(timestamp.ToUnixTimeSeconds().ToString(), json); break; } // If this is the last retry save the scrobble. if (i == 4) { - SaveScrobble(timestamp.TotalSeconds.ToString(), json); + SaveScrobble(timestamp.ToUnixTimeSeconds().ToString(), json); } } } @@ -183,7 +183,7 @@ public void ReceiveNotification(string sourceFileUrl, NotificationType type) { if (json.Contains("\"listen_type\": \"single\"")) // Same as above. { - SaveScrobble(timestamp.TotalSeconds.ToString(), json); + SaveScrobble(timestamp.ToUnixTimeSeconds().ToString(), json); } break; } @@ -244,6 +244,11 @@ string GenerateMbidJson() switch (type) { case NotificationType.PluginStartup: // Perform startup initialisation. + // Get the timestamp in epoch. + // Offset by current playing position assuming the user didn't pause prior to the extension loading, + // should be accurate enough 99% of the time. + timestamp = DateTimeOffset.UtcNow - TimeSpan.FromMilliseconds(mbApiInterface.Player_GetPosition()); + // Get the metadata of the track selected by MusicBee on startup to know what to scrobble. artist = HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist)); track = HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackTitle)); @@ -297,6 +302,7 @@ string GenerateMbidJson() break; case NotificationType.TrackChanged: // Update the metadata on track change. + timestamp = DateTimeOffset.UtcNow; // Get the timestamp in epoch. artist = HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist)); track = HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackTitle)); release = HttpUtility.JavaScriptStringEncode(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Album)); @@ -321,11 +327,9 @@ string GenerateMbidJson() // Scrobble the track but only if the user token is configured and the song wasn't skipped. if (!String.IsNullOrEmpty(userToken) && !(previousPlaycount == mbApiInterface.Library_GetFileProperty(sourceFileUrl, FilePropertyType.PlayCount))) { - timestamp = DateTime.UtcNow - new DateTime(1970, 1, 1); // Get the timestamp in epoch. - // Prepare the scrobble. string submitListenJson = "{\"listen_type\": \"single\", \"payload\": [ { \"listened_at\": " - + (int)timestamp.TotalSeconds + ",\"track_metadata\": {\"artist_name\": \"" + + timestamp.ToUnixTimeSeconds() + ",\"track_metadata\": {\"artist_name\": \"" + artist + "\", \"track_name\": \"" + track + "\", \"release_name\": \"" + release + "\", \"additional_info\": {" + GenerateMbidJson() + "\"duration_ms\":" + duration_ms + "," + "\"media_player\": \"MusicBee\", \"submission_client\": \"ScrobblerBrainz\"} } } ] }"; // Set the scrobble JSON. @@ -406,4 +410,4 @@ public void SaveScrobble(string timestamp, string json) //} } -} \ No newline at end of file +}