From 5a5e4cae70ec5e71d6d08f0974088183a3dff5e8 Mon Sep 17 00:00:00 2001 From: Steven Hildreth Date: Wed, 19 Jan 2022 10:11:09 -0600 Subject: [PATCH] Fix for LastFM returning string empty objects. --- .../MetaData/LastFm/JsonEntities.cs | 28 +++++++++++++++---- .../MetaData/LastFm/LastFmHelper.cs | 5 +++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Roadie.Api.Library/SearchEngines/MetaData/LastFm/JsonEntities.cs b/Roadie.Api.Library/SearchEngines/MetaData/LastFm/JsonEntities.cs index da6377e..521d9ee 100644 --- a/Roadie.Api.Library/SearchEngines/MetaData/LastFm/JsonEntities.cs +++ b/Roadie.Api.Library/SearchEngines/MetaData/LastFm/JsonEntities.cs @@ -1,4 +1,7 @@ -using System.Text.Json.Serialization; +using Roadie.Library.Caching; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Serialization; namespace Roadie.Library.SearchEngines.MetaData.LastFm { @@ -12,8 +15,8 @@ namespace Roadie.Library.SearchEngines.MetaData.LastFm public string artist { get; set; } public string mbid { get; set; } - - public Tags tags { get; set; } + + public string tags { get; set; } public string name { get; set; } @@ -26,6 +29,23 @@ namespace Roadie.Library.SearchEngines.MetaData.LastFm public string playcount { get; set; } public string url { get; set; } + + /// + /// Sometimes LastFM returns string empty for an object (?!) and that blows up the serializer. This returns tags if the object value isn't an empty string. + /// + public IEnumerable GetTags(ICacheSerializer serializer) + { + if(string.IsNullOrWhiteSpace(tags)) + { + return Enumerable.Empty(); + } + var t = serializer.Deserialize(tags); + if(t != null) + { + return t.tag; + } + return Enumerable.Empty(); + } } public class Tags @@ -58,8 +78,6 @@ namespace Roadie.Library.SearchEngines.MetaData.LastFm [JsonPropertyName("@attr")] public Attr attr { get; set; } - //public int? TrackNumber => string.IsNullOrWhiteSpace(attr) ? null : int.Parse(attr.Replace("\"@attr\":{\"rank\":", "").Replace("}", "")); - public int? TrackNumber => attr?.rank; public Artist artist { get; set; } diff --git a/Roadie.Api.Library/SearchEngines/MetaData/LastFm/LastFmHelper.cs b/Roadie.Api.Library/SearchEngines/MetaData/LastFm/LastFmHelper.cs index dca93b5..d47fe5e 100644 --- a/Roadie.Api.Library/SearchEngines/MetaData/LastFm/LastFmHelper.cs +++ b/Roadie.Api.Library/SearchEngines/MetaData/LastFm/LastFmHelper.cs @@ -240,7 +240,10 @@ namespace Roadie.Library.MetaData.LastFm // No longer fetching/consuming images LastFm says is violation of ToS ; https://getsatisfaction.com/lastfm/topics/api-announcement-dac8oefw5vrxq - if (lastFmAlbum.tags != null) result.Tags = lastFmAlbum.tags.tag.Select(x => x.name).ToList(); + if (!string.IsNullOrWhiteSpace(lastFmAlbum.tags)) + { + result.Tags = lastFmAlbum.GetTags(CacheManager.CacheSerializer)?.Select(x => x.name).ToList(); + } if (lastFmAlbum.tracks != null) { var tracks = new List();