Skip to content

Commit

Permalink
Some final tweaks for #32
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Apr 5, 2016
1 parent 5e5fe1f commit f3cca9f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
7 changes: 4 additions & 3 deletions PlexRequests.Core/StatusChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#endregion
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading.Tasks;

using Octokit;
Expand Down Expand Up @@ -62,7 +60,10 @@ public StatusModel GetStatus()
};

var latestRelease = GetLatestRelease();

if (latestRelease.Result == null)
{
return new StatusModel { Version = "Unknown" };
}
var latestVersionArray = latestRelease.Result.Name.Split(new[] { 'v' }, StringSplitOptions.RemoveEmptyEntries);
var latestVersion = latestVersionArray.Length > 1 ? latestVersionArray[1] : string.Empty;

Expand Down
36 changes: 20 additions & 16 deletions PlexRequests.UI/Helpers/HeadphonesSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,40 +111,44 @@ private async Task<bool> AddArtist(RequestedModel request)
return false;
}
}
var addedArtist = index.FirstOrDefault(x => x.ArtistID == request.ArtistId);
var artistName = addedArtist?.ArtistName ?? string.Empty;
while (artistName.Contains("Fetch failed"))

counter = 0;
var artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault();
while (artistStatus != "Active")
{
Thread.Sleep(WaitTime);
await Api.RefreshArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId);

counter++;
Log.Trace("Artist status {1}. Counter = {0}", counter, artistStatus);
index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri);

artistName = index?.FirstOrDefault(x => x.ArtistID == request.ArtistId)?.ArtistName ?? string.Empty;
artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault();
if (counter > CounterMax)
{
Log.Trace("Artist fetch has failed. Counter = {0}. Returning false", counter);
Log.Warn("Artist in headphones fetch has failed, we have tried refreshing the artist but no luck.");
Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter);
Log.Warn("The artist status is still not Active. We have waited long enough, seems to be a big delay in headphones.");
return false;
}
}

var addedArtist = index.FirstOrDefault(x => x.ArtistID == request.ArtistId);
var artistName = addedArtist?.ArtistName ?? string.Empty;
counter = 0;
var artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault();
while (artistStatus != "Active")
while (artistName.Contains("Fetch failed"))
{
Thread.Sleep(WaitTime);
counter++;
Log.Trace("Artist status {1}. Counter = {0}", counter, artistStatus);
await Api.RefreshArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId);

index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri);
artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault();

artistName = index?.FirstOrDefault(x => x.ArtistID == request.ArtistId)?.ArtistName ?? string.Empty;
counter++;
if (counter > CounterMax)
{
Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter);
Log.Warn("The artist status is still not Active. We have waited long enough, seems to be a big delay in headphones.");
Log.Trace("Artist fetch has failed. Counter = {0}. Returning false", counter);
Log.Warn("Artist in headphones fetch has failed, we have tried refreshing the artist but no luck.");
return false;
}
}

return true;
}

Expand Down
31 changes: 22 additions & 9 deletions PlexRequests.UI/Modules/SearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,24 +502,36 @@ private Response RequestAlbum(string releaseId)


Log.Debug("This is a new request");

var albumInfo = MusicBrainzApi.GetAlbum(releaseId);
var img = GetMusicBrainzCoverArt(albumInfo.id);

Log.Trace("Album Details:");
Log.Trace(albumInfo.DumpJson());
Log.Trace("CoverArt Details:");
Log.Trace(img.DumpJson());

DateTime release;
DateTimeHelper.CustomParse(albumInfo.ReleaseEvents?.FirstOrDefault()?.date, out release);

var artist = albumInfo.ArtistCredits?.FirstOrDefault()?.artist;
if (artist == null)
{
return Response.AsJson(new JsonResponseModel {Result = false, Message = "We could not find the artist on MusicBrainz. Please try again later or contact your admin"});
return Response.AsJson(new JsonResponseModel { Result = false, Message = "We could not find the artist on MusicBrainz. Please try again later or contact your admin" });
}

var alreadyInPlex = CheckIfTitleExistsInPlex(albumInfo.title, release.ToString("yyyy"), artist.name, PlexType.Music);

if (alreadyInPlex)
{
return Response.AsJson(new JsonResponseModel
{
Result = false,
Message = $"{albumInfo.title} is already in Plex!"
});
}

var img = GetMusicBrainzCoverArt(albumInfo.id);

Log.Trace("Album Details:");
Log.Trace(albumInfo.DumpJson());
Log.Trace("CoverArt Details:");
Log.Trace(img.DumpJson());


var model = new RequestedModel
{
Title = albumInfo.title,
Expand Down Expand Up @@ -559,6 +571,7 @@ private Response RequestAlbum(string releaseId)

var sender = new HeadphonesSender(HeadphonesApi, hpSettings, RequestService);
sender.AddAlbum(model);
model.Approved = true;
RequestService.AddRequest(model);

return
Expand Down

0 comments on commit f3cca9f

Please sign in to comment.