diff --git a/Emby.Plugins.JavScraper/JavImageProvider.cs b/Emby.Plugins.JavScraper/JavImageProvider.cs index 89c0255..17ac290 100644 --- a/Emby.Plugins.JavScraper/JavImageProvider.cs +++ b/Emby.Plugins.JavScraper/JavImageProvider.cs @@ -3,12 +3,10 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Web; using Emby.Plugins.JavScraper.Scrapers; using Emby.Plugins.JavScraper.Services; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Net; -using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Library; @@ -31,7 +29,6 @@ public class JavImageProvider : IRemoteImageProvider, IHasOrder { private readonly IProviderManager providerManager; private readonly ILibraryManager libraryManager; - private readonly IServerApplicationHost serverApplicationHost; private readonly ImageProxyService imageProxyService; private readonly ILogger _logger; private readonly IJsonSerializer _jsonSerializer; @@ -40,7 +37,6 @@ public class JavImageProvider : IRemoteImageProvider, IHasOrder public int Order => 3; public JavImageProvider(IProviderManager providerManager, ILibraryManager libraryManager, - IServerApplicationHost serverApplicationHost, #if __JELLYFIN__ ILoggerFactory logManager, #else @@ -51,7 +47,6 @@ public JavImageProvider(IProviderManager providerManager, ILibraryManager librar { this.providerManager = providerManager; this.libraryManager = libraryManager; - this.serverApplicationHost = serverApplicationHost; #if __JELLYFIN__ imageProxyService = Plugin.Instance.ImageProxyService; #else @@ -65,25 +60,7 @@ public JavImageProvider(IProviderManager providerManager, ILibraryManager librar public string Name => Plugin.NAME; public Task GetImageResponse(string url, CancellationToken cancellationToken) - { - // /emby/Plugins/JavScraper/Image?url=&type=xx - var type = ImageType.Backdrop; - if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) //本地的链接 - { - var uri = new Uri(url); - var q = HttpUtility.ParseQueryString(uri.Query); - var url2 = q["url"]; - if (url2.IsWebUrl()) - { - url = url2; - var tt = q.Get("type"); - if (!string.IsNullOrWhiteSpace(tt) && Enum.TryParse(tt.Trim(), out var t2)) - type = t2; - } - } - _logger?.Info($"{nameof(GetImageResponse)} {url}"); - return imageProxyService.GetImageResponse(url, type, cancellationToken); - } + => imageProxyService.GetImageResponse(url, ImageType.Backdrop, cancellationToken); public async Task> GetImages(BaseItem item, #if !__JELLYFIN__ @@ -91,8 +68,6 @@ public async Task> GetImages(BaseItem item, #endif CancellationToken cancellationToken) { - // ImageUrl = $"/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}", - var list = new List(); JavVideoIndex index = null; @@ -111,15 +86,14 @@ public async Task> GetImages(BaseItem item, if (string.IsNullOrWhiteSpace(m.Cover) && m.Samples?.Any() == true) m.Cover = m.Samples.FirstOrDefault(); - var api_url = await serverApplicationHost.GetLocalApiUrl(default(CancellationToken)); - RemoteImageInfo Add(string url, ImageType type) + async Task Add(string url, ImageType type) { //http://127.0.0.1:{serverApplicationHost.HttpPort} var img = new RemoteImageInfo() { Type = type, ProviderName = Name, - Url = $"{api_url}/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}&type={type}" + Url = await imageProxyService.GetLocalUrl(url, type) }; list.Add(img); return img; @@ -127,14 +101,14 @@ RemoteImageInfo Add(string url, ImageType type) if (m.Cover.IsWebUrl()) { - Add(m.Cover, ImageType.Primary); - Add(m.Cover, ImageType.Backdrop); + await Add(m.Cover, ImageType.Primary); + await Add(m.Cover, ImageType.Backdrop); } if (m.Samples?.Any() == true) { foreach (var url in m.Samples.Where(o => o.IsWebUrl())) - Add(url, ImageType.Screenshot); + await Add(url, ImageType.Screenshot); } return list; diff --git a/Emby.Plugins.JavScraper/JavMovieProvider.cs b/Emby.Plugins.JavScraper/JavMovieProvider.cs index fd959a8..d35775a 100644 --- a/Emby.Plugins.JavScraper/JavMovieProvider.cs +++ b/Emby.Plugins.JavScraper/JavMovieProvider.cs @@ -10,7 +10,6 @@ using Emby.Plugins.JavScraper.Services; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Net; -using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Providers; @@ -30,7 +29,6 @@ namespace Emby.Plugins.JavScraper public class JavMovieProvider : IRemoteMetadataProvider, IHasOrder { private readonly ILogger _logger; - private readonly IServerApplicationHost serverApplicationHost; private readonly TranslationService translationService; private readonly ImageProxyService imageProxyService; private readonly IProviderManager providerManager; @@ -48,11 +46,9 @@ public JavMovieProvider( ImageProxyService imageProxyService, Gfriends gfriends, #endif - IServerApplicationHost serverApplicationHost, IProviderManager providerManager, IJsonSerializer jsonSerializer, IApplicationPaths appPaths) { _logger = logManager.CreateLogger(); - this.serverApplicationHost = serverApplicationHost; #if __JELLYFIN__ translationService = Plugin.Instance.TranslationService; imageProxyService = Plugin.Instance.ImageProxyService; @@ -72,25 +68,7 @@ public JavMovieProvider( public string Name => Plugin.NAME; public Task GetImageResponse(string url, CancellationToken cancellationToken) - { - // /emby/Plugins/JavScraper/Image?url=&type=xx - var type = ImageType.Backdrop; - if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) //本地的链接 - { - var uri = new Uri(url); - var q = HttpUtility.ParseQueryString(uri.Query); - var url2 = q["url"]; - if (url2.IsWebUrl()) - { - url = url2; - var tt = q.Get("type"); - if (!string.IsNullOrWhiteSpace(tt) && Enum.TryParse(tt.Trim(), out var t2)) - type = t2; - } - } - _logger?.Info($"{nameof(GetImageResponse)} {url}"); - return imageProxyService.GetImageResponse(url, type, cancellationToken); - } + => imageProxyService.GetImageResponse(url, ImageType.Backdrop, cancellationToken); public async Task> GetMetadata(MovieInfo info, CancellationToken cancellationToken) { @@ -256,7 +234,6 @@ from p in ps.DefaultIfEmpty() if (!string.IsNullOrWhiteSpace(m.Studio)) metadataResult.Item.AddStudio(m.Studio); - var api_url = await serverApplicationHost.GetLocalApiUrl(default(CancellationToken)); var cut_persion_image = Plugin.Instance?.Configuration?.EnableCutPersonImage ?? true; var person_image_type = cut_persion_image ? ImageType.Primary : ImageType.Backdrop; @@ -277,7 +254,7 @@ async Task AddPerson(string personName, var url = await Gfriends.FindAsync(person.Name, cancellationToken); if (url.IsWebUrl()) { - person.ImageUrl = $"{api_url}/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(url)}type={person_image_type}"; + person.ImageUrl = await imageProxyService.GetLocalUrl(url, person_image_type); var person_index = new JavPersonIndex() { Provider = Gfriends.Name, Url = url, ImageType = person_image_type }; person.SetJavPersonIndex(_jsonSerializer, person_index); } @@ -333,14 +310,13 @@ public async Task> GetSearchResults(MovieInfo se .SelectMany(o => o) .ToList(); - var api_url = await serverApplicationHost.GetLocalApiUrl(default(CancellationToken)); foreach (var m in all) { var result = new RemoteSearchResult { Name = $"{m.Num} {m.Title}", ProductionYear = m.GetYear(), - ImageUrl = $"/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}", + ImageUrl = await imageProxyService.GetLocalUrl(m.Cover, with_api_url: false), SearchProviderName = Name, PremiereDate = m.GetDate(), }; diff --git a/Emby.Plugins.JavScraper/Plugin.cs b/Emby.Plugins.JavScraper/Plugin.cs index 3092373..dcc5314 100644 --- a/Emby.Plugins.JavScraper/Plugin.cs +++ b/Emby.Plugins.JavScraper/Plugin.cs @@ -21,6 +21,7 @@ using System.Linq; using System.Collections.ObjectModel; using MediaBrowser.Common; +using MediaBrowser.Controller; namespace Emby.Plugins.JavScraper { @@ -68,6 +69,7 @@ public class Plugin /// /// public Plugin(IApplicationPaths applicationPaths, IApplicationHost applicationHost, IXmlSerializer xmlSerializer, + IServerApplicationHost serverApplicationHost, #if __JELLYFIN__ IServiceProvider serviceProvider, ILoggerFactory logManager @@ -83,7 +85,7 @@ ILogManager logManager Scrapers = applicationHost.GetExports(false).Where(o => o != null).ToList().AsReadOnly(); #if __JELLYFIN__ - ImageProxyService = new ImageProxyService(serviceProvider.GetService(), logManager.CreateLogger(), + ImageProxyService = new ImageProxyService(serverApplicationHost, serviceProvider.GetService(), logManager.CreateLogger(), serviceProvider.GetService(), applicationPaths); TranslationService = new TranslationService(serviceProvider.GetService(), logManager.CreateLogger()); #endif diff --git a/Emby.Plugins.JavScraper/Services/ImageProxyService.cs b/Emby.Plugins.JavScraper/Services/ImageProxyService.cs index 2623084..88bd7e0 100644 --- a/Emby.Plugins.JavScraper/Services/ImageProxyService.cs +++ b/Emby.Plugins.JavScraper/Services/ImageProxyService.cs @@ -20,6 +20,8 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Controller; +using System.Web; namespace Emby.Plugins.JavScraper.Services { @@ -31,22 +33,65 @@ public class ImageProxyService private HttpClientEx client; private static FileExtensionContentTypeProvider fileExtensionContentTypeProvider = new FileExtensionContentTypeProvider(); - public ImageProxyService(IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths) + public ImageProxyService(IServerApplicationHost serverApplicationHost, IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths) { client = new HttpClientEx(); + this.serverApplicationHost = serverApplicationHost; this.jsonSerializer = jsonSerializer; this.logger = logger; this.fileSystem = fileSystem; this.appPaths = appPaths; } + private readonly IServerApplicationHost serverApplicationHost; private readonly IJsonSerializer jsonSerializer; private readonly ILogger logger; private readonly IFileSystem fileSystem; private readonly IApplicationPaths appPaths; + /// + /// 构造本地url地址 + /// + /// + /// + /// 是否包含 api url + /// + public async Task GetLocalUrl(string url, ImageType type = ImageType.Backdrop, bool with_api_url = true) + { + if (string.IsNullOrEmpty(url)) + return url; + + if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) + return url; + + var api_url = with_api_url ? await serverApplicationHost.GetLocalApiUrl(default(CancellationToken)) : string.Empty; + return $"{api_url}/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(url)}&type={type}"; + } + + /// + /// 获取图片 + /// + /// 地址 + /// 类型 + /// + /// public async Task GetImageResponse(string url, ImageType type, CancellationToken cancellationToken) { + // /emby/Plugins/JavScraper/Image?url=&type=xx + if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) //本地的链接 + { + var uri = new Uri(url); + var q = HttpUtility.ParseQueryString(uri.Query); + var url2 = q["url"]; + if (url2.IsWebUrl()) + { + url = url2; + var tt = q.Get("type"); + if (!string.IsNullOrWhiteSpace(tt) && Enum.TryParse(tt.Trim(), out var t2)) + type = t2; + } + } + logger?.Info($"{nameof(GetImageResponse)}-{url}"); var key = WebUtility.UrlEncode(url);