forked from jellyfin-archive/jellyfin-plugin-anime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAniSearchApi.cs
305 lines (280 loc) · 11.9 KB
/
AniSearchApi.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Anime.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
namespace Jellyfin.Plugin.Anime.Providers.AniSearch
{
/// <summary>
/// API for https://anisearch.com
/// Anisearch does not have an API interface to work with
/// </summary>
internal class AniSearchApi
{
public static List<string> anime_search_names = new List<string>();
public static List<string> anime_search_ids = new List<string>();
public static string SearchLink = "https://www.anisearch.com/anime/index/?char=all&page=1&text={0}&smode=2&sort=title&order=asc&view=2&title=de,en,fr,it,pl,ru,es,tr&titlex=1,2&hentai=yes";
public static string AniSearch_anime_link = "https://www.anisearch.com/anime/";
public static readonly HttpClient _httpClient;
static AniSearchApi()
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", Constants.UserAgent);
}
/// <summary>
/// API call to get the anime with the id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static async Task<RemoteSearchResult> GetAnime(string id)
{
string WebContent = await WebRequestAPI(AniSearch_anime_link + id);
var result = new RemoteSearchResult
{
Name = await SelectName(WebContent, Plugin.Instance.Configuration.TitlePreference, "en")
};
result.SearchProviderName = await One_line_regex(new Regex("\"" + "Japanisch" + "\"" + @"> <strong>(.*?)<\/"), WebContent);
result.ImageUrl = await Get_ImageUrl(WebContent);
result.SetProviderId(ProviderNames.AniSearch, id);
result.Overview = await Get_Overview(WebContent);
return result;
}
/// <summary>
/// API call to select the language
/// </summary>
/// <param name="WebContent"></param>
/// <param name="preference"></param>
/// <param name="language"></param>
/// <returns></returns>
private static async Task<string> SelectName(string WebContent, TitlePreferenceType preference, string language)
{
if (preference == TitlePreferenceType.Localized && language == "en")
return await Get_title("en", WebContent);
if (preference == TitlePreferenceType.Localized && language == "de")
return await Get_title("de", WebContent);
if (preference == TitlePreferenceType.Localized && language == "ger")
return await Get_title("de", WebContent);
if (preference == TitlePreferenceType.Japanese)
return await Get_title("jap", WebContent);
return await Get_title("jap_r", WebContent);
}
/// <summary>
/// API call to get the title with the right language
/// </summary>
/// <param name="lang"></param>
/// <param name="WebContent"></param>
/// <returns></returns>
public static async Task<string> Get_title(string lang, string WebContent)
{
switch (lang)
{
case "en":
return await One_line_regex(new Regex("\"" + "Englisch" + "\"" + @"> <strong>(.*?)<\/"), WebContent);
case "de":
return await One_line_regex(new Regex("\"" + "Deutsch" + "\"" + @"> <strong>(.*?)<\/"), WebContent);
case "jap":
return await One_line_regex(new Regex("<div class=\"grey\">" + @"(.*?)<\/"), await One_line_regex(new Regex("\"" + "Englisch" + "\"" + @"> <strong>(.*?)<\/div"), WebContent));
//Default is jap_r
default:
return await One_line_regex(new Regex("\"" + "Japanisch" + "\"" + @"> <strong>(.*?)<\/"), WebContent);
}
}
/// <summary>
/// API call to get the genre of the anime
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public static async Task<List<string>> Get_Genre(string WebContent)
{
List<string> result = new List<string>();
string Genres = await One_line_regex(new Regex("<ul class=\"cloud\">" + @"(.*?)<\/ul>"), WebContent);
int x = 0;
string AniSearch_Genre = null;
while (AniSearch_Genre != "")
{
AniSearch_Genre = await One_line_regex(new Regex(@"<li>(.*?)<\/li>"), Genres, 0, x);
AniSearch_Genre = await One_line_regex(new Regex("\">" + @"(.*?)<\/a>"), AniSearch_Genre);
if (AniSearch_Genre != "")
{
result.Add(AniSearch_Genre);
}
x++;
}
return result;
}
/// <summary>
/// API call to get the image url
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public static async Task<string> Get_ImageUrl(string WebContent)
{
return await One_line_regex(new Regex("<img itemprop=\"image\" src=\"" + @"(.*?)" + "\""), WebContent);
}
/// <summary>
/// API call to get the rating
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public static async Task<string> Get_Rating(string WebContent)
{
return await One_line_regex(new Regex("<span itemprop=\"ratingValue\">" + @"(.*?)" + @"<\/span>"), WebContent);
}
/// <summary>
/// API call to get the description
/// </summary>
/// <param name="WebContent"></param>
/// <returns></returns>
public static async Task<string> Get_Overview(string WebContent)
{
return Regex.Replace(await One_line_regex(new Regex("<span itemprop=\"description\" lang=\"de\" id=\"desc-de\" class=\"desc-zz textblock\">" + @"(.*?)<\/span>"), WebContent), "<.*?>", String.Empty);
}
/// <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 static async Task<string> Search_GetSeries(string title, CancellationToken cancellationToken)
{
anime_search_names.Clear();
anime_search_ids.Clear();
string result = null;
string result_text = null;
string WebContent = await WebRequestAPI(string.Format(SearchLink, title));
int x = 0;
while (result_text != "")
{
result_text = await One_line_regex(new Regex("<th scope=\"row\" class=\"showpop\" data-width=\"200\"" + @".*?>(.*)<\/th>"), WebContent, 1, x);
if (result_text != "")
{
//get id
int _x = 0;
string a_name = null;
while (a_name != "")
{
try
{
string id = await One_line_regex(new Regex(@"anime\/(.*?),"), result_text);
a_name = Regex.Replace(await One_line_regex(new Regex(@"((<a|<d).*?>)(.*?)(<\/a>|<\/div>)"), result_text, 3, _x), "<.*?>", String.Empty);
if (a_name != "")
{
if (await Equals_check.Compare_strings(a_name, title, cancellationToken))
{
return id;
}
if (Int32.TryParse(id, out int n))
{
anime_search_names.Add(a_name);
anime_search_ids.Add(id);
}
}
}
catch (Exception) { }
_x++;
}
}
x++;
}
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 static async Task<List<string>> Search_GetSeries_list(string title, CancellationToken cancellationToken)
{
List<string> result = new List<string>();
string result_text = null;
string WebContent = await WebRequestAPI(string.Format(SearchLink, title));
int x = 0;
while (result_text != "")
{
result_text = await One_line_regex(new Regex("<th scope=\"row\" class=\"showpop\" data-width=\"200\"" + @".*?>(.*)<\/th>"), WebContent, 1, x);
if (result_text != "")
{
//get id
int _x = 0;
string a_name = null;
while (a_name != "")
{
string id = await One_line_regex(new Regex(@"anime\/(.*?),"), result_text);
a_name = Regex.Replace(await One_line_regex(new Regex(@"((<a|<d).*?>)(.*?)(<\/a>|<\/div>)"), result_text, 3, _x), "<.*?>", String.Empty);
if (a_name != "")
{
if (await Equals_check.Compare_strings(a_name, title, cancellationToken))
{
result.Add(id);
return result;
}
if (Int32.TryParse(id, out int n))
{
result.Add(id);
}
}
_x++;
}
}
x++;
}
return result;
}
/// <summary>
/// SEARCH Title
/// </summary>
public static async Task<string> FindSeries(string title, CancellationToken cancellationToken)
{
string aid = await Search_GetSeries(title, cancellationToken);
if (!string.IsNullOrEmpty(aid))
{
return aid;
}
else
{
int x = 0;
foreach (string a_name in anime_search_names)
{
if (await Equals_check.Compare_strings(a_name, title, cancellationToken))
{
return anime_search_ids[x];
}
x++;
}
}
aid = await Search_GetSeries(await Equals_check.Clear_name(title, cancellationToken), cancellationToken);
if (!string.IsNullOrEmpty(aid))
{
return aid;
}
return null;
}
/// <summary>
/// Simple regex
/// </summary>
public static async Task<string> One_line_regex(Regex regex, string match, int group = 1, int match_int = 0)
{
int x = 0;
MatchCollection matches = await Task.Run(() => regex.Matches(match));
foreach (Match _match in matches)
{
if (x == match_int)
{
return await Task.Run(() => _match.Groups[group].Value);
}
x++;
}
return "";
}
/// <summary>
/// GET website content from the link
/// </summary>
public static Task<string> WebRequestAPI(string link)
=> _httpClient.GetStringAsync(link);
}
}