Skip to content

Commit

Permalink
2.0.1 (#21)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
karaluh and cgytrus authored Oct 13, 2024
1 parent b52c855 commit 1fde9b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
22 changes: 13 additions & 9 deletions ScrobblerBrainz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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.
Expand Down Expand Up @@ -406,4 +410,4 @@ public void SaveScrobble(string timestamp, string json)
//}

}
}
}

0 comments on commit 1fde9b6

Please sign in to comment.