diff --git a/Ombi.Api.Interfaces/IEmbyApi.cs b/Ombi.Api.Interfaces/IEmbyApi.cs index ddc85868c..bc4697140 100644 --- a/Ombi.Api.Interfaces/IEmbyApi.cs +++ b/Ombi.Api.Interfaces/IEmbyApi.cs @@ -14,5 +14,6 @@ namespace Ombi.Api.Interfaces EmbyItemContainer ViewLibrary(string apiKey, string userId, Uri baseUri); EmbyInformation GetInformation(string mediaId, EmbyMediaType type, string apiKey, string userId, Uri baseUri); EmbyUser LogIn(string username, string password, string apiKey, Uri baseUri); + EmbySystemInfo GetSystemInformation(string apiKey, Uri baseUrl); } } \ No newline at end of file diff --git a/Ombi.Api.Models/Emby/EmbySystemInfo.cs b/Ombi.Api.Models/Emby/EmbySystemInfo.cs new file mode 100644 index 000000000..e4b6859fc --- /dev/null +++ b/Ombi.Api.Models/Emby/EmbySystemInfo.cs @@ -0,0 +1,63 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: EmbySystemInfo.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace Ombi.Api.Models.Emby +{ + public class EmbySystemInfo + { + public string SystemUpdateLevel { get; set; } + public string OperatingSystemDisplayName { get; set; } + public bool SupportsRunningAsService { get; set; } + public string MacAddress { get; set; } + public bool HasPendingRestart { get; set; } + public bool SupportsLibraryMonitor { get; set; } + public object[] InProgressInstallations { get; set; } + public int WebSocketPortNumber { get; set; } + public object[] CompletedInstallations { get; set; } + public bool CanSelfRestart { get; set; } + public bool CanSelfUpdate { get; set; } + public object[] FailedPluginAssemblies { get; set; } + public string ProgramDataPath { get; set; } + public string ItemsByNamePath { get; set; } + public string CachePath { get; set; } + public string LogPath { get; set; } + public string InternalMetadataPath { get; set; } + public string TranscodingTempPath { get; set; } + public int HttpServerPortNumber { get; set; } + public bool SupportsHttps { get; set; } + public int HttpsPortNumber { get; set; } + public bool HasUpdateAvailable { get; set; } + public bool SupportsAutoRunAtStartup { get; set; } + public string EncoderLocationType { get; set; } + public string SystemArchitecture { get; set; } + public string LocalAddress { get; set; } + public string WanAddress { get; set; } + public string ServerName { get; set; } + public string Version { get; set; } + public string OperatingSystem { get; set; } + public string Id { get; set; } + } +} \ No newline at end of file diff --git a/Ombi.Api.Models/Ombi.Api.Models.csproj b/Ombi.Api.Models/Ombi.Api.Models.csproj index 3d5fc3460..eb3297999 100644 --- a/Ombi.Api.Models/Ombi.Api.Models.csproj +++ b/Ombi.Api.Models/Ombi.Api.Models.csproj @@ -71,6 +71,7 @@ + diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index a27d4af28..2de72101d 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -27,6 +27,7 @@ using System; using System.IO; +using System.Net; using System.Xml.Serialization; using Newtonsoft.Json; using NLog; @@ -76,14 +77,7 @@ namespace Ombi.Api var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - - if (response.ErrorException != null) - { - Log.Error(response.ErrorException); - var message = "Error retrieving response. Check inner details for more info."; - throw new ApiRequestException(message, response.ErrorException); - } - + return response; } diff --git a/Ombi.Api/EmbyApi.cs b/Ombi.Api/EmbyApi.cs index 9625ffa4f..83028153a 100644 --- a/Ombi.Api/EmbyApi.cs +++ b/Ombi.Api/EmbyApi.cs @@ -33,6 +33,7 @@ using NLog; using Ombi.Api.Interfaces; using Ombi.Api.Models.Emby; using Ombi.Helpers; +using Polly; using RestSharp; namespace Ombi.Api @@ -71,6 +72,26 @@ namespace Ombi.Api return obj; } + public EmbySystemInfo GetSystemInformation(string apiKey, Uri baseUrl) + { + var request = new RestRequest + { + Resource = "emby/System/Info", + Method = Method.GET + }; + + AddHeaders(request, apiKey); + + var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetSystemInformation for Emby, Retrying {0}", timespan), new[] { + TimeSpan.FromSeconds (1), + TimeSpan.FromSeconds(5) + }); + + var obj = policy.Execute(() => Api.ExecuteJson(request, baseUrl)); + + return obj; + } + public EmbyItemContainer ViewLibrary(string apiKey, string userId, Uri baseUri) { var request = new RestRequest @@ -142,29 +163,71 @@ namespace Ombi.Api TimeSpan.FromSeconds(5) }); - switch (type) + IRestResponse response = null; + try + { + + switch (type) + { + case EmbyMediaType.Movie: + response = policy.Execute(() => Api.Execute(request, baseUri)); + break; + + case EmbyMediaType.Series: + response = policy.Execute(() => Api.Execute(request, baseUri)); + break; + case EmbyMediaType.Music: + break; + case EmbyMediaType.Episode: + response = policy.Execute(() => Api.Execute(request, baseUri)); + break; + default: + throw new ArgumentOutOfRangeException(nameof(type), type, null); + } + + var info = new EmbyInformation(); + + switch (type) + { + case EmbyMediaType.Movie: + return new EmbyInformation + { + MovieInformation = JsonConvert.DeserializeObject(response.Content) + }; + case EmbyMediaType.Series: + return new EmbyInformation + { + SeriesInformation = JsonConvert.DeserializeObject(response.Content) + }; + case EmbyMediaType.Music: + break; + case EmbyMediaType.Episode: + return new EmbyInformation + { + EpisodeInformation = JsonConvert.DeserializeObject(response.Content) + }; + default: + throw new ArgumentOutOfRangeException(nameof(type), type, null); + } + + } + catch (Exception e) { - case EmbyMediaType.Movie: - return new EmbyInformation - { - MovieInformation = policy.Execute(() => Api.ExecuteJson(request, baseUri)) - }; - case EmbyMediaType.Series: - return new EmbyInformation - { - SeriesInformation = - policy.Execute(() => Api.ExecuteJson(request, baseUri)) - }; - case EmbyMediaType.Music: - break; - case EmbyMediaType.Episode: - return new EmbyInformation - { - EpisodeInformation = - policy.Execute(() => Api.ExecuteJson(request, baseUri)) - }; - default: - throw new ArgumentOutOfRangeException(nameof(type), type, null); + Log.Error("Could not get the media item's information"); + Log.Error(e); + Log.Debug("ResponseContent"); + Log.Debug(response?.Content ?? "Empty"); + Log.Debug("ResponseStatusCode"); + Log.Debug(response?.StatusCode ?? HttpStatusCode.PreconditionFailed); + + Log.Debug("ResponseError"); + Log.Debug(response?.ErrorMessage ?? "No Error"); + Log.Debug("ResponseException"); + Log.Debug(response?.ErrorException ?? new Exception()); + + + + throw; } return new EmbyInformation(); } diff --git a/Ombi.Api/TvMazeApi.cs b/Ombi.Api/TvMazeApi.cs index 5bb534990..4330a3d1e 100644 --- a/Ombi.Api/TvMazeApi.cs +++ b/Ombi.Api/TvMazeApi.cs @@ -28,6 +28,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Newtonsoft.Json; using NLog; using Ombi.Api.Models.Tv; using RestSharp; @@ -90,21 +91,29 @@ namespace Ombi.Api }; request.AddUrlSegment("id", theTvDbId.ToString()); request.AddHeader("Content-Type", "application/json"); + try + { + var result = Api.Execute(request, new Uri(Uri)); + var obj = JsonConvert.DeserializeObject(result.Content); - var obj = Api.Execute(request, new Uri(Uri)); - - var episodes = EpisodeLookup(obj.id).ToList(); + var episodes = EpisodeLookup(obj.id).ToList(); - foreach (var e in episodes) - { - obj.Season.Add(new TvMazeCustomSeason + foreach (var e in episodes) { - SeasonNumber = e.season, - EpisodeNumber = e.number - }); + obj.Season.Add(new TvMazeCustomSeason + { + SeasonNumber = e.season, + EpisodeNumber = e.number + }); + } + + return obj; } - - return obj; + catch (Exception e) + { + Log.Error(e); + return null; + } } public List GetSeasons(int id) diff --git a/Ombi.Core.Migration/Migrations/Version1100.cs b/Ombi.Core.Migration/Migrations/Version1100.cs index 4c184ca22..52d5139fb 100644 --- a/Ombi.Core.Migration/Migrations/Version1100.cs +++ b/Ombi.Core.Migration/Migrations/Version1100.cs @@ -181,7 +181,7 @@ namespace Ombi.Core.Migration.Migrations try { var settings = PlexSettings.GetSettings(); - if (string.IsNullOrEmpty(settings.PlexAuthToken)) + if (string.IsNullOrEmpty(settings.PlexAuthToken) || !settings.Enable) { return; } diff --git a/Ombi.Core.Migration/Migrations/Version195.cs b/Ombi.Core.Migration/Migrations/Version195.cs index 3dee6a9e5..832490f90 100644 --- a/Ombi.Core.Migration/Migrations/Version195.cs +++ b/Ombi.Core.Migration/Migrations/Version195.cs @@ -62,6 +62,7 @@ namespace Ombi.Core.Migration.Migrations private void UpdateApplicationSettings() { var plex = PlexRequestSettings.GetSettings(); + var jobSettings = Jobs.GetSettings(); var newsLetter = NewsletterSettings.GetSettings(); diff --git a/Ombi.Core.Migration/Migrations/Version2200.cs b/Ombi.Core.Migration/Migrations/Version2200.cs index 79939c8e3..de9f0d77e 100644 --- a/Ombi.Core.Migration/Migrations/Version2200.cs +++ b/Ombi.Core.Migration/Migrations/Version2200.cs @@ -68,8 +68,11 @@ namespace Ombi.Core.Migration.Migrations { #if !DEBUG var s = PlexSettings.GetSettings(); - s.Enable = true; - PlexSettings.SaveSettings(s); + if (!string.IsNullOrEmpty(s.Ip)) + { + s.Enable = true; + PlexSettings.SaveSettings(s); + } #endif } private void UpdateCustomSettings() diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs index ca65135e2..b78f64dcc 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/EmbyRecentlyAddedNewsletter.cs @@ -31,6 +31,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using NLog; using Ombi.Api; using Ombi.Api.Interfaces; @@ -41,6 +42,7 @@ using Ombi.Services.Jobs.Templates; using Ombi.Store.Models; using Ombi.Store.Models.Emby; using Ombi.Store.Repository; +using TMDbLib.Objects.Exceptions; using EmbyMediaType = Ombi.Store.Models.Plex.EmbyMediaType; namespace Ombi.Services.Jobs.RecentlyAddedNewsletter @@ -79,7 +81,7 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { try { - return GetHtml(test); + return GetHtml(test); } catch (Exception e) { @@ -196,32 +198,42 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter ""); foreach (var movie in orderedMovies) { + // We have a try within a try so we can catch the rate limit without ending the loop (finally block) try { - - var imdbId = movie.ProviderIds.Imdb; - var info = MovieApi.GetMovieInformation(imdbId).Result; - if (info == null) + try { - throw new Exception($"Movie with Imdb id {imdbId} returned null from the MovieApi"); - } - AddImageInsideTable(sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}"); - sb.Append(""); - sb.Append( - ""); + sb.Append( + "
"); + var imdbId = movie.ProviderIds.Imdb; + var info = MovieApi.GetMovieInformation(imdbId).Result; + if (info == null) + { + throw new Exception($"Movie with Imdb id {imdbId} returned null from the MovieApi"); + } + AddImageInsideTable(sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}"); - Href(sb, $"https://www.imdb.com/title/{info.ImdbId}/"); - Header(sb, 3, $"{info.Title} {info.ReleaseDate?.ToString("yyyy") ?? string.Empty}"); - EndTag(sb, "a"); + sb.Append("
"); + + Href(sb, $"https://www.imdb.com/title/{info.ImdbId}/"); + Header(sb, 3, $"{info.Title} {info.ReleaseDate?.ToString("yyyy") ?? string.Empty}"); + EndTag(sb, "a"); - if (info.Genres.Any()) + if (info.Genres.Any()) + { + AddParagraph(sb, + $"Genre: {string.Join(", ", info.Genres.Select(x => x.Name.ToString()).ToArray())}"); + } + + AddParagraph(sb, info.Overview); + } + catch (RequestLimitExceededException limit) { - AddParagraph(sb, - $"Genre: {string.Join(", ", info.Genres.Select(x => x.Name.ToString()).ToArray())}"); + // We have hit a limit, we need to now wait. + Thread.Sleep(TimeSpan.FromSeconds(10)); + Log.Info(limit); } - - AddParagraph(sb, info.Overview); } catch (Exception e) { @@ -260,8 +272,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter { var seriesItem = t.EmbyInformation.SeriesInformation; var relatedEpisodes = t.EpisodeInformation; - - + + try { var info = TvApi.ShowLookupByTheTvDbId(int.Parse(seriesItem.ProviderIds.Tvdb)); @@ -328,8 +340,8 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter sb.Append("


"); } - - + + private void EndLoopHtml(StringBuilder sb) { diff --git a/Ombi.UI/Modules/IssuesModule.cs b/Ombi.UI/Modules/IssuesModule.cs index 58897355a..e54962aba 100644 --- a/Ombi.UI/Modules/IssuesModule.cs +++ b/Ombi.UI/Modules/IssuesModule.cs @@ -145,7 +145,7 @@ namespace Ombi.UI.Modules Deleted = issue.Deleted, Type = issue.Type, ProviderId = issue.ProviderId, - PosterUrl = issue.PosterUrl, + PosterUrl = issue.PosterUrl.Contains("https://image.tmdb.org/t/p/w150/") ? issue.PosterUrl : $"https://image.tmdb.org/t/p/w150/{issue.PosterUrl}", Id = issue.Id }; return View["Details", m]; diff --git a/Ombi.UI/Modules/LandingPageModule.cs b/Ombi.UI/Modules/LandingPageModule.cs index 5f1076be8..d544af2c3 100644 --- a/Ombi.UI/Modules/LandingPageModule.cs +++ b/Ombi.UI/Modules/LandingPageModule.cs @@ -40,12 +40,15 @@ namespace Ombi.UI.Modules public class LandingPageModule : BaseModule { public LandingPageModule(ISettingsService settingsService, ISettingsService landing, - ISettingsService ps, IPlexApi pApi, IResourceLinker linker, ISecurityExtensions security) : base("landing", settingsService, security) + ISettingsService ps, IPlexApi pApi, IResourceLinker linker, ISecurityExtensions security, ISettingsService emby, + IEmbyApi embyApi) : base("landing", settingsService, security) { LandingSettings = landing; PlexSettings = ps; PlexApi = pApi; Linker = linker; + EmbySettings = emby; + EmbyApi = embyApi; Get["LandingPageIndex","/", true] = async (x, ct) => { @@ -75,26 +78,49 @@ namespace Ombi.UI.Modules private ISettingsService LandingSettings { get; } private ISettingsService PlexSettings { get; } + private ISettingsService EmbySettings { get; } private IPlexApi PlexApi { get; } + private IEmbyApi EmbyApi { get; } private IResourceLinker Linker { get; } private async Task CheckStatus() { var plexSettings = await PlexSettings.GetSettingsAsync(); - if (string.IsNullOrEmpty(plexSettings.PlexAuthToken) || string.IsNullOrEmpty(plexSettings.Ip)) + if (plexSettings.Enable) { - return Response.AsJson(false); - } - try - { - var status = PlexApi.GetStatus(plexSettings.PlexAuthToken, plexSettings.FullUri); - return Response.AsJson(status != null); + if (string.IsNullOrEmpty(plexSettings.PlexAuthToken) || string.IsNullOrEmpty(plexSettings.Ip)) + { + return Response.AsJson(false); + } + try + { + var status = PlexApi.GetStatus(plexSettings.PlexAuthToken, plexSettings.FullUri); + return Response.AsJson(status != null); + } + catch (Exception) + { + return Response.AsJson(false); + } } - catch (Exception) + + var emby = await EmbySettings.GetSettingsAsync(); + if (emby.Enable) { - return Response.AsJson(false); + if (string.IsNullOrEmpty(emby.AdministratorId) || string.IsNullOrEmpty(emby.Ip)) + { + return Response.AsJson(false); + } + try + { + var status = EmbyApi.GetSystemInformation(emby.ApiKey, emby.FullUri); + return Response.AsJson(status?.Version != null); + } + catch (Exception) + { + return Response.AsJson(false); + } } - + return Response.AsJson(false); } } } \ No newline at end of file diff --git a/Ombi.UI/Modules/SearchModule.cs b/Ombi.UI/Modules/SearchModule.cs index f11e41b55..2d4a12d49 100644 --- a/Ombi.UI/Modules/SearchModule.cs +++ b/Ombi.UI/Modules/SearchModule.cs @@ -437,6 +437,11 @@ namespace Ombi.UI.Modules { var show = anticipatedShow.Show; var theTvDbId = int.Parse(show.Ids.Tvdb.ToString()); + var result = TvApi.ShowLookupByTheTvDbId(theTvDbId); + if (result == null) + { + continue; + } var model = new SearchTvShowViewModel { @@ -466,6 +471,12 @@ namespace Ombi.UI.Modules { var show = watched.Show; var theTvDbId = int.Parse(show.Ids.Tvdb.ToString()); + var result = TvApi.ShowLookupByTheTvDbId(theTvDbId); + if (result == null) + { + continue; + } + var model = new SearchTvShowViewModel { FirstAired = show.FirstAired?.ToString("yyyy-MM-ddTHH:mm:ss"), @@ -494,6 +505,12 @@ namespace Ombi.UI.Modules { var show = watched.Show; var theTvDbId = int.Parse(show.Ids.Tvdb.ToString()); + var result = TvApi.ShowLookupByTheTvDbId(theTvDbId); + if (result == null) + { + continue; + } + var model = new SearchTvShowViewModel { FirstAired = show.FirstAired?.ToString("yyyy-MM-ddTHH:mm:ss"), diff --git a/Ombi.UI/Views/Issues/Details.cshtml b/Ombi.UI/Views/Issues/Details.cshtml index c5567209a..da025b9cc 100644 --- a/Ombi.UI/Views/Issues/Details.cshtml +++ b/Ombi.UI/Views/Issues/Details.cshtml @@ -18,7 +18,7 @@
- +

Issues For "@Model.Title"