Skip to content

Commit

Permalink
More work on #254
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Jul 26, 2016
1 parent 4d85000 commit 9aad3f9
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions PlexRequests.UI/Modules/SearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public SearchModule(ICacheProvider cache, ISettingsService<CouchPotatoSettings>

Post["request/movie", true] = async (x, ct) => await RequestMovie((int)Request.Form.movieId);
Post["request/tv", true] = async (x, ct) => await RequestTvShow((int)Request.Form.tvId, (string)Request.Form.seasons);
Post["request/tvEpisodes", true] = async (x, ct) => await RequestEpisodes();
Post["request/tvEpisodes", true] = async (x, ct) => await RequestTvShow(0,"episode");
Post["request/album", true] = async (x, ct) => await RequestAlbum((string)Request.Form.albumId);

Post["/notifyuser", true] = async (x, ct) => await NotifyUser((bool)Request.Form.notify);
Expand Down Expand Up @@ -525,13 +525,39 @@ private async Task<Response> RequestMovie(int movieId)
/// <returns></returns>
private async Task<Response> RequestTvShow(int showId, string seasons)
{
// Get the JSON from the request
var req = (Dictionary<string, object>.ValueCollection)Request.Form.Values;
var json = req.FirstOrDefault()?.ToString();
var episodeModel = JsonConvert.DeserializeObject<EpisodeRequestModel>(json); // Convert it into the object

var episodeResult = false;
var episodeRequest = false;

var settings = await PrService.GetSettingsAsync();
if (!await CheckRequestLimit(settings, RequestType.TvShow))
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = Resources.UI.Search_WeeklyRequestLimitTVShow });
}
Analytics.TrackEventAsync(Category.Search, Action.Request, "TvShow", Username, CookieHelper.GetAnalyticClientId(Cookies));

// This means we are requesting an episode rather than a whole series or season
if (episodeModel != null)
{
episodeRequest = true;
var sonarrSettings = await SonarrService.GetSettingsAsync();
if (!sonarrSettings.Enabled)
{
return Response.AsJson("This is currently only supported with Sonarr");
}

var series = await GetSonarrSeries(sonarrSettings, episodeModel.ShowId);
if (series != null)
{
// The series already exists in Sonarr
episodeResult = RequestEpisodesWithExistingSeries(episodeModel, series, sonarrSettings);
}
}

var showInfo = TvApi.ShowLookupByTheTvDbId(showId);
DateTime firstAir;
DateTime.TryParse(showInfo.premiered, out firstAir);
Expand Down Expand Up @@ -565,6 +591,7 @@ private async Task<Response> RequestTvShow(int showId, string seasons)
{
providerId = showId.ToString();
}
// TODO: If it's an episode request, check if the episode exists
if (Checker.IsTvShowAvailable(shows.ToArray(), showInfo.name, showInfo.premiered?.Substring(0, 4), providerId))
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {Resources.UI.Search_AlreadyInPlex}" });
Expand Down Expand Up @@ -596,6 +623,7 @@ private async Task<Response> RequestTvShow(int showId, string seasons)
};

var seasonsList = new List<int>();
//TODO something here for the episodes
switch (seasons)
{
case "first":
Expand All @@ -609,6 +637,8 @@ private async Task<Response> RequestTvShow(int showId, string seasons)
case "all":
model.SeasonsRequested = "All";
break;
case "episode":

default:
model.SeasonsRequested = seasons;
var split = seasons.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -956,37 +986,8 @@ await RequestLimitRepo.InsertAsync(new RequestLimit
return Response.AsJson(new JsonResponseModel { Result = true, Message = message });
}

private async Task<Response> RequestEpisodes()
private bool RequestEpisodesWithExistingSeries(EpisodeRequestModel model, Series selectedSeries, SonarrSettings sonarrSettings)
{
var req = (Dictionary<string, object>.ValueCollection)this.Request.Form.Values;
var json = req.FirstOrDefault().ToString();
var model = JsonConvert.DeserializeObject<EpisodeRequestModel>(json);
//var model = this.Bind<EpisodeRequestModel>();
if (model == null)
{
return Nancy.Response.NoBody;
}
var sonarrSettings = await SonarrService.GetSettingsAsync();
if (!sonarrSettings.Enabled)
{
return Response.AsJson("Need sonarr");
}

var existingRequest = await RequestService.CheckRequestAsync(model.ShowId);




// Find the correct series
var task = await Task.Run(() => SonarrApi.GetSeries(sonarrSettings.ApiKey, sonarrSettings.FullUri)).ConfigureAwait(false);
var selectedSeries = task.FirstOrDefault(series => series.tvdbId == model.ShowId);
if (selectedSeries == null)
{

// Need to add the series as unmonitored.
return Response.AsJson("");
}

// Show Exists
// Look up all episodes
var episodes = SonarrApi.GetEpisodes(selectedSeries.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri).ToList();
Expand All @@ -1010,9 +1011,16 @@ private async Task<Response> RequestEpisodes()
Task.WaitAll(tasks.ToArray());

SonarrApi.SearchForEpisodes(internalEpisodeIds.ToArray(), sonarrSettings.ApiKey, sonarrSettings.FullUri);

return true;
}

private async Task<Series> GetSonarrSeries(SonarrSettings sonarrSettings, int showId)
{
var task = await Task.Run(() => SonarrApi.GetSeries(sonarrSettings.ApiKey, sonarrSettings.FullUri)).ConfigureAwait(false);
var selectedSeries = task.FirstOrDefault(series => series.tvdbId == showId);

return Response.AsJson(new JsonResponseModel() { Result = true });
return selectedSeries;
}

}
Expand Down

0 comments on commit 9aad3f9

Please sign in to comment.