Skip to content

Commit e844e05

Browse files
committed
19.01.2024
1 parent c8e2781 commit e844e05

File tree

8 files changed

+227
-62
lines changed

8 files changed

+227
-62
lines changed

Controllers/ApiController.cs

Lines changed: 120 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Newtonsoft.Json;
54
using JacRed.Engine.CORE;
65
using System.Text.RegularExpressions;
76
using JacRed.Engine;
@@ -13,6 +12,7 @@
1312
using MonoTorrent;
1413
using JacRed.Models.Details;
1514
using JacRed.Models.Tracks;
15+
using JacRed.Models.Api;
1616

1717
namespace JacRed.Controllers
1818
{
@@ -37,7 +37,8 @@ public JsonResult JacRedConf(string apikey)
3737
[Route("/api/v2.0/indexers/{status}/results")]
3838
public ActionResult Jackett(string apikey, string query, string title, string title_original, int year, Dictionary<string, string> category, int is_serial = -1)
3939
{
40-
bool rqnum = false, setcache = false;
40+
bool rqnum = false;
41+
var fastdb = getFastdb();
4142
var torrents = new Dictionary<string, TorrentDetails>();
4243

4344
#region Запрос с NUM
@@ -102,32 +103,38 @@ void AddTorrents(TorrentDetails t)
102103
}
103104
#endregion
104105

105-
string memoryKey = $"{AppInit.conf.mergeduplicates}:{rqnum}:{title}:{title_original}:{year}:{is_serial}";
106-
if (memoryCache.TryGetValue(memoryKey, out string jval))
107-
return Content(jval, "application/json; charset=utf-8");
108-
109106
if (!string.IsNullOrWhiteSpace(title) || !string.IsNullOrWhiteSpace(title_original))
110107
{
111108
#region Точный поиск
112-
setcache = true;
113-
114109
string _n = StringConvert.SearchName(title);
115110
string _o = StringConvert.SearchName(title_original);
116111

117-
// Быстрая выборка по совпадению ключа в имени
118-
var mdb = FileDB.masterDb.Where(i => (_n != null && i.Key.StartsWith($"{_n}:")) || (_o != null && i.Key.EndsWith($":{_o}")));
119-
if (!AppInit.conf.evercache.enable)
120-
mdb = mdb.Take(AppInit.conf.maxreadfile);
112+
HashSet<string> keys = new HashSet<string>(20);
121113

122-
foreach (var val in mdb)
114+
void updateKeys(string k)
123115
{
124-
foreach (var t in FileDB.OpenRead(val.Key, true).Values)
116+
if (k != null && fastdb.TryGetValue(k, out List<string> _keys))
117+
{
118+
foreach (string val in _keys)
119+
keys.Add(val);
120+
}
121+
}
122+
123+
updateKeys(_n);
124+
updateKeys(_o);
125+
126+
if ((!AppInit.conf.evercache.enable || AppInit.conf.evercache.validHour > 0) && keys.Count > AppInit.conf.maxreadfile)
127+
keys = keys.Take(AppInit.conf.maxreadfile).ToHashSet();
128+
129+
foreach (string key in keys)
130+
{
131+
foreach (var t in FileDB.OpenRead(key, true).Values)
125132
{
126133
if (t.types == null || t.title.Contains(" КПК"))
127134
continue;
128135

129-
string name = StringConvert.SearchName(t.name);
130-
string originalname = StringConvert.SearchName(t.originalname);
136+
string name = t._sn ?? StringConvert.SearchName(t.name);
137+
string originalname = t._so ?? StringConvert.SearchName(t.originalname);
131138

132139
// Точная выборка по name или originalname
133140
if ((_n != null && _n == name) || (_o != null && _o == originalname))
@@ -256,17 +263,38 @@ void AddTorrents(TorrentDetails t)
256263
#region torrentsSearch
257264
void torrentsSearch(bool exact)
258265
{
259-
var mdb = FileDB.masterDb.Where(i => i.Key.Contains(_s));
260-
if (!AppInit.conf.evercache.enable)
261-
mdb = mdb.Take(AppInit.conf.maxreadfile);
266+
if (_s == null)
267+
return;
268+
269+
var keys = new HashSet<string>(20);
270+
271+
if (exact)
272+
{
273+
if (fastdb.TryGetValue(_s, out List<string> _keys))
274+
{
275+
foreach (string val in _keys)
276+
keys.Add(val);
277+
}
278+
}
279+
else
280+
{
281+
foreach (var f in fastdb.Where(i => i.Key.Contains(_s)))
282+
{
283+
foreach (string k in f.Value)
284+
keys.Add(k);
262285

263-
foreach (var val in mdb)
286+
if ((!AppInit.conf.evercache.enable || AppInit.conf.evercache.validHour > 0) && keys.Count > AppInit.conf.maxreadfile)
287+
break;
288+
}
289+
}
290+
291+
foreach (string key in keys)
264292
{
265-
foreach (var t in FileDB.OpenRead(val.Key, true).Values)
293+
foreach (var t in FileDB.OpenRead(key, true).Values)
266294
{
267295
if (exact)
268296
{
269-
if (StringConvert.SearchName(t.name) != _s && StringConvert.SearchName(t.originalname) != _s)
297+
if ((t._sn ?? StringConvert.SearchName(t.name)) != _s && (t._so ?? StringConvert.SearchName(t.originalname)) != _s)
270298
continue;
271299
}
272300

@@ -323,7 +351,7 @@ void torrentsSearch(bool exact)
323351
HashSet<int> getCategoryIds(TorrentDetails t, out string categoryDesc)
324352
{
325353
categoryDesc = null;
326-
HashSet<int> categoryIds = new HashSet<int>();
354+
HashSet<int> categoryIds = new HashSet<int>(t.types.Length);
327355

328356
foreach (string type in t.types)
329357
{
@@ -363,13 +391,13 @@ HashSet<int> getCategoryIds(TorrentDetails t, out string categoryDesc)
363391
#endregion
364392

365393
#region Объединить дубликаты
366-
var tsort = new List<TorrentDetails>();
394+
IEnumerable<TorrentDetails> result = null;
367395

368396
if ((!rqnum && AppInit.conf.mergeduplicates) || (rqnum && AppInit.conf.mergenumduplicates))
369397
{
370398
Dictionary<string, (TorrentDetails torrent, string title, string Name, List<string> AnnounceUrls)> temp = new Dictionary<string, (TorrentDetails, string, string, List<string>)>();
371399

372-
foreach (var torrent in torrents.Values.ToList())
400+
foreach (var torrent in torrents.Values)
373401
{
374402
var magnetLink = MagnetLink.Parse(torrent.magnet);
375403
string hex = magnetLink.InfoHash.ToHex();
@@ -490,40 +518,41 @@ void UpdateTitle()
490518
}
491519
}
492520

493-
foreach (var item in temp.Select(i => i.Value.torrent))
494-
tsort.Add(item);
521+
result = temp.Select(i => i.Value.torrent);
495522
}
496523
else
497524
{
498-
tsort = torrents.Values.ToList();
525+
result = torrents.Values;
499526
}
500527
#endregion
501528

529+
if (apikey == "rus")
530+
result = result.Where(i => (i.languages != null && i.languages.Contains("rus")) || (i.types != null && (i.types.Contains("sport") || i.types.Contains("tvshow") || i.types.Contains("docuserial"))));
531+
502532
#region FFprobe
503533
List<ffStream> FFprobe(TorrentDetails t, out HashSet<string> langs)
504534
{
505535
langs = t.languages;
506-
if (t.ffprobe != null)
536+
if (t.ffprobe != null || !AppInit.conf.tracks)
507537
return t.ffprobe;
508538

509539
var streams = TracksDB.Get(t.magnet, t.types);
510-
if (streams == null)
540+
if (streams == null)
511541
return null;
512542

513543
langs = TracksDB.Languages(t, streams);
514544
return streams;
515545
}
516546
#endregion
517547

518-
var result = tsort.OrderByDescending(i => i.createTime).Take(2_000);
519-
if (apikey == "rus")
520-
result = result.Where(i => (i.languages != null && i.languages.Contains("rus")) || (i.types != null && (i.types.Contains("sport") || i.types.Contains("tvshow") || i.types.Contains("docuserial"))));
521-
522-
HashSet<string> languages = null;
548+
var Results = new List<Result>(torrents.Values.Count);
523549

524-
jval = JsonConvert.SerializeObject(new
550+
foreach (var i in result)
525551
{
526-
Results = result.Select(i => new
552+
HashSet<string> languages = null;
553+
var ffprobe = rqnum ? null : FFprobe(i, out languages);
554+
555+
Results.Add(new Result()
527556
{
528557
Tracker = i.trackerName,
529558
Details = i.url != null && i.url.StartsWith("http") ? i.url : null,
@@ -535,29 +564,24 @@ List<ffStream> FFprobe(TorrentDetails t, out HashSet<string> langs)
535564
Seeders = i.sid,
536565
Peers = i.pir,
537566
MagnetUri = i.magnet,
538-
ffprobe = rqnum ? null : FFprobe(i, out languages),
539-
languages = rqnum ? null : languages,
540-
info = rqnum ? null : new
567+
ffprobe = ffprobe,
568+
languages = languages,
569+
info = rqnum ? null : new TorrentInfo()
541570
{
542-
i.name,
543-
i.originalname,
544-
i.sizeName,
545-
i.relased,
546-
i.videotype,
547-
i.quality,
548-
i.voices,
571+
name = i.name,
572+
originalname = i.originalname,
573+
sizeName = i.sizeName,
574+
relased = i.relased,
575+
videotype = i.videotype,
576+
quality = i.quality,
577+
voices = i.voices,
549578
seasons = i.seasons != null && i.seasons.Count > 0 ? i.seasons : null,
550-
i.types
579+
types = i.types
551580
}
552-
}),
553-
jacred = true
554-
555-
}, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
556-
557-
if (setcache && !AppInit.conf.evercache.enable)
558-
memoryCache.Set(memoryKey, jval, DateTime.Now.AddMinutes(10));
581+
});
582+
}
559583

560-
return Content(jval, "application/json; charset=utf-8");
584+
return Json(new RootObject() { Results = Results });
561585
}
562586
#endregion
563587

@@ -634,8 +658,8 @@ void AddTorrents(TorrentDetails t)
634658

635659
if (string.IsNullOrWhiteSpace(type) || t.types.Contains(type))
636660
{
637-
string _n = StringConvert.SearchName(t.name);
638-
string _o = StringConvert.SearchName(t.originalname);
661+
string _n = t._sn ?? StringConvert.SearchName(t.name);
662+
string _o = t._so ?? StringConvert.SearchName(t.originalname);
639663

640664
if (_n == _s || _o == _s || (_altsearch != null && (_n == _altsearch || _o == _altsearch)))
641665
AddTorrents(t);
@@ -649,7 +673,7 @@ void AddTorrents(TorrentDetails t)
649673
{
650674
#region Поиск по совпадению ключа в имени
651675
var mdb = FileDB.masterDb.Where(i => i.Key.Contains(_s) || (_altsearch != null && i.Key.Contains(_altsearch)));
652-
if (!AppInit.conf.evercache.enable)
676+
if (!AppInit.conf.evercache.enable || AppInit.conf.evercache.validHour > 0)
653677
mdb = mdb.Take(AppInit.conf.maxreadfile);
654678

655679
foreach (var val in mdb)
@@ -684,7 +708,7 @@ void AddTorrents(TorrentDetails t)
684708
case "size":
685709
query = query.OrderByDescending(i => i.size);
686710
break;
687-
default:
711+
case "create":
688712
query = query.OrderByDescending(i => i.createTime);
689713
break;
690714
}
@@ -731,5 +755,41 @@ void AddTorrents(TorrentDetails t)
731755
}));
732756
}
733757
#endregion
758+
759+
760+
#region getFastdb
761+
Dictionary<string, List<string>> getFastdb()
762+
{
763+
if (!memoryCache.TryGetValue("api:fastdb", out Dictionary<string, List<string>> fastdb))
764+
{
765+
fastdb = new Dictionary<string, List<string>>();
766+
767+
foreach (var item in FileDB.masterDb)
768+
{
769+
foreach (string k in item.Key.Split(":"))
770+
{
771+
if (string.IsNullOrEmpty(k))
772+
continue;
773+
774+
if (fastdb.TryGetValue(k, out List<string> keys))
775+
{
776+
if (keys == null)
777+
keys = new List<string>();
778+
779+
keys.Add(item.Key);
780+
}
781+
else
782+
{
783+
fastdb.Add(k, new List<string>() { item.Key });
784+
}
785+
}
786+
}
787+
788+
memoryCache.Set("api:fastdb", fastdb, DateTime.Now.AddMinutes(10));
789+
}
790+
791+
return fastdb;
792+
}
793+
#endregion
734794
}
735795
}

Controllers/DevController.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text.RegularExpressions;
55
using JacRed.Engine;
6+
using JacRed.Engine.CORE;
67
using Microsoft.AspNetCore.Mvc;
78

89
namespace JacRed.Controllers
@@ -111,5 +112,28 @@ public JsonResult UpdateDetails()
111112
FileDB.SaveChangesToFile();
112113
return Json(new { ok = true });
113114
}
115+
116+
public JsonResult UpdateSearchName()
117+
{
118+
if (HttpContext.Connection.RemoteIpAddress.ToString() != "127.0.0.1")
119+
return Json(new { badip = true });
120+
121+
foreach (var item in FileDB.masterDb.ToArray())
122+
{
123+
using (var fdb = FileDB.OpenWrite(item.Key))
124+
{
125+
foreach (var torrent in fdb.Database)
126+
{
127+
torrent.Value._sn = StringConvert.SearchName(torrent.Value.name);
128+
torrent.Value._so = StringConvert.SearchName(torrent.Value.originalname);
129+
}
130+
131+
fdb.savechanges = true;
132+
}
133+
}
134+
135+
FileDB.SaveChangesToFile();
136+
return Json(new { ok = true });
137+
}
114138
}
115139
}

Engine/FileDB/FileDB.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Globalization;
45
using System.IO;
@@ -109,12 +110,14 @@ void upt(bool uptfull = false, bool updatetime = true)
109110
if (!string.IsNullOrWhiteSpace(torrent.name) && torrent.name != t.name)
110111
{
111112
t.name = torrent.name;
113+
t._sn = StringConvert.SearchName(t.name);
112114
upt();
113115
}
114116

115117
if (!string.IsNullOrWhiteSpace(torrent.originalname) && torrent.originalname != t.originalname)
116118
{
117119
t.originalname = torrent.originalname;
120+
t._so = StringConvert.SearchName(t.originalname);
118121
upt();
119122
}
120123

0 commit comments

Comments
 (0)