From b026772764a705e1ef49fa9b3be053f45a293968 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Wed, 17 Jul 2024 09:48:31 -0400 Subject: [PATCH] Move external url listing to provider for plugin use (#12279) --- .../ApplicationHost.cs | 3 +- MediaBrowser.Controller/Entities/BaseItem.cs | 5 -- .../Entities/Movies/Movie.cs | 18 ------ .../Entities/TV/Episode.cs | 17 ------ MediaBrowser.Controller/Entities/TV/Series.cs | 17 ------ MediaBrowser.Controller/Entities/Trailer.cs | 17 ------ .../LiveTv/LiveTvProgram.cs | 20 ------ .../Providers/IExternalId.cs | 2 + .../Providers/IExternalUrlProvider.cs | 22 +++++++ .../Providers/IProviderManager.cs | 4 +- .../Providers/ExternalIdInfo.cs | 5 ++ .../Manager/ProviderManager.cs | 61 +++++++++++-------- .../Manager/ProviderManagerTests.cs | 6 +- 13 files changed, 73 insertions(+), 124 deletions(-) create mode 100644 MediaBrowser.Controller/Providers/IExternalUrlProvider.cs diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index c394b25bd..5bf9c4fc2 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -664,7 +664,8 @@ namespace Emby.Server.Implementations GetExports(), GetExports(), GetExports(), - GetExports()); + GetExports(), + GetExports()); Resolve().AddParts(GetExports()); } diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 8bd4fb4f3..68ae67d05 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -2497,11 +2497,6 @@ namespace MediaBrowser.Controller.Entities return new[] { Id }; } - public virtual List GetRelatedUrls() - { - return new List(); - } - public virtual double? GetRefreshProgress() { return null; diff --git a/MediaBrowser.Controller/Entities/Movies/Movie.cs b/MediaBrowser.Controller/Entities/Movies/Movie.cs index ede544eec..710b05e7f 100644 --- a/MediaBrowser.Controller/Entities/Movies/Movie.cs +++ b/MediaBrowser.Controller/Entities/Movies/Movie.cs @@ -121,23 +121,5 @@ namespace MediaBrowser.Controller.Entities.Movies return hasChanges; } - - /// - public override List GetRelatedUrls() - { - var list = base.GetRelatedUrls(); - - var imdbId = this.GetProviderId(MetadataProvider.Imdb); - if (!string.IsNullOrEmpty(imdbId)) - { - list.Add(new ExternalUrl - { - Name = "Trakt", - Url = string.Format(CultureInfo.InvariantCulture, "https://trakt.tv/movies/{0}", imdbId) - }); - } - - return list; - } } } diff --git a/MediaBrowser.Controller/Entities/TV/Episode.cs b/MediaBrowser.Controller/Entities/TV/Episode.cs index 37e241414..5c54f014c 100644 --- a/MediaBrowser.Controller/Entities/TV/Episode.cs +++ b/MediaBrowser.Controller/Entities/TV/Episode.cs @@ -344,22 +344,5 @@ namespace MediaBrowser.Controller.Entities.TV return hasChanges; } - - public override List GetRelatedUrls() - { - var list = base.GetRelatedUrls(); - - var imdbId = this.GetProviderId(MetadataProvider.Imdb); - if (!string.IsNullOrEmpty(imdbId)) - { - list.Add(new ExternalUrl - { - Name = "Trakt", - Url = string.Format(CultureInfo.InvariantCulture, "https://trakt.tv/episodes/{0}", imdbId) - }); - } - - return list; - } } } diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index 6297b67e4..69b04a927 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -482,22 +482,5 @@ namespace MediaBrowser.Controller.Entities.TV return hasChanges; } - - public override List GetRelatedUrls() - { - var list = base.GetRelatedUrls(); - - var imdbId = this.GetProviderId(MetadataProvider.Imdb); - if (!string.IsNullOrEmpty(imdbId)) - { - list.Add(new ExternalUrl - { - Name = "Trakt", - Url = string.Format(CultureInfo.InvariantCulture, "https://trakt.tv/shows/{0}", imdbId) - }); - } - - return list; - } } } diff --git a/MediaBrowser.Controller/Entities/Trailer.cs b/MediaBrowser.Controller/Entities/Trailer.cs index 81d50bbc1..939709215 100644 --- a/MediaBrowser.Controller/Entities/Trailer.cs +++ b/MediaBrowser.Controller/Entities/Trailer.cs @@ -80,22 +80,5 @@ namespace MediaBrowser.Controller.Entities return hasChanges; } - - public override List GetRelatedUrls() - { - var list = base.GetRelatedUrls(); - - var imdbId = this.GetProviderId(MetadataProvider.Imdb); - if (!string.IsNullOrEmpty(imdbId)) - { - list.Add(new ExternalUrl - { - Name = "Trakt", - Url = string.Format(CultureInfo.InvariantCulture, "https://trakt.tv/movies/{0}", imdbId) - }); - } - - return list; - } } } diff --git a/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs b/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs index 05540d490..2ac6f9963 100644 --- a/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs +++ b/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs @@ -254,25 +254,5 @@ namespace MediaBrowser.Controller.LiveTv return name; } - - public override List GetRelatedUrls() - { - var list = base.GetRelatedUrls(); - - var imdbId = this.GetProviderId(MetadataProvider.Imdb); - if (!string.IsNullOrEmpty(imdbId)) - { - if (IsMovie) - { - list.Add(new ExternalUrl - { - Name = "Trakt", - Url = string.Format(CultureInfo.InvariantCulture, "https://trakt.tv/movies/{0}", imdbId) - }); - } - } - - return list; - } } } diff --git a/MediaBrowser.Controller/Providers/IExternalId.cs b/MediaBrowser.Controller/Providers/IExternalId.cs index 0d847520d..f451eac6d 100644 --- a/MediaBrowser.Controller/Providers/IExternalId.cs +++ b/MediaBrowser.Controller/Providers/IExternalId.cs @@ -1,3 +1,4 @@ +using System; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Providers; @@ -33,6 +34,7 @@ namespace MediaBrowser.Controller.Providers /// /// Gets the URL format string for this id. /// + [Obsolete("Obsolete in 10.10, to be removed in 10.11")] string? UrlFormatString { get; } /// diff --git a/MediaBrowser.Controller/Providers/IExternalUrlProvider.cs b/MediaBrowser.Controller/Providers/IExternalUrlProvider.cs new file mode 100644 index 000000000..86a180627 --- /dev/null +++ b/MediaBrowser.Controller/Providers/IExternalUrlProvider.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using MediaBrowser.Controller.Entities; + +namespace MediaBrowser.Controller.Providers; + +/// +/// Interface to include related urls for an item. +/// +public interface IExternalUrlProvider +{ + /// + /// Gets the external service name. + /// + string Name { get; } + + /// + /// Get the list of external urls. + /// + /// The item to get external urls for. + /// The list of external urls. + IEnumerable GetExternalUrls(BaseItem item); +} diff --git a/MediaBrowser.Controller/Providers/IProviderManager.cs b/MediaBrowser.Controller/Providers/IProviderManager.cs index b52f16edc..38fc5f2cc 100644 --- a/MediaBrowser.Controller/Providers/IProviderManager.cs +++ b/MediaBrowser.Controller/Providers/IProviderManager.cs @@ -99,12 +99,14 @@ namespace MediaBrowser.Controller.Providers /// Metadata providers to use. /// Metadata savers to use. /// External IDs to use. + /// The list of external url providers. void AddParts( IEnumerable imageProviders, IEnumerable metadataServices, IEnumerable metadataProviders, IEnumerable metadataSavers, - IEnumerable externalIds); + IEnumerable externalIds, + IEnumerable externalUrlProviders); /// /// Gets the available remote images. diff --git a/MediaBrowser.Model/Providers/ExternalIdInfo.cs b/MediaBrowser.Model/Providers/ExternalIdInfo.cs index d026d574f..1f5163aa8 100644 --- a/MediaBrowser.Model/Providers/ExternalIdInfo.cs +++ b/MediaBrowser.Model/Providers/ExternalIdInfo.cs @@ -1,3 +1,5 @@ +using System; + namespace MediaBrowser.Model.Providers { /// @@ -17,7 +19,9 @@ namespace MediaBrowser.Model.Providers Name = name; Key = key; Type = type; +#pragma warning disable CS0618 // Type or member is obsolete - Remove 10.11 UrlFormatString = urlFormatString; +#pragma warning restore CS0618 // Type or member is obsolete } /// @@ -46,6 +50,7 @@ namespace MediaBrowser.Model.Providers /// /// Gets or sets the URL format string. /// + [Obsolete("Obsolete in 10.10, to be removed in 10.11")] public string? UrlFormatString { get; set; } } } diff --git a/MediaBrowser.Providers/Manager/ProviderManager.cs b/MediaBrowser.Providers/Manager/ProviderManager.cs index f2ca99da6..60d89a51b 100644 --- a/MediaBrowser.Providers/Manager/ProviderManager.cs +++ b/MediaBrowser.Providers/Manager/ProviderManager.cs @@ -69,11 +69,12 @@ namespace MediaBrowser.Providers.Manager o.PoolInitialFill = 1; }); - private IImageProvider[] _imageProviders = Array.Empty(); - private IMetadataService[] _metadataServices = Array.Empty(); - private IMetadataProvider[] _metadataProviders = Array.Empty(); - private IMetadataSaver[] _savers = Array.Empty(); - private IExternalId[] _externalIds = Array.Empty(); + private IImageProvider[] _imageProviders = []; + private IMetadataService[] _metadataServices = []; + private IMetadataProvider[] _metadataProviders = []; + private IMetadataSaver[] _savers = []; + private IExternalId[] _externalIds = []; + private IExternalUrlProvider[] _externalUrlProviders = []; private bool _isProcessingRefreshQueue; private bool _disposed; @@ -132,12 +133,14 @@ namespace MediaBrowser.Providers.Manager IEnumerable metadataServices, IEnumerable metadataProviders, IEnumerable metadataSavers, - IEnumerable externalIds) + IEnumerable externalIds, + IEnumerable externalUrlProviders) { _imageProviders = imageProviders.ToArray(); _metadataServices = metadataServices.OrderBy(i => i.Order).ToArray(); _metadataProviders = metadataProviders.ToArray(); _externalIds = externalIds.OrderBy(i => i.ProviderName).ToArray(); + _externalUrlProviders = externalUrlProviders.OrderBy(i => i.Name).ToArray(); _savers = metadataSavers.ToArray(); } @@ -877,31 +880,35 @@ namespace MediaBrowser.Providers.Manager /// public IEnumerable GetExternalUrls(BaseItem item) { - return GetExternalIds(item) +#pragma warning disable CS0618 // Type or member is obsolete - Remove 10.11 + var legacyExternalIdUrls = GetExternalIds(item) .Select(i => - { - if (string.IsNullOrEmpty(i.UrlFormatString)) { - return null; - } + var urlFormatString = i.UrlFormatString; + if (string.IsNullOrEmpty(urlFormatString) + || !item.TryGetProviderId(i.Key, out var providerId)) + { + return null; + } - var value = item.GetProviderId(i.Key); + return new ExternalUrl + { + Name = i.ProviderName, + Url = string.Format( + CultureInfo.InvariantCulture, + urlFormatString, + providerId) + }; + }) + .OfType(); +#pragma warning restore CS0618 // Type or member is obsolete - if (string.IsNullOrEmpty(value)) - { - return null; - } + var externalUrls = _externalUrlProviders + .SelectMany(p => p + .GetExternalUrls(item) + .Select(externalUrl => new ExternalUrl { Name = p.Name, Url = externalUrl })); - return new ExternalUrl - { - Name = i.ProviderName, - Url = string.Format( - CultureInfo.InvariantCulture, - i.UrlFormatString, - value) - }; - }).Where(i => i is not null) - .Concat(item.GetRelatedUrls())!; // We just filtered out all the nulls + return legacyExternalIdUrls.Concat(externalUrls).OrderBy(u => u.Name); } /// @@ -912,7 +919,9 @@ namespace MediaBrowser.Providers.Manager name: i.ProviderName, key: i.Key, type: i.Type, +#pragma warning disable CS0618 // Type or member is obsolete - Remove 10.11 urlFormatString: i.UrlFormatString)); +#pragma warning restore CS0618 // Type or member is obsolete } /// diff --git a/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs b/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs index 6fccce049..cced2b1e2 100644 --- a/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs +++ b/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs @@ -585,15 +585,17 @@ namespace Jellyfin.Providers.Tests.Manager IEnumerable? metadataServices = null, IEnumerable? metadataProviders = null, IEnumerable? metadataSavers = null, - IEnumerable? externalIds = null) + IEnumerable? externalIds = null, + IEnumerable? externalUrlProviders = null) { imageProviders ??= Array.Empty(); metadataServices ??= Array.Empty(); metadataProviders ??= Array.Empty(); metadataSavers ??= Array.Empty(); externalIds ??= Array.Empty(); + externalUrlProviders ??= Array.Empty(); - providerManager.AddParts(imageProviders, metadataServices, metadataProviders, metadataSavers, externalIds); + providerManager.AddParts(imageProviders, metadataServices, metadataProviders, metadataSavers, externalIds, externalUrlProviders); } ///