diff --git a/PlexRequests.Api.Interfaces/IHeadphonesApi.cs b/PlexRequests.Api.Interfaces/IHeadphonesApi.cs index 5af3940e9..a895f1689 100644 --- a/PlexRequests.Api.Interfaces/IHeadphonesApi.cs +++ b/PlexRequests.Api.Interfaces/IHeadphonesApi.cs @@ -39,5 +39,6 @@ namespace PlexRequests.Api.Interfaces Task AddArtist(string apiKey, Uri baseUrl, string artistId); Task QueueAlbum(string apiKey, Uri baseUrl, string albumId); Task> GetIndex(string apiKey, Uri baseUrl); + Task RefreshArtist(string apiKey, Uri baseUrl, string artistId); } } \ No newline at end of file diff --git a/PlexRequests.Api/HeadphonesApi.cs b/PlexRequests.Api/HeadphonesApi.cs index 398745741..34cecb8fe 100644 --- a/PlexRequests.Api/HeadphonesApi.cs +++ b/PlexRequests.Api/HeadphonesApi.cs @@ -152,6 +152,33 @@ namespace PlexRequests.Api } } + public async Task RefreshArtist(string apiKey, Uri baseUrl, string artistId) + { + Log.Trace("Refreshing artist: {0}", artistId); + var request = new RestRequest + { + Resource = "/api", + Method = Method.GET + }; + + request.AddQueryParameter("apikey", apiKey); + request.AddQueryParameter("cmd", "queueAlbum"); + request.AddQueryParameter("id", artistId); + + try + { + var result = await Task.Run(() => Api.Execute(request, baseUrl)).ConfigureAwait(false); + Log.Info("Artist refresh Result: {0}", result.Content); + Log.Trace("Artist refresh Result: {0}", result.DumpJson()); + return result.Content.Equals("OK", StringComparison.CurrentCultureIgnoreCase); + } + catch (JsonSerializationException jse) + { + Log.Error(jse); + return false; // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + public HeadphonesVersion GetVersion(string apiKey, Uri baseUrl) { var request = new RestRequest diff --git a/PlexRequests.UI/Helpers/HeadphonesSender.cs b/PlexRequests.UI/Helpers/HeadphonesSender.cs index 008f8078e..0b6edaedc 100644 --- a/PlexRequests.UI/Helpers/HeadphonesSender.cs +++ b/PlexRequests.UI/Helpers/HeadphonesSender.cs @@ -24,7 +24,10 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion + +using System; using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; @@ -46,8 +49,8 @@ namespace PlexRequests.UI.Helpers RequestService = request; } - private int WaitTime => 1000; - private int CounterMax => 30; + private int WaitTime => 2000; + private int CounterMax => 60; private static readonly Logger Log = LogManager.GetCurrentClassLogger(); private IHeadphonesApi Api { get; } @@ -103,12 +106,23 @@ namespace PlexRequests.UI.Helpers counter++; Log.Trace("Artist is still not present in the index. Counter = {0}", counter); index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); + //Fetch failed name if (counter > CounterMax) { Log.Trace("Artist is still not present in the index. Counter = {0}. Returning false", counter); return false; } } + var addedArtist = index.FirstOrDefault(x => x.ArtistID == request.ArtistId); + var artistName = addedArtist?.ArtistName ?? string.Empty; + while (artistName.Contains("Fetch failed")) + { + await Api.RefreshArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + + index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); + + artistName = index?.FirstOrDefault(x => x.ArtistID == request.ArtistId)?.ArtistName ?? string.Empty; + } counter = 0; var artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault();