-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAniListApi.cs
386 lines (359 loc) · 11.7 KB
/
AniListApi.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Anime.Configuration;
using Jellyfin.Plugin.Anime.Providers.AniList;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
namespace Jellyfin.Plugin.Anime.Providers.AniList
{
/// <summary>
/// Based on the new API from AniList
/// 🛈 This code works with the API Interface (v2) from AniList
/// 🛈 https://anilist.gitbooks.io/anilist-apiv2-docs
/// 🛈 THIS IS AN UNOFFICAL API INTERFACE FOR EMBY
/// </summary>
public class AniListApi
{
private static IJsonSerializer _jsonSerializer;
private const string SearchLink = @"https://graphql.anilist.co/api/v2?query=
query ($query: String, $type: MediaType) {
Page {
media(search: $query, type: $type) {
id
title {
romaji
english
native
}
coverImage {
medium
large
}
format
type
averageScore
popularity
episodes
season
hashtag
isAdult
startDate {
year
month
day
}
endDate {
year
month
day
}
}
}
}&variables={ ""query"":""{0}"",""type"":""ANIME""}";
public string AniList_anime_link = @"https://graphql.anilist.co/api/v2?query=query($id: Int!, $type: MediaType) {
Media(id: $id, type: $type)
{
id
title {
romaji
english
native
userPreferred
}
startDate {
year
month
day
}
endDate {
year
month
day
}
coverImage {
large
medium
}
bannerImage
format
type
status
episodes
chapters
volumes
season
description
averageScore
meanScore
genres
synonyms
nextAiringEpisode {
airingAt
timeUntilAiring
episode
}
}
}&variables={ ""id"":""{0}"",""type"":""ANIME""}";
private const string AniList_anime_char_link = @"https://graphql.anilist.co/api/v2?query=query($id: Int!, $type: MediaType, $page: Int = 1) {
Media(id: $id, type: $type) {
id
characters(page: $page, sort: [ROLE]) {
pageInfo {
total
perPage
hasNextPage
currentPage
lastPage
}
edges {
node {
id
name {
first
last
}
image {
medium
large
}
}
role
voiceActors {
id
name {
first
last
native
}
image {
medium
large
}
language
}
}
}
}
}&variables={ ""id"":""{0}"",""type"":""ANIME""}";
public AniListApi(IJsonSerializer jsonSerializer)
{
_jsonSerializer = jsonSerializer;
}
/// <summary>
/// API call to get the anime with the id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<RemoteSearchResult> GetAnime(string id)
{
RootObject WebContent = await WebRequestAPI(AniList_anime_link.Replace("{0}",id));
var result = new RemoteSearchResult
{
Name = ""
};
result.SearchProviderName = WebContent.data.Media.title.romaji;
result.ImageUrl = WebContent.data.Media.coverImage.large;
result.SetProviderId(ProviderNames.AniList, id);
result.Overview = WebContent.data.Media.description;
return result;
}
/// <summary>
/// API call to select the lang
/// </summary>
/// <param name="WebContent"></param>
/// <param name="preference"></param>
/// <param name="language"></param>
/// <returns></returns>
private string SelectName(RootObject WebContent, TitlePreferenceType preference, string language)
{
if (preference == TitlePreferenceType.Localized && language == "en")
return WebContent.data.Media.title.english;
if (preference == TitlePreferenceType.Japanese)
return WebContent.data.Media.title.native;
return WebContent.data.Media.title.romaji;
}
/// <summary>
/// API call to get the title with the right lang
/// </summary>
/// <param name="lang"></param>
/// <param name="WebContent"></param>
/// <returns></returns>
public string Get_title(string lang, RootObject WebContent)
{
switch (lang)
{
case "en":
return WebContent.data.Media.title.english;
case "jap":
return WebContent.data.Media.title.native;
//Default is jap_r
default:
return WebContent.data.Media.title.romaji;
}
}
public async Task<List<PersonInfo>> GetPersonInfo(int id, CancellationToken cancellationToken)
{
List<PersonInfo> lpi = new List<PersonInfo>();
RootObject WebContent = await WebRequestAPI(AniList_anime_char_link.Replace("{0}", id.ToString()));
foreach (Edge edge in WebContent.data.Media.characters.edges)
{
PersonInfo pi = new PersonInfo();
pi.Name = edge.node.name.first+" "+ edge.node.name.last;
pi.ImageUrl = edge.node.image.large;
pi.Role = edge.role;
}
return lpi;
}
/// <summary>
/// Convert int to Guid
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public async static Task<Guid> ToGuid(int value, CancellationToken cancellationToken)
{
byte[] bytes = new byte[16];
await Task.Run(() => BitConverter.GetBytes(value).CopyTo(bytes, 0), cancellationToken);
return new Guid(bytes);
}
/// <summary>
/// API call to get the genre of the anime
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public List<string> Get_Genre(RootObject WebContent)
{
return WebContent.data.Media.genres;
}
/// <summary>
/// API call to get the img url
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public string Get_ImageUrl(RootObject WebContent)
{
return WebContent.data.Media.coverImage.large;
}
/// <summary>
/// API call too get the rating
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public string Get_Rating(RootObject WebContent)
{
return (WebContent.data.Media.averageScore / 10).ToString();
}
/// <summary>
/// API call to get the description
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public string Get_Overview(RootObject WebContent)
{
return WebContent.data.Media.description;
}
/// <summary>
/// API call to search a title and return the right one back
/// </summary>
/// <param name="title"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> Search_GetSeries(string title, CancellationToken cancellationToken)
{
string result = null;
RootObject WebContent = await WebRequestAPI(SearchLink.Replace("{0}", title));
foreach (Medium media in WebContent.data.Page.media) {
//get id
try
{
if (await Equals_check.Compare_strings(media.title.romaji, title, cancellationToken))
{
return media.id.ToString();
}
if (await Equals_check.Compare_strings(media.title.english, title, cancellationToken))
{
return media.id.ToString();
}
//Disabled due to false result.
/*if (await Task.Run(() => Equals_check.Compare_strings(media.title.native, title)))
{
return media.id.ToString();
}*/
}
catch (Exception) { }
}
return result;
}
/// <summary>
/// API call to search a title and return a list back
/// </summary>
/// <param name="title"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<List<string>> Search_GetSeries_list(string title, CancellationToken cancellationToken)
{
List<string> result = new List<string>();
RootObject WebContent = await WebRequestAPI(SearchLink.Replace("{0}", title));
foreach (Medium media in WebContent.data.Page.media)
{
//get id
try
{
if (await Equals_check.Compare_strings(media.title.romaji, title, cancellationToken))
{
result.Add(media.id.ToString());
}
if (await Equals_check.Compare_strings(media.title.english, title, cancellationToken))
{
result.Add(media.id.ToString());
}
//Disabled due to false result.
/*if (await Task.Run(() => Equals_check.Compare_strings(media.title.native, title)))
{
result.Add(media.id.ToString());
}*/
}
catch (Exception) { }
}
return result;
}
/// <summary>
/// SEARCH Title
/// </summary>
public async Task<string> FindSeries(string title, CancellationToken cancellationToken)
{
string aid = await Search_GetSeries(title, cancellationToken);
if (!string.IsNullOrEmpty(aid))
{
return aid;
}
aid = await Search_GetSeries(await Equals_check.Clear_name(title, cancellationToken), cancellationToken);
if (!string.IsNullOrEmpty(aid))
{
return aid;
}
return null;
}
/// <summary>
/// GET website content from the link
/// </summary>
public async Task<RootObject> WebRequestAPI(string link)
{
string _strContent = "";
using (WebClient client = new WebClient())
{
client.Headers.Add("User-Agent", Constants.UserAgent);
var values = new System.Collections.Specialized.NameValueCollection();
var response = await Task.Run(() => client.UploadValues(new Uri(link),values));
_strContent = System.Text.Encoding.Default.GetString(response);
}
RootObject data = _jsonSerializer.DeserializeFromString<RootObject>(_strContent);
return data;
}
}
}