Improvements to album art retrieval

This commit is contained in:
Thord Johansson 2024-08-12 18:07:38 +02:00
parent 90eca9e76a
commit 67a3549e7b
2 changed files with 36 additions and 9 deletions

View File

@ -87,7 +87,20 @@ public class LbzRpcService
var username = config_data["general"]["listenbrainz_username"];
var ignoredPlayers = config_data["general"]["ignored_sources"].Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var listen = listenBrainz.GetPlayingNow(username);
MetaBrainz.ListenBrainz.Interfaces.IPlayingNow? listen = null;
try
{
listen = listenBrainz.GetPlayingNow(username);
}
catch (MetaBrainz.Common.HttpError e)
{
Console.WriteLine("Error getting playing now: {0}", e.Message);
Thread.Sleep(10000);
continue;
}
if (listen != null)
{
//check ignored players
@ -101,7 +114,10 @@ public class LbzRpcService
var listenData = listen.Track?.Info;
if (listenData != null)
{
if (listenData.AdditionalInfo.OriginUrl != null && (listenData.AdditionalInfo.OriginUrl?.Host == "music.youtube.com" || listenData.AdditionalInfo.OriginUrl?.Host == "www.youtube.com") && lastRecording != GetYoutubeId(listenData.AdditionalInfo.OriginUrl))
if (listenData.AdditionalInfo.OriginUrl != null
&& (listenData.AdditionalInfo.OriginUrl?.Host == "music.youtube.com" || listenData.AdditionalInfo.OriginUrl?.Host == "www.youtube.com")
&& GetYoutubeId(listenData.AdditionalInfo.OriginUrl) != null
&& lastRecording != GetYoutubeId(listenData.AdditionalInfo.OriginUrl))
{
lastRecording = GetYoutubeId(listenData.AdditionalInfo.OriginUrl);
@ -129,6 +145,8 @@ public class LbzRpcService
}
}
lastRecording = GetYoutubeId(listenData.AdditionalInfo.OriginUrl);
if (release != null)
{
albumArtUrl = "https://coverartarchive.org/release/" + release.Item.Id + "/front-250.jpg";
@ -140,12 +158,12 @@ public class LbzRpcService
albumArtUrl = "https://img.youtube.com/vi/" + youtubeId + "/mqdefault.jpg";
}
}
else if (lastRecording != listenData.AdditionalInfo?.RecordingId.ToString()) // MusicBrainz ID available
else if (lastRecording != listenData.AdditionalInfo?.RecordingId.ToString() && listenData.AdditionalInfo?.RecordingId != null) // MusicBrainz ID available
{
Console.WriteLine("Listening to {0} by {1}", listenData.Name, listenData.Artist);
lastRecording = listenData.AdditionalInfo?.RecordingId.ToString();
buttons = new List<Button>();
buttons = [];
if (listenData.AdditionalInfo?.RecordingId != null)
{
@ -184,25 +202,29 @@ public class LbzRpcService
newAlbumArt = "https://coverartarchive.org/release/" + release?.Item.Id + "/front-250.jpg";
}
}
Console.WriteLine("Album art URL: " + (newAlbumArt ?? "(none)"));
albumArtUrl = newAlbumArt ?? "";
}
else
{
albumArtUrl = "";
}
client.SetPresence(new RichPresence()
{
Details = $"{listenData.Name}",
State = (listenData.Release != null && listenData.Release != listenData.Name) ? $"{listenData.Release} ({listenData.Artist})" : listenData.Artist,
State = listenData.Artist,
Buttons = buttons.ToArray(),
Assets = new Assets()
{
SmallImageKey = "play",
LargeImageKey = albumArtUrl,
LargeImageKey = albumArtUrl?.Length > 0 ? albumArtUrl : null,
LargeImageText = $"Listening to {listenData.Name} by {listenData.Artist}{(listenData.AdditionalInfo?.MediaPlayer != null ? " (via " + listenData.AdditionalInfo.MediaPlayer + ")" : "")}"
}
});
}
else
else if (listenData == null)
{
Console.WriteLine("Not listening to anything");
client.ClearPresence();
@ -211,7 +233,11 @@ public class LbzRpcService
}
else
{
Console.WriteLine("Not listening to anything");
if (lastRecording != null) {
Console.WriteLine("Not listening to anything");
lastRecording = null;
}
client.ClearPresence();
}

View File

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="INIFileParserDotNetCore" Version="2.5.2" />
<PackageReference Include="MetaBrainz.ListenBrainz" Version="4.0.0" />
<PackageReference Include="MetaBrainz.MusicBrainz" Version="6.1.0" />