mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-30 11:38:32 -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);
|
PlexEpisodeMetadata GetEpisodeMetaData(string authToken, Uri host, string ratingKey);
|
||||||
PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int startPage, int returnCount);
|
PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int startPage, int returnCount);
|
||||||
PlexServer GetServer(string authToken);
|
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; }
|
public string UpdatedAt { get; set; }
|
||||||
[XmlAttribute(AttributeName = "parentTitle")]
|
[XmlAttribute(AttributeName = "parentTitle")]
|
||||||
public string ParentTitle { get; set; }
|
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)
|
public PlexServer GetServer(string authToken)
|
||||||
{
|
{
|
||||||
var request = new RestRequest
|
var request = new RestRequest
|
||||||
|
|
|
@ -55,6 +55,11 @@ namespace PlexRequests.Helpers.Tests
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCaseSource(nameof(SeasonNumbers))]
|
||||||
|
public int TitleToSeasonNumber(string title)
|
||||||
|
{
|
||||||
|
return PlexHelper.GetSeasonNumberFromTitle(title);
|
||||||
|
}
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> PlexGuids
|
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
|
private static IEnumerable<TestCaseData> PlexTvEpisodeGuids
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||||
<dependentAssembly>
|
<dependentAssembly>
|
||||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
|
<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>
|
||||||
<dependentAssembly>
|
<dependentAssembly>
|
||||||
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||||
|
|
|
@ -72,6 +72,29 @@ namespace PlexRequests.Helpers
|
||||||
return ep;
|
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
|
public class EpisodeModelHelper
|
||||||
|
|
|
@ -38,7 +38,7 @@ namespace PlexRequests.Services.Interfaces
|
||||||
List<PlexMovie> GetPlexMovies();
|
List<PlexMovie> GetPlexMovies();
|
||||||
bool IsMovieAvailable(PlexMovie[] plexMovies, string title, string year, string providerId = null);
|
bool IsMovieAvailable(PlexMovie[] plexMovies, string title, string year, string providerId = null);
|
||||||
List<PlexTvShow> GetPlexTvShows();
|
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();
|
List<PlexAlbum> GetPlexAlbums();
|
||||||
bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist);
|
bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist);
|
||||||
bool IsEpisodeAvailable(string theTvDbId, int season, int episode);
|
bool IsEpisodeAvailable(string theTvDbId, int season, int episode);
|
||||||
|
|
|
@ -215,19 +215,27 @@ namespace PlexRequests.Services.Jobs
|
||||||
Title = x.Title,
|
Title = x.Title,
|
||||||
ReleaseYear = x.Year,
|
ReleaseYear = x.Year,
|
||||||
ProviderId = x.ProviderId,
|
ProviderId = x.ProviderId,
|
||||||
|
Seasons = x.Seasons.Select(d => PlexHelper.GetSeasonNumberFromTitle(d.Title)).ToArray()
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return shows;
|
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);
|
var advanced = !string.IsNullOrEmpty(providerId);
|
||||||
foreach (var show in plexShows)
|
foreach (var show in plexShows)
|
||||||
{
|
{
|
||||||
if (advanced)
|
if (advanced)
|
||||||
{
|
{
|
||||||
|
if (seasons != null)
|
||||||
|
{
|
||||||
|
if (seasons.Any(season => show.Seasons.Contains(season)))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!string.IsNullOrEmpty(show.ProviderId) &&
|
if (!string.IsNullOrEmpty(show.ProviderId) &&
|
||||||
show.ProviderId.Equals(providerId, StringComparison.InvariantCultureIgnoreCase))
|
show.ProviderId.Equals(providerId, StringComparison.InvariantCultureIgnoreCase))
|
||||||
{
|
{
|
||||||
|
@ -366,6 +374,8 @@ namespace PlexRequests.Services.Jobs
|
||||||
var currentItem = results[i].Directory[j];
|
var currentItem = results[i].Directory[j];
|
||||||
var metaData = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
|
var metaData = PlexApi.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri,
|
||||||
currentItem.RatingKey);
|
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);
|
var providerId = PlexHelper.GetProviderIdFromPlexGuid(metaData.Directory.Guid);
|
||||||
results[i].Directory[j].ProviderId = providerId;
|
results[i].Directory[j].ProviderId = providerId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
public string ReleaseYear { get; set; }
|
public string ReleaseYear { get; set; }
|
||||||
public string ProviderId { 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 PlexRequests.Store.Repository;
|
||||||
|
|
||||||
using TMDbLib.Objects.General;
|
using TMDbLib.Objects.General;
|
||||||
|
using TMDbLib.Objects.Search;
|
||||||
|
|
||||||
using Action = PlexRequests.Helpers.Analytics.Action;
|
using Action = PlexRequests.Helpers.Analytics.Action;
|
||||||
using EpisodesModel = PlexRequests.Store.EpisodesModel;
|
using EpisodesModel = PlexRequests.Store.EpisodesModel;
|
||||||
|
@ -190,7 +191,7 @@ namespace PlexRequests.UI.Modules
|
||||||
case MovieSearchType.Search:
|
case MovieSearchType.Search:
|
||||||
var movies = await MovieApi.SearchMovie(searchTerm);
|
var movies = await MovieApi.SearchMovie(searchTerm);
|
||||||
apiMovies = movies.Select(x =>
|
apiMovies = movies.Select(x =>
|
||||||
new MovieResult()
|
new MovieResult
|
||||||
{
|
{
|
||||||
Adult = x.Adult,
|
Adult = x.Adult,
|
||||||
BackdropPath = x.BackdropPath,
|
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
|
var model = new RequestedModel
|
||||||
{
|
{
|
||||||
ProviderId = showInfo.externals?.thetvdb ?? 0,
|
ProviderId = showInfo.externals?.thetvdb ?? 0,
|
||||||
|
@ -687,6 +653,41 @@ namespace PlexRequests.UI.Modules
|
||||||
|
|
||||||
model.SeasonList = seasonsList.ToArray();
|
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))
|
if (ShouldAutoApprove(RequestType.TvShow, settings))
|
||||||
{
|
{
|
||||||
model.Approved = true;
|
model.Approved = true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue