Skip to content

Commit

Permalink
优化 本地图片地址生成和识别
Browse files Browse the repository at this point in the history
  • Loading branch information
JavScraper committed May 29, 2021
1 parent 6d2ba0d commit 3478ff8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 61 deletions.
38 changes: 6 additions & 32 deletions Emby.Plugins.JavScraper/JavImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -65,34 +60,14 @@ public JavImageProvider(IProviderManager providerManager, ILibraryManager librar
public string Name => Plugin.NAME;

public Task<HttpResponseInfo> 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<ImageType>(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<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item,
#if !__JELLYFIN__
LibraryOptions libraryOptions,
#endif
CancellationToken cancellationToken)
{
// ImageUrl = $"/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}",

var list = new List<RemoteImageInfo>();

JavVideoIndex index = null;
Expand All @@ -111,30 +86,29 @@ public async Task<IEnumerable<RemoteImageInfo>> 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<RemoteImageInfo> 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;
}

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;
Expand Down
30 changes: 3 additions & 27 deletions Emby.Plugins.JavScraper/JavMovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +29,6 @@ namespace Emby.Plugins.JavScraper
public class JavMovieProvider : IRemoteMetadataProvider<Movie, MovieInfo>, IHasOrder
{
private readonly ILogger _logger;
private readonly IServerApplicationHost serverApplicationHost;
private readonly TranslationService translationService;
private readonly ImageProxyService imageProxyService;
private readonly IProviderManager providerManager;
Expand All @@ -48,11 +46,9 @@ public JavMovieProvider(
ImageProxyService imageProxyService,
Gfriends gfriends,
#endif
IServerApplicationHost serverApplicationHost,
IProviderManager providerManager, IJsonSerializer jsonSerializer, IApplicationPaths appPaths)
{
_logger = logManager.CreateLogger<JavMovieProvider>();
this.serverApplicationHost = serverApplicationHost;
#if __JELLYFIN__
translationService = Plugin.Instance.TranslationService;
imageProxyService = Plugin.Instance.ImageProxyService;
Expand All @@ -72,25 +68,7 @@ public JavMovieProvider(
public string Name => Plugin.NAME;

public Task<HttpResponseInfo> 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<ImageType>(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<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -333,14 +310,13 @@ public async Task<IEnumerable<RemoteSearchResult>> 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(),
};
Expand Down
4 changes: 3 additions & 1 deletion Emby.Plugins.JavScraper/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Linq;
using System.Collections.ObjectModel;
using MediaBrowser.Common;
using MediaBrowser.Controller;

namespace Emby.Plugins.JavScraper
{
Expand Down Expand Up @@ -68,6 +69,7 @@ public class Plugin
/// <param name="xmlSerializer"></param>
/// <param name="logger"></param>
public Plugin(IApplicationPaths applicationPaths, IApplicationHost applicationHost, IXmlSerializer xmlSerializer,
IServerApplicationHost serverApplicationHost,
#if __JELLYFIN__
IServiceProvider serviceProvider,
ILoggerFactory logManager
Expand All @@ -83,7 +85,7 @@ ILogManager logManager
Scrapers = applicationHost.GetExports<AbstractScraper>(false).Where(o => o != null).ToList().AsReadOnly();

#if __JELLYFIN__
ImageProxyService = new ImageProxyService(serviceProvider.GetService<IJsonSerializer>(), logManager.CreateLogger<ImageProxyService>(),
ImageProxyService = new ImageProxyService(serverApplicationHost, serviceProvider.GetService<IJsonSerializer>(), logManager.CreateLogger<ImageProxyService>(),
serviceProvider.GetService<MediaBrowser.Model.IO.IFileSystem>(), applicationPaths);
TranslationService = new TranslationService(serviceProvider.GetService<IJsonSerializer>(), logManager.CreateLogger<TranslationService>());
#endif
Expand Down
47 changes: 46 additions & 1 deletion Emby.Plugins.JavScraper/Services/ImageProxyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;

/// <summary>
/// 构造本地url地址
/// </summary>
/// <param name="url"></param>
/// <param name="type"></param>
/// <param name="with_api_url">是否包含 api url</param>
/// <returns></returns>
public async Task<string> 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}";
}

/// <summary>
/// 获取图片
/// </summary>
/// <param name="url">地址</param>
/// <param name="type">类型</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<HttpResponseInfo> 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<ImageType>(tt.Trim(), out var t2))
type = t2;
}
}

logger?.Info($"{nameof(GetImageResponse)}-{url}");

var key = WebUtility.UrlEncode(url);
Expand Down

0 comments on commit 3478ff8

Please sign in to comment.