-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
filter.go
907 lines (779 loc) · 34 KB
/
filter.go
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package domain
import (
"context"
"regexp"
"strconv"
"strings"
"time"
"github.com/autobrr/autobrr/pkg/wildcard"
"github.com/dustin/go-humanize"
)
/*
Works the same way as for autodl-irssi
https://autodl-community.github.io/autodl-irssi/configuration/filter/
*/
type FilterRepo interface {
FindByID(ctx context.Context, filterID int) (*Filter, error)
FindByIndexerIdentifier(ctx context.Context, indexer string) ([]Filter, error)
Find(ctx context.Context, params FilterQueryParams) ([]Filter, error)
ListFilters(ctx context.Context) ([]Filter, error)
Store(ctx context.Context, filter Filter) (*Filter, error)
Update(ctx context.Context, filter Filter) (*Filter, error)
UpdatePartial(ctx context.Context, filter FilterUpdate) error
ToggleEnabled(ctx context.Context, filterID int, enabled bool) error
Delete(ctx context.Context, filterID int) error
StoreIndexerConnection(ctx context.Context, filterID int, indexerID int) error
StoreIndexerConnections(ctx context.Context, filterID int, indexers []Indexer) error
DeleteIndexerConnections(ctx context.Context, filterID int) error
GetDownloadsByFilterId(ctx context.Context, filterID int) (*FilterDownloads, error)
}
type FilterDownloads struct {
HourCount int
DayCount int
WeekCount int
MonthCount int
TotalCount int
}
type FilterMaxDownloadsUnit string
const (
FilterMaxDownloadsHour FilterMaxDownloadsUnit = "HOUR"
FilterMaxDownloadsDay FilterMaxDownloadsUnit = "DAY"
FilterMaxDownloadsWeek FilterMaxDownloadsUnit = "WEEK"
FilterMaxDownloadsMonth FilterMaxDownloadsUnit = "MONTH"
FilterMaxDownloadsEver FilterMaxDownloadsUnit = "EVER"
)
type FilterQueryParams struct {
Sort map[string]string
Filters struct {
Indexers []string
}
Search string
}
type Filter struct {
ID int `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
MinSize string `json:"min_size,omitempty"`
MaxSize string `json:"max_size,omitempty"`
Delay int `json:"delay,omitempty"`
Priority int32 `json:"priority"`
MaxDownloads int `json:"max_downloads,omitempty"`
MaxDownloadsUnit FilterMaxDownloadsUnit `json:"max_downloads_unit,omitempty"`
MatchReleases string `json:"match_releases,omitempty"`
ExceptReleases string `json:"except_releases,omitempty"`
UseRegex bool `json:"use_regex,omitempty"`
MatchReleaseGroups string `json:"match_release_groups,omitempty"`
ExceptReleaseGroups string `json:"except_release_groups,omitempty"`
Scene bool `json:"scene,omitempty"`
Origins []string `json:"origins,omitempty"`
ExceptOrigins []string `json:"except_origins,omitempty"`
Bonus []string `json:"bonus,omitempty"`
Freeleech bool `json:"freeleech,omitempty"`
FreeleechPercent string `json:"freeleech_percent,omitempty"`
SmartEpisode bool `json:"smart_episode"`
Shows string `json:"shows,omitempty"`
Seasons string `json:"seasons,omitempty"`
Episodes string `json:"episodes,omitempty"`
Resolutions []string `json:"resolutions,omitempty"` // SD, 480i, 480p, 576p, 720p, 810p, 1080i, 1080p.
Codecs []string `json:"codecs,omitempty"` // XviD, DivX, x264, h.264 (or h264), mpeg2 (or mpeg-2), VC-1 (or VC1), WMV, Remux, h.264 Remux (or h264 Remux), VC-1 Remux (or VC1 Remux).
Sources []string `json:"sources,omitempty"` // DSR, PDTV, HDTV, HR.PDTV, HR.HDTV, DVDRip, DVDScr, BDr, BD5, BD9, BDRip, BRRip, DVDR, MDVDR, HDDVD, HDDVDRip, BluRay, WEB-DL, TVRip, CAM, R5, TELESYNC, TS, TELECINE, TC. TELESYNC and TS are synonyms (you don't need both). Same for TELECINE and TC
Containers []string `json:"containers,omitempty"`
MatchHDR []string `json:"match_hdr,omitempty"`
ExceptHDR []string `json:"except_hdr,omitempty"`
MatchOther []string `json:"match_other,omitempty"`
ExceptOther []string `json:"except_other,omitempty"`
Years string `json:"years,omitempty"`
Artists string `json:"artists,omitempty"`
Albums string `json:"albums,omitempty"`
MatchReleaseTypes []string `json:"match_release_types,omitempty"` // Album,Single,EP
ExceptReleaseTypes string `json:"except_release_types,omitempty"`
Formats []string `json:"formats,omitempty"` // MP3, FLAC, Ogg, AAC, AC3, DTS
Quality []string `json:"quality,omitempty"` // 192, 320, APS (VBR), V2 (VBR), V1 (VBR), APX (VBR), V0 (VBR), q8.x (VBR), Lossless, 24bit Lossless, Other
Media []string `json:"media,omitempty"` // CD, DVD, Vinyl, Soundboard, SACD, DAT, Cassette, WEB, Other
PerfectFlac bool `json:"perfect_flac,omitempty"`
Cue bool `json:"cue,omitempty"`
Log bool `json:"log,omitempty"`
LogScore int `json:"log_score,omitempty"`
MatchCategories string `json:"match_categories,omitempty"`
ExceptCategories string `json:"except_categories,omitempty"`
MatchUploaders string `json:"match_uploaders,omitempty"`
ExceptUploaders string `json:"except_uploaders,omitempty"`
MatchLanguage []string `json:"match_language,omitempty"`
ExceptLanguage []string `json:"except_language,omitempty"`
Tags string `json:"tags,omitempty"`
ExceptTags string `json:"except_tags,omitempty"`
TagsAny string `json:"tags_any,omitempty"`
ExceptTagsAny string `json:"except_tags_any,omitempty"`
TagsMatchLogic string `json:"tags_match_logic,omitempty"`
ExceptTagsMatchLogic string `json:"except_tags_match_logic,omitempty"`
MatchReleaseTags string `json:"match_release_tags,omitempty"`
ExceptReleaseTags string `json:"except_release_tags,omitempty"`
UseRegexReleaseTags bool `json:"use_regex_release_tags,omitempty"`
MatchDescription string `json:"match_description,omitempty"`
ExceptDescription string `json:"except_description,omitempty"`
UseRegexDescription bool `json:"use_regex_description,omitempty"`
ExternalScriptEnabled bool `json:"external_script_enabled,omitempty"`
ExternalScriptCmd string `json:"external_script_cmd,omitempty"`
ExternalScriptArgs string `json:"external_script_args,omitempty"`
ExternalScriptExpectStatus int `json:"external_script_expect_status,omitempty"`
ExternalWebhookEnabled bool `json:"external_webhook_enabled,omitempty"`
ExternalWebhookHost string `json:"external_webhook_host,omitempty"`
ExternalWebhookData string `json:"external_webhook_data,omitempty"`
ExternalWebhookExpectStatus int `json:"external_webhook_expect_status,omitempty"`
ActionsCount int `json:"actions_count"`
Actions []*Action `json:"actions,omitempty"`
Indexers []Indexer `json:"indexers"`
Downloads *FilterDownloads `json:"-"`
}
type FilterUpdate struct {
ID int `json:"id"`
Name *string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
MinSize *string `json:"min_size,omitempty"`
MaxSize *string `json:"max_size,omitempty"`
Delay *int `json:"delay,omitempty"`
Priority *int32 `json:"priority,omitempty"`
MaxDownloads *int `json:"max_downloads,omitempty"`
MaxDownloadsUnit *FilterMaxDownloadsUnit `json:"max_downloads_unit,omitempty"`
MatchReleases *string `json:"match_releases,omitempty"`
ExceptReleases *string `json:"except_releases,omitempty"`
UseRegex *bool `json:"use_regex,omitempty"`
MatchReleaseGroups *string `json:"match_release_groups,omitempty"`
ExceptReleaseGroups *string `json:"except_release_groups,omitempty"`
MatchReleaseTags *string `json:"match_release_tags,omitempty"`
ExceptReleaseTags *string `json:"except_release_tags,omitempty"`
UseRegexReleaseTags *bool `json:"use_regex_release_tags,omitempty"`
MatchDescription *string `json:"match_description,omitempty"`
ExceptDescription *string `json:"except_description,omitempty"`
UseRegexDescription *bool `json:"use_regex_description,omitempty"`
Scene *bool `json:"scene,omitempty"`
Origins *[]string `json:"origins,omitempty"`
ExceptOrigins *[]string `json:"except_origins,omitempty"`
Bonus *[]string `json:"bonus,omitempty"`
Freeleech *bool `json:"freeleech,omitempty"`
FreeleechPercent *string `json:"freeleech_percent,omitempty"`
SmartEpisode *bool `json:"smart_episode,omitempty"`
Shows *string `json:"shows,omitempty"`
Seasons *string `json:"seasons,omitempty"`
Episodes *string `json:"episodes,omitempty"`
Resolutions *[]string `json:"resolutions,omitempty"` // SD, 480i, 480p, 576p, 720p, 810p, 1080i, 1080p.
Codecs *[]string `json:"codecs,omitempty"` // XviD, DivX, x264, h.264 (or h264), mpeg2 (or mpeg-2), VC-1 (or VC1), WMV, Remux, h.264 Remux (or h264 Remux), VC-1 Remux (or VC1 Remux).
Sources *[]string `json:"sources,omitempty"` // DSR, PDTV, HDTV, HR.PDTV, HR.HDTV, DVDRip, DVDScr, BDr, BD5, BD9, BDRip, BRRip, DVDR, MDVDR, HDDVD, HDDVDRip, BluRay, WEB-DL, TVRip, CAM, R5, TELESYNC, TS, TELECINE, TC. TELESYNC and TS are synonyms (you don't need both). Same for TELECINE and TC
Containers *[]string `json:"containers,omitempty"`
MatchHDR *[]string `json:"match_hdr,omitempty"`
ExceptHDR *[]string `json:"except_hdr,omitempty"`
MatchOther *[]string `json:"match_other,omitempty"`
ExceptOther *[]string `json:"except_other,omitempty"`
Years *string `json:"years,omitempty"`
Artists *string `json:"artists,omitempty"`
Albums *string `json:"albums,omitempty"`
MatchReleaseTypes *[]string `json:"match_release_types,omitempty"` // Album,Single,EP
ExceptReleaseTypes *string `json:"except_release_types,omitempty"`
Formats *[]string `json:"formats,omitempty"` // MP3, FLAC, Ogg, AAC, AC3, DTS
Quality *[]string `json:"quality,omitempty"` // 192, 320, APS (VBR), V2 (VBR), V1 (VBR), APX (VBR), V0 (VBR), q8.x (VBR), Lossless, 24bit Lossless, Other
Media *[]string `json:"media,omitempty"` // CD, DVD, Vinyl, Soundboard, SACD, DAT, Cassette, WEB, Other
PerfectFlac *bool `json:"perfect_flac,omitempty"`
Cue *bool `json:"cue,omitempty"`
Log *bool `json:"log,omitempty"`
LogScore *int `json:"log_score,omitempty"`
MatchCategories *string `json:"match_categories,omitempty"`
ExceptCategories *string `json:"except_categories,omitempty"`
MatchUploaders *string `json:"match_uploaders,omitempty"`
ExceptUploaders *string `json:"except_uploaders,omitempty"`
MatchLanguage *[]string `json:"match_language,omitempty"`
ExceptLanguage *[]string `json:"except_language,omitempty"`
Tags *string `json:"tags,omitempty"`
ExceptTags *string `json:"except_tags,omitempty"`
TagsAny *string `json:"tags_any,omitempty"`
ExceptTagsAny *string `json:"except_tags_any,omitempty"`
TagsMatchLogic *string `json:"tags_match_logic,omitempty"`
ExceptTagsMatchLogic *string `json:"except_tags_match_logic,omitempty"`
ExternalScriptEnabled *bool `json:"external_script_enabled,omitempty"`
ExternalScriptCmd *string `json:"external_script_cmd,omitempty"`
ExternalScriptArgs *string `json:"external_script_args,omitempty"`
ExternalScriptExpectStatus *int `json:"external_script_expect_status,omitempty"`
ExternalWebhookEnabled *bool `json:"external_webhook_enabled,omitempty"`
ExternalWebhookHost *string `json:"external_webhook_host,omitempty"`
ExternalWebhookData *string `json:"external_webhook_data,omitempty"`
ExternalWebhookExpectStatus *int `json:"external_webhook_expect_status,omitempty"`
Actions []*Action `json:"actions,omitempty"`
Indexers []Indexer `json:"indexers,omitempty"`
}
func (f Filter) CheckFilter(r *Release) ([]string, bool) {
// reset rejections first to clean previous checks
r.resetRejections()
// max downloads check. If reached return early
if f.MaxDownloads > 0 && !f.checkMaxDownloads(f.MaxDownloads, f.MaxDownloadsUnit) {
r.addRejectionF("max downloads (%d) this (%v) reached", f.MaxDownloads, f.MaxDownloadsUnit)
return r.Rejections, false
}
if len(f.Bonus) > 0 && !sliceContainsSlice(r.Bonus, f.Bonus) {
r.addRejectionF("bonus not matching. got: %v want: %v", r.Bonus, f.Bonus)
}
if f.Freeleech && r.Freeleech != f.Freeleech {
r.addRejection("wanted: freeleech")
}
if f.FreeleechPercent != "" && !checkFreeleechPercent(r.FreeleechPercent, f.FreeleechPercent) {
r.addRejectionF("freeleech percent not matching. got: %v want: %v", r.FreeleechPercent, f.FreeleechPercent)
}
if len(f.Origins) > 0 && !containsSlice(r.Origin, f.Origins) {
r.addRejectionF("origin not matching. got: %v want: %v", r.Origin, f.Origins)
}
if len(f.ExceptOrigins) > 0 && containsSlice(r.Origin, f.ExceptOrigins) {
r.addRejectionF("except origin not matching. got: %v unwanted: %v", r.Origin, f.ExceptOrigins)
}
// title is the parsed title
if f.Shows != "" && !contains(r.Title, f.Shows) {
r.addRejectionF("shows not matching. got: %v want: %v", r.Title, f.Shows)
}
if f.Seasons != "" && !containsIntStrings(r.Season, f.Seasons) {
r.addRejectionF("season not matching. got: %d want: %v", r.Season, f.Seasons)
}
if f.Episodes != "" && !containsIntStrings(r.Episode, f.Episodes) {
r.addRejectionF("episodes not matching. got: %d want: %v", r.Episode, f.Episodes)
}
// matchRelease
// match against regex
if f.UseRegex {
if f.MatchReleases != "" && !matchRegex(r.TorrentName, f.MatchReleases) {
r.addRejectionF("match release regex not matching. got: %v want: %v", r.TorrentName, f.MatchReleases)
}
if f.ExceptReleases != "" && matchRegex(r.TorrentName, f.ExceptReleases) {
r.addRejectionF("except releases regex: unwanted release. got: %v want: %v", r.TorrentName, f.ExceptReleases)
}
} else {
if f.MatchReleases != "" && !containsFuzzy(r.TorrentName, f.MatchReleases) {
r.addRejectionF("match release not matching. got: %v want: %v", r.TorrentName, f.MatchReleases)
}
if f.ExceptReleases != "" && containsFuzzy(r.TorrentName, f.ExceptReleases) {
r.addRejectionF("except releases: unwanted release. got: %v want: %v", r.TorrentName, f.ExceptReleases)
}
}
if f.MatchReleaseGroups != "" && !contains(r.Group, f.MatchReleaseGroups) {
r.addRejectionF("release groups not matching. got: %v want: %v", r.Group, f.MatchReleaseGroups)
}
if f.ExceptReleaseGroups != "" && contains(r.Group, f.ExceptReleaseGroups) {
r.addRejectionF("unwanted release group. got: %v unwanted: %v", r.Group, f.ExceptReleaseGroups)
}
// check raw releaseTags string
if f.UseRegexReleaseTags {
if f.MatchReleaseTags != "" && !matchRegex(r.ReleaseTags, f.MatchReleaseTags) {
r.addRejectionF("match release tags regex not matching. got: %v want: %v", r.ReleaseTags, f.MatchReleaseTags)
}
if f.ExceptReleaseTags != "" && matchRegex(r.ReleaseTags, f.ExceptReleaseTags) {
r.addRejectionF("except release tags regex: unwanted release. got: %v want: %v", r.ReleaseTags, f.ExceptReleaseTags)
}
} else {
if f.MatchReleaseTags != "" && !containsFuzzy(r.ReleaseTags, f.MatchReleaseTags) {
r.addRejectionF("match release tags not matching. got: %v want: %v", r.ReleaseTags, f.MatchReleaseTags)
}
if f.ExceptReleaseTags != "" && containsFuzzy(r.ReleaseTags, f.ExceptReleaseTags) {
r.addRejectionF("except release tags: unwanted release. got: %v want: %v", r.ReleaseTags, f.ExceptReleaseTags)
}
}
if f.MatchUploaders != "" && !contains(r.Uploader, f.MatchUploaders) {
r.addRejectionF("uploaders not matching. got: %v want: %v", r.Uploader, f.MatchUploaders)
}
if f.ExceptUploaders != "" && contains(r.Uploader, f.ExceptUploaders) {
r.addRejectionF("unwanted uploaders. got: %v unwanted: %v", r.Uploader, f.ExceptUploaders)
}
if len(f.MatchLanguage) > 0 && !sliceContainsSlice(r.Language, f.MatchLanguage) {
r.addRejectionF("language not matching. got: %v want: %v", r.Language, f.MatchLanguage)
}
if len(f.ExceptLanguage) > 0 && sliceContainsSlice(r.Language, f.ExceptLanguage) {
r.addRejectionF("language unwanted. got: %v want: %v", r.Language, f.ExceptLanguage)
}
if len(f.Resolutions) > 0 && !containsSlice(r.Resolution, f.Resolutions) {
r.addRejectionF("resolution not matching. got: %v want: %v", r.Resolution, f.Resolutions)
}
if len(f.Codecs) > 0 && !sliceContainsSlice(r.Codec, f.Codecs) {
r.addRejectionF("codec not matching. got: %v want: %v", r.Codec, f.Codecs)
}
if len(f.Sources) > 0 && !containsSlice(r.Source, f.Sources) {
r.addRejectionF("source not matching. got: %v want: %v", r.Source, f.Sources)
}
if len(f.Containers) > 0 && !containsSlice(r.Container, f.Containers) {
r.addRejectionF("container not matching. got: %v want: %v", r.Container, f.Containers)
}
// HDR is parsed into the Codec slice from rls
if len(f.MatchHDR) > 0 && !matchHDR(r.HDR, f.MatchHDR) {
r.addRejectionF("hdr not matching. got: %v want: %v", r.HDR, f.MatchHDR)
}
// HDR is parsed into the Codec slice from rls
if len(f.ExceptHDR) > 0 && matchHDR(r.HDR, f.ExceptHDR) {
r.addRejectionF("hdr unwanted. got: %v want: %v", r.HDR, f.ExceptHDR)
}
// Other is parsed into the Other slice from rls
if len(f.MatchOther) > 0 && !sliceContainsSlice(r.Other, f.MatchOther) {
r.addRejectionF("match other not matching. got: %v want: %v", r.Other, f.MatchOther)
}
// Other is parsed into the Other slice from rls
if len(f.ExceptOther) > 0 && sliceContainsSlice(r.Other, f.ExceptOther) {
r.addRejectionF("except other unwanted. got: %v unwanted: %v", r.Other, f.ExceptOther)
}
if f.Years != "" && !containsIntStrings(r.Year, f.Years) {
r.addRejectionF("year not matching. got: %d want: %v", r.Year, f.Years)
}
if f.MatchCategories != "" {
var categories []string
categories = append(categories, r.Categories...)
if r.Category != "" {
categories = append(categories, r.Category)
}
if !contains(r.Category, f.MatchCategories) && !containsAny(categories, f.MatchCategories) {
r.addRejectionF("category not matching. got: %v want: %v", strings.Join(categories, ","), f.MatchCategories)
}
}
if f.ExceptCategories != "" {
var categories []string
categories = append(categories, r.Categories...)
if r.Category != "" {
categories = append(categories, r.Category)
}
if contains(r.Category, f.ExceptCategories) && containsAny(categories, f.ExceptCategories) {
r.addRejectionF("category unwanted. got: %v unwanted: %v", strings.Join(categories, ","), f.ExceptCategories)
}
}
if len(f.MatchReleaseTypes) > 0 && !containsSlice(r.Category, f.MatchReleaseTypes) {
r.addRejectionF("release type not matching. got: %v want: %v", r.Category, f.MatchReleaseTypes)
}
if (f.MinSize != "" || f.MaxSize != "") && !f.checkSizeFilter(r, f.MinSize, f.MaxSize) {
r.addRejectionF("size not matching. got: %v want min: %v max: %v", r.Size, f.MinSize, f.MaxSize)
}
if f.Tags != "" {
if f.TagsMatchLogic == "ALL" && !containsAll(r.Tags, f.Tags) {
r.addRejectionF("tags not matching. got: %v want(all): %v", r.Tags, f.Tags)
} else if !containsAny(r.Tags, f.Tags) { // TagsMatchLogic is set to "" by default, this makes sure that "" and "ANY" are treated the same way.
r.addRejectionF("tags not matching. got: %v want: %v", r.Tags, f.Tags)
}
}
if f.ExceptTags != "" {
if f.ExceptTagsMatchLogic == "ALL" && containsAll(r.Tags, f.ExceptTags) {
r.addRejectionF("tags unwanted. got: %v don't want: %v", r.Tags, f.ExceptTags)
} else if containsAny(r.Tags, f.ExceptTags) { // ExceptTagsMatchLogic is set to "" by default, this makes sure that "" and "ANY" are treated the same way.
r.addRejectionF("tags unwanted. got: %v don't want: %v", r.Tags, f.ExceptTags)
}
}
if len(f.Artists) > 0 && !contains(r.Artists, f.Artists) {
r.addRejectionF("artists not matching. got: %v want: %v", r.Artists, f.Artists)
}
if len(f.Albums) > 0 && !contains(r.Title, f.Albums) {
r.addRejectionF("albums not matching. got: %v want: %v", r.Title, f.Albums)
}
// Perfect flac requires Cue, Log, Log Score 100, FLAC and 24bit Lossless
if f.PerfectFlac && !f.isPerfectFLAC(r) {
r.addRejectionF("wanted: perfect flac. got: %v", r.Audio)
}
if len(f.Formats) > 0 && !sliceContainsSlice(r.Audio, f.Formats) {
r.addRejectionF("formats not matching. got: %v want: %v", r.Audio, f.Formats)
}
if len(f.Quality) > 0 && !sliceContainsSlice(r.Audio, f.Quality) {
r.addRejectionF("quality not matching. got: %v want: %v", r.Audio, f.Quality)
}
if len(f.Media) > 0 && !containsSlice(r.Source, f.Media) {
r.addRejectionF("media not matching. got: %v want: %v", r.Source, f.Media)
}
if f.Cue && !containsAny(r.Audio, "Cue") {
r.addRejection("wanted: cue")
}
if f.Log && !containsAny(r.Audio, "Log") {
r.addRejection("wanted: log")
}
if f.Log && f.LogScore != 0 && r.LogScore != f.LogScore {
r.addRejectionF("log score. got: %v want: %v", r.LogScore, f.LogScore)
}
// check description string
if f.UseRegexDescription {
if f.MatchDescription != "" && !matchRegex(r.Description, f.MatchDescription) {
r.addRejectionF("match description regex not matching. got: %v want: %v", r.Description, f.MatchDescription)
}
if f.ExceptDescription != "" && matchRegex(r.Description, f.ExceptDescription) {
r.addRejectionF("except description regex: unwanted release. got: %v want: %v", r.Description, f.ExceptDescription)
}
} else {
if f.MatchDescription != "" && !containsFuzzy(r.Description, f.MatchDescription) {
r.addRejectionF("match description not matching. got: %v want: %v", r.Description, f.MatchDescription)
}
if f.ExceptDescription != "" && containsFuzzy(r.Description, f.ExceptDescription) {
r.addRejectionF("except description: unwanted release. got: %v want: %v", r.Description, f.ExceptDescription)
}
}
if len(r.Rejections) > 0 {
return r.Rejections, false
}
return nil, true
}
func (f Filter) checkMaxDownloads(max int, perTimeUnit FilterMaxDownloadsUnit) bool {
if f.Downloads == nil {
return false
}
switch perTimeUnit {
case FilterMaxDownloadsHour:
if f.Downloads.HourCount > 0 && f.Downloads.HourCount >= max {
return false
}
case FilterMaxDownloadsDay:
if f.Downloads.DayCount > 0 && f.Downloads.DayCount >= max {
return false
}
case FilterMaxDownloadsWeek:
if f.Downloads.WeekCount > 0 && f.Downloads.WeekCount >= max {
return false
}
case FilterMaxDownloadsMonth:
if f.Downloads.MonthCount > 0 && f.Downloads.MonthCount >= max {
return false
}
case FilterMaxDownloadsEver:
if f.Downloads.TotalCount > 0 && f.Downloads.TotalCount >= max {
return false
}
default:
return true
}
return true
}
// isPerfectFLAC Perfect is "CD FLAC Cue Log 100% Lossless or 24bit Lossless"
func (f Filter) isPerfectFLAC(r *Release) bool {
if !contains(r.Source, "CD") {
return false
}
if !containsAny(r.Audio, "Cue") {
return false
}
if !containsAny(r.Audio, "Log") {
return false
}
if !containsAny(r.Audio, "Log100") {
return false
}
if !containsAny(r.Audio, "FLAC") {
return false
}
if !containsAnySlice(r.Audio, []string{"Lossless", "24bit Lossless"}) {
return false
}
return true
}
// checkSizeFilter additional size check
// for indexers that doesn't announce size, like some gazelle based
// set flag r.AdditionalSizeCheckRequired if there's a size in the filter, otherwise go a head
// implement API for ptp,btn,ggn to check for size if needed
// for others pull down torrent and do check
func (f Filter) checkSizeFilter(r *Release, minSize string, maxSize string) bool {
if r.Size == 0 {
r.AdditionalSizeCheckRequired = true
return true
} else {
r.AdditionalSizeCheckRequired = false
}
// if r.Size parse filter to bytes and compare
// handle both min and max
if minSize != "" {
// string to bytes
minSizeBytes, err := humanize.ParseBytes(minSize)
if err != nil {
// log could not parse into bytes
}
if r.Size <= minSizeBytes {
r.addRejection("size: smaller than min size")
return false
}
}
if maxSize != "" {
// string to bytes
maxSizeBytes, err := humanize.ParseBytes(maxSize)
if err != nil {
// log could not parse into bytes
}
if r.Size >= maxSizeBytes {
r.addRejection("size: larger than max size")
return false
}
}
return true
}
func matchRegex(tag string, filterList string) bool {
if tag == "" {
return false
}
filters := strings.Split(filterList, ",")
for _, filter := range filters {
if filter == "" {
continue
}
re, err := regexp.Compile(`(?i)(?:` + filter + `)`)
if err != nil {
return false
}
match := re.MatchString(tag)
if match {
return true
}
}
return false
}
// checkFilterIntStrings "1,2,3-20"
func containsIntStrings(value int, filterList string) bool {
filters := strings.Split(filterList, ",")
for _, s := range filters {
s = strings.Replace(s, "%", "", -1)
s = strings.Trim(s, " ")
if strings.Contains(s, "-") {
minMax := strings.Split(s, "-")
// to int
min, err := strconv.ParseInt(minMax[0], 10, 32)
if err != nil {
return false
}
max, err := strconv.ParseInt(minMax[1], 10, 32)
if err != nil {
return false
}
if min > max {
// handle error
return false
} else {
// if announcePercent is greater than min and less than max return true
if value >= int(min) && value <= int(max) {
return true
}
}
}
filterInt, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return false
}
if int(filterInt) == value {
return true
}
}
return false
}
func contains(tag string, filter string) bool {
return containsMatch([]string{tag}, strings.Split(filter, ","))
}
func containsFuzzy(tag string, filter string) bool {
return containsMatchFuzzy([]string{tag}, strings.Split(filter, ","))
}
func containsSlice(tag string, filters []string) bool {
return containsMatch([]string{tag}, filters)
}
func containsAny(tags []string, filter string) bool {
return containsMatch(tags, strings.Split(filter, ","))
}
func containsAll(tags []string, filter string) bool {
return containsAllMatch(tags, strings.Split(filter, ","))
}
func containsAnyOther(filter string, tags ...string) bool {
return containsMatch(tags, strings.Split(filter, ","))
}
func sliceContainsSlice(tags []string, filters []string) bool {
return containsMatchBasic(tags, filters)
}
func containsMatchFuzzy(tags []string, filters []string) bool {
for _, tag := range tags {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
for _, filter := range filters {
if filter == "" {
continue
}
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
a := strings.ContainsAny(filter, "?|*")
if a {
match := wildcard.Match(filter, tag)
if match {
return true
}
} else if strings.Contains(tag, filter) {
return true
}
}
}
return false
}
func containsMatch(tags []string, filters []string) bool {
for _, tag := range tags {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
tag = strings.Trim(tag, " ")
for _, filter := range filters {
if filter == "" {
continue
}
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
a := strings.ContainsAny(filter, "?|*")
if a {
match := wildcard.Match(filter, tag)
if match {
return true
}
} else if tag == filter {
return true
}
}
}
return false
}
func containsAllMatch(tags []string, filters []string) bool {
for _, filter := range filters {
if filter == "" {
continue
}
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
found := false
for _, tag := range tags {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
tag = strings.Trim(tag, " ")
if tag == filter {
found = true
break
} else if strings.ContainsAny(filter, "?|*") {
if wildcard.Match(filter, tag) {
found = true
break
}
}
}
if !found {
return false
}
}
return true
}
func containsMatchBasic(tags []string, filters []string) bool {
for _, tag := range tags {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
for _, filter := range filters {
if filter == "" {
continue
}
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
if tag == filter {
return true
}
}
}
return false
}
func containsAnySlice(tags []string, filters []string) bool {
for _, tag := range tags {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
for _, filter := range filters {
if filter == "" {
continue
}
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
wild := strings.ContainsAny(filter, "?|*")
if wild {
match := wildcard.Match(filter, tag)
if match {
return true
}
} else if tag == filter {
return true
}
}
}
return false
}
func checkFreeleechPercent(announcePercent int, filterPercent string) bool {
filters := strings.Split(filterPercent, ",")
for _, s := range filters {
s = strings.Replace(s, "%", "", -1)
s = strings.Trim(s, " ")
if strings.Contains(s, "-") {
minMax := strings.Split(s, "-")
// to int
min, err := strconv.ParseInt(minMax[0], 10, 32)
if err != nil {
return false
}
max, err := strconv.ParseInt(minMax[1], 10, 32)
if err != nil {
return false
}
if min > max {
// handle error
return false
} else {
// if announcePercent is greater than min and less than max return true
if announcePercent >= int(min) && announcePercent <= int(max) {
return true
}
}
}
filterPercentInt, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return false
}
if int(filterPercentInt) == announcePercent {
return true
}
}
return false
}
func matchHDR(releaseValues []string, filterValues []string) bool {
for _, filter := range filterValues {
if filter == "" {
continue
}
filter = strings.ToLower(filter)
filter = strings.Trim(filter, " ")
parts := strings.Split(filter, " ")
if len(parts) == 2 {
partsMatched := 0
for _, part := range parts {
for _, tag := range releaseValues {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
if tag == part {
partsMatched++
}
if len(parts) == partsMatched {
return true
}
}
}
} else {
for _, tag := range releaseValues {
if tag == "" {
continue
}
tag = strings.ToLower(tag)
if tag == filter {
return true
}
}
}
}
return false
}