11using Microsoft . AspNetCore . Mvc ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using Newtonsoft . Json ;
54using JacRed . Engine . CORE ;
65using System . Text . RegularExpressions ;
76using JacRed . Engine ;
1312using MonoTorrent ;
1413using JacRed . Models . Details ;
1514using JacRed . Models . Tracks ;
15+ using JacRed . Models . Api ;
1616
1717namespace 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}
0 commit comments