-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathTorrentByController.cs
360 lines (314 loc) · 15.9 KB
/
TorrentByController.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
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Lampac.Engine.CORE;
using Lampac.Engine.Parse;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Shared;
using Shared.Engine.CORE;
using JacRed.Engine;
using JacRed.Models;
using System.Collections.Generic;
namespace Lampac.Controllers.JAC
{
[Route("torrentby/[action]")]
public class TorrentByController : JacBaseController
{
#region parseMagnet
async public Task<ActionResult> parseMagnet(int id, string magnet)
{
if (!jackett.TorrentBy.enable || jackett.TorrentBy.priority != "torrent")
return Content("disable");
string key = $"torrentby:parseMagnet:{id}";
if (id == 0 || Startup.memoryCache.TryGetValue($"{key}:error", out _))
return Redirect(magnet);
if (Startup.memoryCache.TryGetValue(key, out byte[] _t))
return File(_t, "application/x-bittorrent");
var proxyManager = new ProxyManager("torrentby", jackett.TorrentBy);
_t = await HttpClient.Download($"{jackett.TorrentBy.host}/d.php?id={id}", referer: jackett.TorrentBy.host, timeoutSeconds: 10, proxy: proxyManager.Get());
if (_t != null && BencodeTo.Magnet(_t) != null)
{
if (jackett.cache)
{
TorrentCache.Write(key, _t);
Startup.memoryCache.Set(key, _t, DateTime.Now.AddMinutes(Math.Max(1, jackett.torrentCacheToMinutes)));
}
return File(_t, "application/x-bittorrent");
}
else if (jackett.emptycache && jackett.cache)
Startup.memoryCache.Set($"{key}:error", 0, DateTime.Now.AddMinutes(1));
proxyManager.Refresh();
return Redirect(magnet);
}
#endregion
public static Task<bool> search(string host, ConcurrentBag<TorrentDetails> torrents, string query, string cat)
{
if (!jackett.TorrentBy.enable)
return Task.FromResult(false);
return JackettCache.Invoke($"torrentby:{cat}:{query}", torrents, () => parsePage(host, query, cat));
}
async static ValueTask<List<TorrentDetails>> parsePage(string host, string query, string cat)
{
var torrents = new List<TorrentDetails>();
#region html
var proxyManager = new ProxyManager("torrentby", jackett.TorrentBy);
string html = await HttpClient.Get($"{jackett.TorrentBy.host}/search/?search={HttpUtility.UrlEncode(query)}&category={cat}&search_in=0", proxy: proxyManager.Get(), timeoutSeconds: jackett.timeoutSeconds);
if (html == null)
{
proxyManager.Refresh();
return null;
}
#endregion
foreach (string row in html.Split("<tr class=\"ttable_col").Skip(1))
{
#region Локальный метод - Match
string Match(string pattern, int index = 1)
{
string res = HttpUtility.HtmlDecode(new Regex(pattern, RegexOptions.IgnoreCase).Match(row).Groups[index].Value.Trim());
res = Regex.Replace(res, "[\n\r\t ]+", " ");
return res.Trim();
}
#endregion
if (string.IsNullOrWhiteSpace(row) || !row.Contains("magnet:?xt=urn"))
continue;
#region Дата создания
DateTime createTime = default;
if (row.Contains(">Сегодня</td>"))
{
createTime = DateTime.Today;
}
else if (row.Contains(">Вчера</td>"))
{
createTime = DateTime.Today.AddDays(-1);
}
else
{
string _createTime = Match(">([0-9]{4}-[0-9]{2}-[0-9]{2})</td>").Replace("-", " ");
if (!DateTime.TryParseExact(_createTime, "yyyy MM dd", new CultureInfo("ru-RU"), DateTimeStyles.None, out createTime))
continue;
}
//if (createTime == default)
// continue;
#endregion
#region Данные раздачи
string url = Match("<a name=\"search_select\" [^>]+ href=\"/([0-9]+/[^\"]+)\"");
string title = Match("<a name=\"search_select\" [^>]+>([^<]+)</a>");
string _sid = Match("<font color=\"green\">↑ ([0-9]+)</font>");
string _pir = Match("<font color=\"red\">↓ ([0-9]+)</font>");
string sizeName = Match("</td><td style=\"white-space:nowrap;\">([^<]+)</td>");
string magnet = Match("href=\"(magnet:\\?xt=[^\"]+)\"");
string viewtopic = Regex.Match(url, "^([0-9]+)").Groups[1].Value;
if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(magnet))
continue;
url = $"{jackett.TorrentBy.host}/{url}";
#endregion
#region Парсим раздачи
int relased = 0;
string name = null, originalname = null;
if (cat == "1")
{
#region Зарубежные фильмы
// Код бессмертия / Код молодости / Eternal Code (2019)
var g = Regex.Match(title, "^([^/\\(]+) / [^/]+ / ([^/\\(]+) \\(([0-9]{4})\\)").Groups;
if (!string.IsNullOrWhiteSpace(g[1].Value) && !string.IsNullOrWhiteSpace(g[2].Value) && !string.IsNullOrWhiteSpace(g[3].Value))
{
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
else
{
// Брешь / Breach (2020)
g = Regex.Match(title, "^([^/\\(]+) / ([^/\\(]+) \\(([0-9]{4})\\)").Groups;
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
#endregion
}
else if (cat == "2")
{
#region Наши фильмы
// Временная связь (2020)
// Приключения принца Флоризеля / Клуб самоубийц или Приключения титулованной особы (1979)
var g = Regex.Match(title, "^([^/\\(]+) (/ [^/\\(]+)?\\(([0-9]{4})\\)").Groups;
name = g[1].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
#endregion
}
else if (cat == "3")
{
#region Сериалы
// Голяк / Без гроша / Без денег / Brassic [S04] (2022) WEB-DLRip | Ozz
var g = Regex.Match(title, "^([^/\\(\\[]+) / [^/]+ / [^/]+ / ([^/\\(\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
if (!string.IsNullOrWhiteSpace(g[1].Value) && !string.IsNullOrWhiteSpace(g[2].Value) && !string.IsNullOrWhiteSpace(g[3].Value))
{
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
else
{
// Перевал / Der Pass / Pagan Peak [S01] (2018)
g = Regex.Match(title, "^([^/\\(\\[]+) / [^/]+ / ([^/\\(\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
if (!string.IsNullOrWhiteSpace(g[1].Value) && !string.IsNullOrWhiteSpace(g[2].Value) && !string.IsNullOrWhiteSpace(g[3].Value))
{
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
else
{
// Стража / The Watch [01x01-05 из 08] (2020)
g = Regex.Match(title, "^([^/\\(\\[]+) / ([^/\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
if (!string.IsNullOrWhiteSpace(g[1].Value) && !string.IsNullOrWhiteSpace(g[2].Value) && !string.IsNullOrWhiteSpace(g[3].Value))
{
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
else
{
// Стажёры [01-10 из 24] (2019)
g = Regex.Match(title, "^([^/\\(\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
name = g[1].Value;
if (int.TryParse(g[2].Value, out int _yer))
relased = _yer;
}
}
}
#endregion
}
else if (cat == "5" || cat == "6" || cat == "4" || cat == "12")
{
#region Мультфильмы / Аниме / Телевизор / Юмор
if (title.Contains(" / "))
{
if (title.Contains("[") && title.Contains("]"))
{
// Разочарование / еще название / Disenchantment [S03] (2021)
var g = Regex.Match(title, "^([^/]+) / [^/]+ / ([^/\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
if (!string.IsNullOrWhiteSpace(g[1].Value) && !string.IsNullOrWhiteSpace(g[2].Value) && !string.IsNullOrWhiteSpace(g[3].Value))
{
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
else
{
// Разочарование / Disenchantment [S03] (2021)
g = Regex.Match(title, "^([^/]+) / ([^/\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
}
else
{
// Душа / еще название / Soul (2020)
var g = Regex.Match(title, "^([^/\\(]+) / [^/]+ / ([^/\\(]+) \\(([0-9]{4})(\\)|-)").Groups;
if (!string.IsNullOrWhiteSpace(g[1].Value) && !string.IsNullOrWhiteSpace(g[2].Value) && !string.IsNullOrWhiteSpace(g[3].Value))
{
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
else
{
// Душа / Soul (2020)
// Галактики / Galaxies (2017-2019)
g = Regex.Match(title, "^([^/\\(]+) / ([^/\\(]+) \\(([0-9]{4})(\\)|-)").Groups;
name = g[1].Value;
originalname = g[2].Value;
if (int.TryParse(g[3].Value, out int _yer))
relased = _yer;
}
}
}
else
{
if (title.Contains("[") && title.Contains("]"))
{
// Непокоренные [01-04 из 04] (2020)
var g = Regex.Match(title, "^([^/\\[]+) \\[[^\\]]+\\] +\\(([0-9]{4})(\\)|-)").Groups;
name = g[1].Value;
if (int.TryParse(g[2].Value, out int _yer))
relased = _yer;
}
else
{
// Душа (2020)
var g = Regex.Match(title, "^([^/\\(]+) \\(([0-9]{4})(\\)|-)").Groups;
name = g[1].Value;
if (int.TryParse(g[2].Value, out int _yer))
relased = _yer;
}
}
#endregion
}
#endregion
if (string.IsNullOrWhiteSpace(name))
name = Regex.Split(title, "(\\[|\\/|\\(|\\|)", RegexOptions.IgnoreCase)[0].Trim();
if (!string.IsNullOrWhiteSpace(name) || cat == "0")
{
#region types
string[] types = new string[] { };
switch (cat)
{
case "1":
case "2":
types = new string[] { "movie" };
break;
case "3":
types = new string[] { "serial" };
break;
case "4":
case "12":
types = new string[] { "tvshow" };
break;
case "5":
types = new string[] { "multfilm", "multserial" };
break;
case "6":
types = new string[] { "anime" };
break;
}
#endregion
int.TryParse(_sid, out int sid);
int.TryParse(_pir, out int pir);
torrents.Add(new TorrentDetails()
{
trackerName = "torrentby",
types = types,
url = url,
title = title,
sid = sid,
pir = pir,
sizeName = sizeName,
magnet = jackett.TorrentBy.priority == "torrent" ? null : magnet,
parselink = jackett.TorrentBy.priority == "torrent" ? $"{host}/torrentby/parsemagnet?id={viewtopic}&magnet={HttpUtility.UrlEncode(magnet)}" : null,
createTime = createTime,
name = name,
originalname = originalname,
relased = relased
});
}
}
return torrents;
}
}
}