forked from jellyfin-archive/jellyfin-plugin-anime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAniDbImageProvider.cs
110 lines (95 loc) · 3.72 KB
/
AniDbImageProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
namespace Jellyfin.Plugin.Anime.Providers.AniDB.Metadata
{
public class AniDbImageProvider : IRemoteImageProvider
{
public string Name => "AniDB";
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _appPaths;
public AniDbImageProvider(IHttpClient httpClient, IApplicationPaths appPaths)
{
_httpClient = httpClient;
_appPaths = appPaths;
}
public async Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
await AniDbSeriesProvider.RequestLimiter.Tick().ConfigureAwait(false);
return await _httpClient.GetResponse(new HttpRequestOptions
{
UserAgent = Constants.UserAgent,
CancellationToken = cancellationToken,
Url = url
}).ConfigureAwait(false);
}
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var seriesId = item.GetProviderId(ProviderNames.AniDb);
return GetImages(seriesId, cancellationToken);
}
public async Task<IEnumerable<RemoteImageInfo>> GetImages(string aniDbId, CancellationToken cancellationToken)
{
var list = new List<RemoteImageInfo>();
if (!string.IsNullOrEmpty(aniDbId))
{
var seriesDataPath = await AniDbSeriesProvider.GetSeriesData(_appPaths, _httpClient, aniDbId, cancellationToken);
var imageUrl = await FindImageUrl(seriesDataPath).ConfigureAwait(false);
if (!string.IsNullOrEmpty(imageUrl))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = imageUrl
});
}
}
return list;
}
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new[] { ImageType.Primary };
}
public bool Supports(BaseItem item)
{
return item is Series || item is Season || item is Movie;
}
private async Task<string> FindImageUrl(string seriesDataPath)
{
var settings = new XmlReaderSettings
{
Async = true,
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true,
ValidationType = ValidationType.None
};
using (var streamReader = new StreamReader(seriesDataPath, Encoding.UTF8))
{
using (XmlReader reader = XmlReader.Create(streamReader, settings))
{
await reader.MoveToContentAsync().ConfigureAwait(false);
while (await reader.ReadAsync().ConfigureAwait(false))
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "picture")
{
return "https://cdn.anidb.net/images/main/" + reader.ReadElementContentAsString();
}
}
}
}
return null;
}
}
}