-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAniListSeriesProvider.cs
170 lines (148 loc) · 6.36 KB
/
AniListSeriesProvider.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
//API v2
namespace Jellyfin.Plugin.Anime.Providers.AniList
{
public class AniListSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder
{
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _paths;
private readonly ILogger _log;
private readonly AniListApi _aniListApi;
public int Order => -2;
public string Name => "AniList";
public AniListSeriesProvider(IApplicationPaths appPaths, IHttpClient httpClient, ILogger<AniListSeriesProvider> logger, IJsonSerializer jsonSerializer)
{
_log = logger;
_httpClient = httpClient;
_aniListApi = new AniListApi(jsonSerializer);
_paths = appPaths;
}
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<Series>();
var aid = info.ProviderIds.GetOrDefault(ProviderNames.AniList);
if (string.IsNullOrEmpty(aid))
{
_log.LogInformation("Start AniList... Searching({Name})", info.Name);
aid = await _aniListApi.FindSeries(info.Name, cancellationToken);
}
if (!string.IsNullOrEmpty(aid))
{
RootObject WebContent = await _aniListApi.WebRequestAPI(_aniListApi.AniList_anime_link.Replace("{0}", aid));
result.Item = new Series();
result.HasMetadata = true;
result.People = await _aniListApi.GetPersonInfo(WebContent.data.Media.id, cancellationToken);
result.Item.ProviderIds.Add(ProviderNames.AniList, aid);
result.Item.Overview = WebContent.data.Media.description;
try
{
//AniList has a max rating of 5
result.Item.CommunityRating = (WebContent.data.Media.averageScore/10);
}
catch (Exception) { }
foreach (var genre in _aniListApi.Get_Genre(WebContent))
result.Item.AddGenre(genre);
GenreHelper.CleanupGenres(result.Item);
StoreImageUrl(aid, WebContent.data.Media.coverImage.large, "image");
}
return result;
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
{
var results = new Dictionary<string, RemoteSearchResult>();
var aid = searchInfo.ProviderIds.GetOrDefault(ProviderNames.AniList);
if (!string.IsNullOrEmpty(aid))
{
if (!results.ContainsKey(aid))
{
results.Add(aid, await _aniListApi.GetAnime(aid).ConfigureAwait(false));
}
}
if (!string.IsNullOrEmpty(searchInfo.Name))
{
List<string> ids = await _aniListApi.Search_GetSeries_list(searchInfo.Name, cancellationToken).ConfigureAwait(false);
foreach (string a in ids)
{
results.Add(a, await _aniListApi.GetAnime(a).ConfigureAwait(false));
}
}
return results.Values;
}
private void StoreImageUrl(string series, string url, string type)
{
var path = Path.Combine(_paths.CachePath, "anilist", type, series + ".txt");
var directory = Path.GetDirectoryName(path);
Directory.CreateDirectory(directory);
File.WriteAllText(path, url);
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
UserAgent = Constants.UserAgent,
CancellationToken = cancellationToken,
Url = url
});
}
}
public class AniListSeriesImageProvider : IRemoteImageProvider
{
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _appPaths;
private readonly AniListApi _aniListApi;
public AniListSeriesImageProvider(IHttpClient httpClient, IApplicationPaths appPaths, IJsonSerializer jsonSerializer)
{
_httpClient = httpClient;
_appPaths = appPaths;
_aniListApi = new AniListApi(jsonSerializer);
}
public string Name => "AniList";
public bool Supports(BaseItem item) => item is Series || item is Season;
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new[] { ImageType.Primary };
}
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var seriesId = item.GetProviderId(ProviderNames.AniList);
return GetImages(seriesId, cancellationToken);
}
public async Task<IEnumerable<RemoteImageInfo>> GetImages(string aid, CancellationToken cancellationToken)
{
var list = new List<RemoteImageInfo>();
if (!string.IsNullOrEmpty(aid))
{
var primary = _aniListApi.Get_ImageUrl(await _aniListApi.WebRequestAPI(_aniListApi.AniList_anime_link.Replace("{0}", aid)));
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Type = ImageType.Primary,
Url = primary
});
}
return list;
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
UserAgent = Constants.UserAgent,
CancellationToken = cancellationToken,
Url = url
});
}
}
}