mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-16 02:02:55 -07:00
Attempt at fixing a potential bug found from #466
This commit is contained in:
parent
f098e0355e
commit
a7d5378426
11 changed files with 222 additions and 133 deletions
|
@ -43,5 +43,6 @@ namespace PlexRequests.Api.Interfaces
|
|||
PlexEpisodeMetadata GetEpisodeMetaData(string authToken, Uri host, string ratingKey);
|
||||
PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int startPage, int returnCount);
|
||||
PlexServer GetServer(string authToken);
|
||||
PlexMetadata GetSeasons(string authToken, Uri plexFullHost, string ratingKey);
|
||||
}
|
||||
}
|
|
@ -311,6 +311,7 @@ namespace PlexRequests.Api.Models.Plex
|
|||
public string UpdatedAt { get; set; }
|
||||
[XmlAttribute(AttributeName = "parentTitle")]
|
||||
public string ParentTitle { get; set; }
|
||||
public List<Directory1> Seasons { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -301,6 +301,36 @@ namespace PlexRequests.Api
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public PlexMetadata GetSeasons(string authToken, Uri plexFullHost, string ratingKey)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.GET,
|
||||
Resource = "library/metadata/{ratingKey}/children"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("ratingKey", ratingKey);
|
||||
AddHeaders(ref request, authToken);
|
||||
|
||||
try
|
||||
{
|
||||
var lib = RetryHandler.Execute(() => Api.ExecuteXml<PlexMetadata>(request, plexFullHost),
|
||||
(exception, timespan) => Log.Error(exception, "Exception when calling GetMetadata for Plex, Retrying {0}", timespan), new[] {
|
||||
TimeSpan.FromSeconds (5),
|
||||
TimeSpan.FromSeconds(10),
|
||||
TimeSpan.FromSeconds(30)
|
||||
});
|
||||
|
||||
return lib;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "There has been a API Exception when attempting to get the Plex GetMetadata");
|
||||
return new PlexMetadata();
|
||||
}
|
||||
}
|
||||
|
||||
public PlexServer GetServer(string authToken)
|
||||
{
|
||||
var request = new RestRequest
|
||||
|
|
|
@ -55,6 +55,11 @@ namespace PlexRequests.Helpers.Tests
|
|||
return list;
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(SeasonNumbers))]
|
||||
public int TitleToSeasonNumber(string title)
|
||||
{
|
||||
return PlexHelper.GetSeasonNumberFromTitle(title);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> PlexGuids
|
||||
{
|
||||
|
@ -70,6 +75,23 @@ namespace PlexRequests.Helpers.Tests
|
|||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> SeasonNumbers
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new TestCaseData("Season 1").Returns(1).SetName("Season 1");
|
||||
yield return new TestCaseData("Season 2").Returns(2).SetName("Season 2");
|
||||
yield return new TestCaseData("Season 3").Returns(3).SetName("Season 3");
|
||||
yield return new TestCaseData("Season 4").Returns(4).SetName("Season 4");
|
||||
yield return new TestCaseData("Season 5").Returns(5).SetName("Season 5");
|
||||
yield return new TestCaseData("Season 100").Returns(100).SetName("Season 100");
|
||||
yield return new TestCaseData("InvalidSeason").Returns(0).SetName("InvalidSeason");
|
||||
yield return new TestCaseData("Invalid Season with no number").Returns(0).SetName("Invalid Season with no number");
|
||||
yield return new TestCaseData("").Returns(0).SetName("Empty string");
|
||||
yield return new TestCaseData(null).Returns(0).SetName("Null string");
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> PlexTvEpisodeGuids
|
||||
{
|
||||
get
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||
|
|
|
@ -72,6 +72,29 @@ namespace PlexRequests.Helpers
|
|||
return ep;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetSeasonNumberFromTitle(string title)
|
||||
{
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var split = title.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (split.Length < 2)
|
||||
{
|
||||
// Cannot get the season number, it's not in the usual format
|
||||
return 0;
|
||||
}
|
||||
|
||||
int season;
|
||||
if (int.TryParse(split[1], out season))
|
||||
{
|
||||
return season;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class EpisodeModelHelper
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace PlexRequests.Services.Interfaces
|
|||
List<PlexMovie> GetPlexMovies();
|
||||
bool IsMovieAvailable(PlexMovie[] plexMovies, string title, string year, string providerId = null);
|
||||
List<PlexTvShow> GetPlexTvShows();
|
||||
bool IsTvShowAvailable(PlexTvShow[] plexShows, string title, string year, string providerId = null);
|
||||
bool IsTvShowAvailable(PlexTvShow[] plexShows, string title, string year, string providerId = null, int[] seasons = null);
|
||||
List<PlexAlbum> GetPlexAlbums();
|
||||
bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist);
|
||||
bool IsEpisodeAvailable(string theTvDbId, int season, int episode);
|
||||
|
|
|
@ -215,19 +215,27 @@ namespace PlexRequests.Services.Jobs
|
|||
Title = x.Title,
|
||||
ReleaseYear = x.Year,
|
||||
ProviderId = x.ProviderId,
|
||||
Seasons = x.Seasons.Select(d => PlexHelper.GetSeasonNumberFromTitle(d.Title)).ToArray()
|
||||
}));
|
||||
}
|
||||
}
|
||||
return shows;
|
||||
}
|
||||
|
||||
public bool IsTvShowAvailable(PlexTvShow[] plexShows, string title, string year, string providerId = null)
|
||||
public bool IsTvShowAvailable(PlexTvShow[] plexShows, string title, string year, string providerId = null, int[] seasons = null)
|
||||
{
|
||||
var advanced = !string.IsNullOrEmpty(providerId);
|
||||
foreach (var show in plexShows)
|
||||
{
|
||||
if (advanced)
|
||||
{
|
||||
if (seasons != null)
|
||||
{
|
||||
if (seasons.Any(season => show.Seasons.Contains(season)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(show.ProviderId) &&
|
||||
show.ProviderId.Equals(providerId, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
|
@ -366,6 +374,8 @@ namespace PlexRequests.Services.Jobs
|
|||
var currentItem = results[i].Directory[j];
|
||||
var metaData = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
|
||||
currentItem.RatingKey);
|
||||
var seasons = PlexApi.GetSeasons(plexSettings.PlexAuthToken, plexSettings.FullUri, currentItem.RatingKey);
|
||||
results[i].Directory[j] = seasons.Directory;
|
||||
var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Directory.Guid);
|
||||
results[i].Directory[j].ProviderId = providerId;
|
||||
}
|
||||
|
|
|
@ -5,5 +5,6 @@
|
|||
public string Title { get; set; }
|
||||
public string ReleaseYear { get; set; }
|
||||
public string ProviderId { get; set; }
|
||||
public int[] Seasons { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ using PlexRequests.Store.Models;
|
|||
using PlexRequests.Store.Repository;
|
||||
|
||||
using TMDbLib.Objects.General;
|
||||
using TMDbLib.Objects.Search;
|
||||
|
||||
using Action = PlexRequests.Helpers.Analytics.Action;
|
||||
using EpisodesModel = PlexRequests.Store.EpisodesModel;
|
||||
|
@ -190,7 +191,7 @@ namespace PlexRequests.UI.Modules
|
|||
case MovieSearchType.Search:
|
||||
var movies = await MovieApi.SearchMovie(searchTerm);
|
||||
apiMovies = movies.Select(x =>
|
||||
new MovieResult()
|
||||
new MovieResult
|
||||
{
|
||||
Adult = x.Adult,
|
||||
BackdropPath = x.BackdropPath,
|
||||
|
@ -594,41 +595,6 @@ namespace PlexRequests.UI.Modules
|
|||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var shows = Checker.GetPlexTvShows();
|
||||
var providerId = string.Empty;
|
||||
var plexSettings = await PlexService.GetSettingsAsync();
|
||||
if (plexSettings.AdvancedSearch)
|
||||
{
|
||||
providerId = showId.ToString();
|
||||
}
|
||||
if (episodeRequest)
|
||||
{
|
||||
var cachedEpisodesTask = await Checker.GetEpisodes();
|
||||
var cachedEpisodes = cachedEpisodesTask.ToList();
|
||||
foreach (var d in difference)
|
||||
{
|
||||
if (cachedEpisodes.Any(x => x.SeasonNumber == d.SeasonNumber && x.EpisodeNumber == d.EpisodeNumber && x.ProviderId == providerId))
|
||||
{
|
||||
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {d.SeasonNumber} - {d.EpisodeNumber} {Resources.UI.Search_AlreadyInPlex}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return Response.AsJson(new JsonResponseModel { Result = false, Message = string.Format(Resources.UI.Search_CouldNotCheckPlex, fullShowName) });
|
||||
}
|
||||
|
||||
|
||||
var model = new RequestedModel
|
||||
{
|
||||
ProviderId = showInfo.externals?.thetvdb ?? 0,
|
||||
|
@ -687,6 +653,41 @@ namespace PlexRequests.UI.Modules
|
|||
|
||||
model.SeasonList = seasonsList.ToArray();
|
||||
|
||||
try
|
||||
{
|
||||
var shows = Checker.GetPlexTvShows();
|
||||
var providerId = string.Empty;
|
||||
var plexSettings = await PlexService.GetSettingsAsync();
|
||||
if (plexSettings.AdvancedSearch)
|
||||
{
|
||||
providerId = showId.ToString();
|
||||
}
|
||||
if (episodeRequest)
|
||||
{
|
||||
var cachedEpisodesTask = await Checker.GetEpisodes();
|
||||
var cachedEpisodes = cachedEpisodesTask.ToList();
|
||||
foreach (var d in difference)
|
||||
{
|
||||
if (cachedEpisodes.Any(x => x.SeasonNumber == d.SeasonNumber && x.EpisodeNumber == d.EpisodeNumber && x.ProviderId == providerId))
|
||||
{
|
||||
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {d.SeasonNumber} - {d.EpisodeNumber} {Resources.UI.Search_AlreadyInPlex}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Checker.IsTvShowAvailable(shows.ToArray(), showInfo.name, showInfo.premiered?.Substring(0, 4), providerId, model.SeasonList))
|
||||
{
|
||||
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {Resources.UI.Search_AlreadyInPlex}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return Response.AsJson(new JsonResponseModel { Result = false, Message = string.Format(Resources.UI.Search_CouldNotCheckPlex, fullShowName) });
|
||||
}
|
||||
|
||||
|
||||
if (ShouldAutoApprove(RequestType.TvShow, settings))
|
||||
{
|
||||
model.Approved = true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue