Skip to content

Commit 3478ff8

Browse files
committed
优化 本地图片地址生成和识别
1 parent 6d2ba0d commit 3478ff8

File tree

4 files changed

+58
-61
lines changed

4 files changed

+58
-61
lines changed

Emby.Plugins.JavScraper/JavImageProvider.cs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using System.Web;
76
using Emby.Plugins.JavScraper.Scrapers;
87
using Emby.Plugins.JavScraper.Services;
98
using MediaBrowser.Common.Configuration;
109
using MediaBrowser.Common.Net;
11-
using MediaBrowser.Controller;
1210
using MediaBrowser.Controller.Entities;
1311
using MediaBrowser.Controller.Entities.Movies;
1412
using MediaBrowser.Controller.Library;
@@ -31,7 +29,6 @@ public class JavImageProvider : IRemoteImageProvider, IHasOrder
3129
{
3230
private readonly IProviderManager providerManager;
3331
private readonly ILibraryManager libraryManager;
34-
private readonly IServerApplicationHost serverApplicationHost;
3532
private readonly ImageProxyService imageProxyService;
3633
private readonly ILogger _logger;
3734
private readonly IJsonSerializer _jsonSerializer;
@@ -40,7 +37,6 @@ public class JavImageProvider : IRemoteImageProvider, IHasOrder
4037
public int Order => 3;
4138

4239
public JavImageProvider(IProviderManager providerManager, ILibraryManager libraryManager,
43-
IServerApplicationHost serverApplicationHost,
4440
#if __JELLYFIN__
4541
ILoggerFactory logManager,
4642
#else
@@ -51,7 +47,6 @@ public JavImageProvider(IProviderManager providerManager, ILibraryManager librar
5147
{
5248
this.providerManager = providerManager;
5349
this.libraryManager = libraryManager;
54-
this.serverApplicationHost = serverApplicationHost;
5550
#if __JELLYFIN__
5651
imageProxyService = Plugin.Instance.ImageProxyService;
5752
#else
@@ -65,34 +60,14 @@ public JavImageProvider(IProviderManager providerManager, ILibraryManager librar
6560
public string Name => Plugin.NAME;
6661

6762
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
68-
{
69-
// /emby/Plugins/JavScraper/Image?url=&type=xx
70-
var type = ImageType.Backdrop;
71-
if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) //本地的链接
72-
{
73-
var uri = new Uri(url);
74-
var q = HttpUtility.ParseQueryString(uri.Query);
75-
var url2 = q["url"];
76-
if (url2.IsWebUrl())
77-
{
78-
url = url2;
79-
var tt = q.Get("type");
80-
if (!string.IsNullOrWhiteSpace(tt) && Enum.TryParse<ImageType>(tt.Trim(), out var t2))
81-
type = t2;
82-
}
83-
}
84-
_logger?.Info($"{nameof(GetImageResponse)} {url}");
85-
return imageProxyService.GetImageResponse(url, type, cancellationToken);
86-
}
63+
=> imageProxyService.GetImageResponse(url, ImageType.Backdrop, cancellationToken);
8764

8865
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item,
8966
#if !__JELLYFIN__
9067
LibraryOptions libraryOptions,
9168
#endif
9269
CancellationToken cancellationToken)
9370
{
94-
// ImageUrl = $"/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}",
95-
9671
var list = new List<RemoteImageInfo>();
9772

9873
JavVideoIndex index = null;
@@ -111,30 +86,29 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item,
11186
if (string.IsNullOrWhiteSpace(m.Cover) && m.Samples?.Any() == true)
11287
m.Cover = m.Samples.FirstOrDefault();
11388

114-
var api_url = await serverApplicationHost.GetLocalApiUrl(default(CancellationToken));
115-
RemoteImageInfo Add(string url, ImageType type)
89+
async Task<RemoteImageInfo> Add(string url, ImageType type)
11690
{
11791
//http://127.0.0.1:{serverApplicationHost.HttpPort}
11892
var img = new RemoteImageInfo()
11993
{
12094
Type = type,
12195
ProviderName = Name,
122-
Url = $"{api_url}/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}&type={type}"
96+
Url = await imageProxyService.GetLocalUrl(url, type)
12397
};
12498
list.Add(img);
12599
return img;
126100
}
127101

128102
if (m.Cover.IsWebUrl())
129103
{
130-
Add(m.Cover, ImageType.Primary);
131-
Add(m.Cover, ImageType.Backdrop);
104+
await Add(m.Cover, ImageType.Primary);
105+
await Add(m.Cover, ImageType.Backdrop);
132106
}
133107

134108
if (m.Samples?.Any() == true)
135109
{
136110
foreach (var url in m.Samples.Where(o => o.IsWebUrl()))
137-
Add(url, ImageType.Screenshot);
111+
await Add(url, ImageType.Screenshot);
138112
}
139113

140114
return list;

Emby.Plugins.JavScraper/JavMovieProvider.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Emby.Plugins.JavScraper.Services;
1111
using MediaBrowser.Common.Configuration;
1212
using MediaBrowser.Common.Net;
13-
using MediaBrowser.Controller;
1413
using MediaBrowser.Controller.Entities;
1514
using MediaBrowser.Controller.Entities.Movies;
1615
using MediaBrowser.Controller.Providers;
@@ -30,7 +29,6 @@ namespace Emby.Plugins.JavScraper
3029
public class JavMovieProvider : IRemoteMetadataProvider<Movie, MovieInfo>, IHasOrder
3130
{
3231
private readonly ILogger _logger;
33-
private readonly IServerApplicationHost serverApplicationHost;
3432
private readonly TranslationService translationService;
3533
private readonly ImageProxyService imageProxyService;
3634
private readonly IProviderManager providerManager;
@@ -48,11 +46,9 @@ public JavMovieProvider(
4846
ImageProxyService imageProxyService,
4947
Gfriends gfriends,
5048
#endif
51-
IServerApplicationHost serverApplicationHost,
5249
IProviderManager providerManager, IJsonSerializer jsonSerializer, IApplicationPaths appPaths)
5350
{
5451
_logger = logManager.CreateLogger<JavMovieProvider>();
55-
this.serverApplicationHost = serverApplicationHost;
5652
#if __JELLYFIN__
5753
translationService = Plugin.Instance.TranslationService;
5854
imageProxyService = Plugin.Instance.ImageProxyService;
@@ -72,25 +68,7 @@ public JavMovieProvider(
7268
public string Name => Plugin.NAME;
7369

7470
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
75-
{
76-
// /emby/Plugins/JavScraper/Image?url=&type=xx
77-
var type = ImageType.Backdrop;
78-
if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) //本地的链接
79-
{
80-
var uri = new Uri(url);
81-
var q = HttpUtility.ParseQueryString(uri.Query);
82-
var url2 = q["url"];
83-
if (url2.IsWebUrl())
84-
{
85-
url = url2;
86-
var tt = q.Get("type");
87-
if (!string.IsNullOrWhiteSpace(tt) && Enum.TryParse<ImageType>(tt.Trim(), out var t2))
88-
type = t2;
89-
}
90-
}
91-
_logger?.Info($"{nameof(GetImageResponse)} {url}");
92-
return imageProxyService.GetImageResponse(url, type, cancellationToken);
93-
}
71+
=> imageProxyService.GetImageResponse(url, ImageType.Backdrop, cancellationToken);
9472

9573
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
9674
{
@@ -256,7 +234,6 @@ from p in ps.DefaultIfEmpty()
256234
if (!string.IsNullOrWhiteSpace(m.Studio))
257235
metadataResult.Item.AddStudio(m.Studio);
258236

259-
var api_url = await serverApplicationHost.GetLocalApiUrl(default(CancellationToken));
260237
var cut_persion_image = Plugin.Instance?.Configuration?.EnableCutPersonImage ?? true;
261238
var person_image_type = cut_persion_image ? ImageType.Primary : ImageType.Backdrop;
262239

@@ -277,7 +254,7 @@ async Task AddPerson(string personName,
277254
var url = await Gfriends.FindAsync(person.Name, cancellationToken);
278255
if (url.IsWebUrl())
279256
{
280-
person.ImageUrl = $"{api_url}/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(url)}type={person_image_type}";
257+
person.ImageUrl = await imageProxyService.GetLocalUrl(url, person_image_type);
281258
var person_index = new JavPersonIndex() { Provider = Gfriends.Name, Url = url, ImageType = person_image_type };
282259
person.SetJavPersonIndex(_jsonSerializer, person_index);
283260
}
@@ -333,14 +310,13 @@ public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo se
333310
.SelectMany(o => o)
334311
.ToList();
335312

336-
var api_url = await serverApplicationHost.GetLocalApiUrl(default(CancellationToken));
337313
foreach (var m in all)
338314
{
339315
var result = new RemoteSearchResult
340316
{
341317
Name = $"{m.Num} {m.Title}",
342318
ProductionYear = m.GetYear(),
343-
ImageUrl = $"/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(m.Cover)}",
319+
ImageUrl = await imageProxyService.GetLocalUrl(m.Cover, with_api_url: false),
344320
SearchProviderName = Name,
345321
PremiereDate = m.GetDate(),
346322
};

Emby.Plugins.JavScraper/Plugin.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Linq;
2222
using System.Collections.ObjectModel;
2323
using MediaBrowser.Common;
24+
using MediaBrowser.Controller;
2425

2526
namespace Emby.Plugins.JavScraper
2627
{
@@ -68,6 +69,7 @@ public class Plugin
6869
/// <param name="xmlSerializer"></param>
6970
/// <param name="logger"></param>
7071
public Plugin(IApplicationPaths applicationPaths, IApplicationHost applicationHost, IXmlSerializer xmlSerializer,
72+
IServerApplicationHost serverApplicationHost,
7173
#if __JELLYFIN__
7274
IServiceProvider serviceProvider,
7375
ILoggerFactory logManager
@@ -83,7 +85,7 @@ ILogManager logManager
8385
Scrapers = applicationHost.GetExports<AbstractScraper>(false).Where(o => o != null).ToList().AsReadOnly();
8486

8587
#if __JELLYFIN__
86-
ImageProxyService = new ImageProxyService(serviceProvider.GetService<IJsonSerializer>(), logManager.CreateLogger<ImageProxyService>(),
88+
ImageProxyService = new ImageProxyService(serverApplicationHost, serviceProvider.GetService<IJsonSerializer>(), logManager.CreateLogger<ImageProxyService>(),
8789
serviceProvider.GetService<MediaBrowser.Model.IO.IFileSystem>(), applicationPaths);
8890
TranslationService = new TranslationService(serviceProvider.GetService<IJsonSerializer>(), logManager.CreateLogger<TranslationService>());
8991
#endif

Emby.Plugins.JavScraper/Services/ImageProxyService.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System.Net.Http;
2121
using System.Threading;
2222
using System.Threading.Tasks;
23+
using MediaBrowser.Controller;
24+
using System.Web;
2325

2426
namespace Emby.Plugins.JavScraper.Services
2527
{
@@ -31,22 +33,65 @@ public class ImageProxyService
3133
private HttpClientEx client;
3234
private static FileExtensionContentTypeProvider fileExtensionContentTypeProvider = new FileExtensionContentTypeProvider();
3335

34-
public ImageProxyService(IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths)
36+
public ImageProxyService(IServerApplicationHost serverApplicationHost, IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths)
3537
{
3638
client = new HttpClientEx();
39+
this.serverApplicationHost = serverApplicationHost;
3740
this.jsonSerializer = jsonSerializer;
3841
this.logger = logger;
3942
this.fileSystem = fileSystem;
4043
this.appPaths = appPaths;
4144
}
4245

46+
private readonly IServerApplicationHost serverApplicationHost;
4347
private readonly IJsonSerializer jsonSerializer;
4448
private readonly ILogger logger;
4549
private readonly IFileSystem fileSystem;
4650
private readonly IApplicationPaths appPaths;
4751

52+
/// <summary>
53+
/// 构造本地url地址
54+
/// </summary>
55+
/// <param name="url"></param>
56+
/// <param name="type"></param>
57+
/// <param name="with_api_url">是否包含 api url</param>
58+
/// <returns></returns>
59+
public async Task<string> GetLocalUrl(string url, ImageType type = ImageType.Backdrop, bool with_api_url = true)
60+
{
61+
if (string.IsNullOrEmpty(url))
62+
return url;
63+
64+
if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0)
65+
return url;
66+
67+
var api_url = with_api_url ? await serverApplicationHost.GetLocalApiUrl(default(CancellationToken)) : string.Empty;
68+
return $"{api_url}/emby/Plugins/JavScraper/Image?url={HttpUtility.UrlEncode(url)}&type={type}";
69+
}
70+
71+
/// <summary>
72+
/// 获取图片
73+
/// </summary>
74+
/// <param name="url">地址</param>
75+
/// <param name="type">类型</param>
76+
/// <param name="cancellationToken"></param>
77+
/// <returns></returns>
4878
public async Task<HttpResponseInfo> GetImageResponse(string url, ImageType type, CancellationToken cancellationToken)
4979
{
80+
// /emby/Plugins/JavScraper/Image?url=&type=xx
81+
if (url.IndexOf("Plugins/JavScraper/Image", StringComparison.OrdinalIgnoreCase) >= 0) //本地的链接
82+
{
83+
var uri = new Uri(url);
84+
var q = HttpUtility.ParseQueryString(uri.Query);
85+
var url2 = q["url"];
86+
if (url2.IsWebUrl())
87+
{
88+
url = url2;
89+
var tt = q.Get("type");
90+
if (!string.IsNullOrWhiteSpace(tt) && Enum.TryParse<ImageType>(tt.Trim(), out var t2))
91+
type = t2;
92+
}
93+
}
94+
5095
logger?.Info($"{nameof(GetImageResponse)}-{url}");
5196

5297
var key = WebUtility.UrlEncode(url);

0 commit comments

Comments
 (0)