-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
521 lines (485 loc) · 11.4 KB
/
Copy pathparser.go
File metadata and controls
521 lines (485 loc) · 11.4 KB
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
package ptt
import (
"regexp"
"strings"
)
// Parser is the main parser that uses registered handlers to extract information.
type Parser struct {
handlers []Handler
}
// Handler is a function that attempts to extract information from a title.
type Handler func(ctx *ParseContext) *MatchResult
// ParseContext contains the current state during parsing.
type ParseContext struct {
Title string
RawTitle string
Result *TorrentInfo
Matched map[string]*MatchInfo
}
// MatchInfo stores information about a matched pattern.
type MatchInfo struct {
RawMatch string
MatchIndex int
}
// MatchResult is returned by handlers when they find a match.
type MatchResult struct {
RawMatch string
MatchIndex int
Remove bool
SkipFromTitle bool
}
// HandlerOptions configures handler behavior.
type HandlerOptions struct {
Remove bool
SkipIfAlreadyFound bool
SkipIfAlreadyFoundSet bool
SkipFromTitle bool
SkipIfFirst bool
Value interface{}
}
var beforeTitleMatchRegex = regexp.MustCompile(`^\[([^\[\]]+)\]`)
// DefaultHandlerOptions returns the default options for handlers.
func DefaultHandlerOptions() HandlerOptions {
return HandlerOptions{
SkipIfAlreadyFound: true,
SkipIfAlreadyFoundSet: true,
Remove: false,
SkipFromTitle: false,
SkipIfFirst: false,
}
}
// NewParser creates a new Parser instance.
func NewParser() *Parser {
return &Parser{
handlers: make([]Handler, 0),
}
}
// AddHandler adds a handler function to the parser.
func (p *Parser) AddHandler(handler Handler) {
p.handlers = append(p.handlers, handler)
}
// AddRegexHandler creates and adds a handler from a regex pattern.
func (p *Parser) AddRegexHandler(name string, pattern *regexp.Regexp, transformer Transformer, opts HandlerOptions) {
handler := createHandlerFromRegexp(name, pattern, transformer, opts)
p.handlers = append(p.handlers, handler)
}
// Parse parses a title and returns the extracted information.
func (p *Parser) Parse(title string) *TorrentInfo {
return p.ParseWithOptions(title, ParseOptions{})
}
// ParseWithOptions parses a title with custom options.
func (p *Parser) ParseWithOptions(title string, opts ParseOptions) *TorrentInfo {
// Replace underscores with spaces
rawTitle := title
title = strings.ReplaceAll(title, "_", " ")
result := &TorrentInfo{
Seasons: []int{},
Episodes: []int{},
Languages: []string{},
}
matched := make(map[string]*MatchInfo)
endOfTitle := len(title)
ctx := &ParseContext{
Title: title,
RawTitle: title,
Result: result,
Matched: matched,
}
for _, handler := range p.handlers {
matchResult := handler(ctx)
if matchResult == nil {
continue
}
matchIndex := matchResult.MatchIndex
rawMatch := matchResult.RawMatch
if matchResult.Remove && matchIndex >= 0 && matchIndex+len(rawMatch) <= len(ctx.Title) {
ctx.Title = ctx.Title[:matchIndex] + ctx.Title[matchIndex+len(rawMatch):]
}
if !matchResult.SkipFromTitle && matchIndex > 1 && matchIndex < endOfTitle {
endOfTitle = matchIndex
}
if matchResult.Remove && matchResult.SkipFromTitle && matchIndex < endOfTitle {
endOfTitle -= len(rawMatch)
}
}
// Clean the title up to endOfTitle
if endOfTitle > 0 && endOfTitle <= len(ctx.Title) {
title = ctx.Title[:endOfTitle]
} else {
title = ctx.Title
}
result.Title = cleanTitle(title)
// Translate languages if requested
if opts.TranslateLanguages && len(result.Languages) > 0 {
result.Languages = translateLanguages(result.Languages)
}
// Anime fallback for common group markers
if !result.Anime && isAnimeByGroup(rawTitle) {
result.Anime = true
}
return result
}
// createHandlerFromRegexp creates a handler function from a regex pattern.
func createHandlerFromRegexp(name string, pattern *regexp.Regexp, transformer Transformer, opts HandlerOptions) Handler {
return func(ctx *ParseContext) *MatchResult {
if !opts.SkipIfAlreadyFoundSet {
opts.SkipIfAlreadyFound = true
}
// Check if already found and should skip
if opts.SkipIfAlreadyFound && hasField(ctx.Result, name) {
return nil
}
match := pattern.FindStringSubmatchIndex(ctx.Title)
if match == nil {
return nil
}
// Get the full match and first capture group
fullMatch := ctx.Title[match[0]:match[1]]
cleanMatch := fullMatch
if len(match) >= 4 && match[2] >= 0 {
cleanMatch = ctx.Title[match[2]:match[3]]
}
// Check skipIfFirst - skip if this match appears before all other matches (excluding same field)
if opts.SkipIfFirst {
earliest := true
hasOther := false
for key, info := range ctx.Matched {
if key == name || info == nil {
continue
}
hasOther = true
if match[0] >= info.MatchIndex {
earliest = false
break
}
}
if hasOther && earliest {
return nil
}
}
// Apply transformer
transformed := transformer(cleanMatch, ctx.Result, name)
if transformed == nil {
return nil
}
// Determine if this match should skip from title (before-title bracket)
skipFromTitle := opts.SkipFromTitle
if beforeTitle := beforeTitleMatchRegex.FindStringSubmatch(ctx.Title); len(beforeTitle) > 1 {
if strings.Contains(beforeTitle[1], fullMatch) {
skipFromTitle = true
}
}
// Store match info
if ctx.Matched[name] == nil {
ctx.Matched[name] = &MatchInfo{
RawMatch: fullMatch,
MatchIndex: match[0],
}
}
// Set the value
if opts.Value != nil {
setField(ctx.Result, name, opts.Value)
} else {
setField(ctx.Result, name, transformed)
}
return &MatchResult{
RawMatch: fullMatch,
MatchIndex: match[0],
Remove: opts.Remove,
SkipFromTitle: skipFromTitle,
}
}
}
// hasField checks if a field has been set in the result.
func hasField(result *TorrentInfo, name string) bool {
switch name {
case "title":
return result.Title != ""
case "year":
return result.Year != 0
case "resolution":
return result.Resolution != ""
case "quality":
return result.Quality != ""
case "codec":
return result.Codec != ""
case "bit_depth":
return result.BitDepth != ""
case "group":
return result.Group != ""
case "edition":
return result.Edition != ""
case "network":
return result.Network != ""
case "container":
return result.Container != ""
case "extension":
return result.Extension != ""
case "date":
return result.Date != ""
case "site":
return result.Site != ""
case "size":
return result.Size != ""
case "region":
return result.Region != ""
case "episode_code":
return result.EpisodeCode != ""
case "country":
return result.Country != ""
case "bitrate":
return result.Bitrate != ""
case "anime":
return result.Anime
case "adult":
return result.Adult
case "complete":
return result.Complete
case "dubbed":
return result.Dubbed
case "subbed":
return result.Subbed
case "hardcoded":
return result.Hardcoded
case "proper":
return result.Proper
case "repack":
return result.Repack
case "retail":
return result.Retail
case "remastered":
return result.Remastered
case "unrated":
return result.Unrated
case "uncensored":
return result.Uncensored
case "documentary":
return result.Documentary
case "convert":
return result.Convert
case "upscaled":
return result.Upscaled
case "scene":
return result.Scene
case "ppv":
return result.PPV
case "3d":
return result.ThreeD
case "trash":
return result.Trash
case "torrent":
return result.Torrent
case "commentary":
return result.Commentary
case "seasons":
return len(result.Seasons) > 0
case "episodes":
return len(result.Episodes) > 0
case "languages":
return len(result.Languages) > 0
case "audio":
return len(result.Audio) > 0
case "channels":
return len(result.Channels) > 0
case "hdr":
return len(result.HDR) > 0
case "volumes":
return len(result.Volumes) > 0
case "extras":
return len(result.Extras) > 0
}
return false
}
// setField sets a field in the result by name.
func setField(result *TorrentInfo, name string, value interface{}) {
switch name {
case "title":
if v, ok := value.(string); ok {
result.Title = v
}
case "year":
if v, ok := value.(int); ok {
result.Year = v
}
case "resolution":
if v, ok := value.(string); ok {
result.Resolution = v
}
case "quality":
if v, ok := value.(string); ok {
result.Quality = v
}
case "codec":
if v, ok := value.(string); ok {
result.Codec = v
}
case "bit_depth":
if v, ok := value.(string); ok {
result.BitDepth = v
}
case "group":
if v, ok := value.(string); ok {
result.Group = v
}
case "edition":
if v, ok := value.(string); ok {
result.Edition = v
}
case "network":
if v, ok := value.(string); ok {
result.Network = v
}
case "container":
if v, ok := value.(string); ok {
result.Container = v
}
case "extension":
if v, ok := value.(string); ok {
result.Extension = v
}
case "date":
if v, ok := value.(string); ok {
result.Date = v
}
case "site":
if v, ok := value.(string); ok {
result.Site = v
}
case "size":
if v, ok := value.(string); ok {
result.Size = v
}
case "region":
if v, ok := value.(string); ok {
result.Region = v
}
case "episode_code":
if v, ok := value.(string); ok {
result.EpisodeCode = v
}
case "country":
if v, ok := value.(string); ok {
result.Country = v
}
case "bitrate":
if v, ok := value.(string); ok {
result.Bitrate = v
}
case "anime":
if v, ok := value.(bool); ok {
result.Anime = v
}
case "adult":
if v, ok := value.(bool); ok {
result.Adult = v
}
case "complete":
if v, ok := value.(bool); ok {
result.Complete = v
}
case "dubbed":
if v, ok := value.(bool); ok {
result.Dubbed = v
}
case "subbed":
if v, ok := value.(bool); ok {
result.Subbed = v
}
case "hardcoded":
if v, ok := value.(bool); ok {
result.Hardcoded = v
}
case "proper":
if v, ok := value.(bool); ok {
result.Proper = v
}
case "repack":
if v, ok := value.(bool); ok {
result.Repack = v
}
case "retail":
if v, ok := value.(bool); ok {
result.Retail = v
}
case "remastered":
if v, ok := value.(bool); ok {
result.Remastered = v
}
case "unrated":
if v, ok := value.(bool); ok {
result.Unrated = v
}
case "uncensored":
if v, ok := value.(bool); ok {
result.Uncensored = v
}
case "documentary":
if v, ok := value.(bool); ok {
result.Documentary = v
}
case "convert":
if v, ok := value.(bool); ok {
result.Convert = v
}
case "upscaled":
if v, ok := value.(bool); ok {
result.Upscaled = v
}
case "scene":
if v, ok := value.(bool); ok {
result.Scene = v
}
case "ppv":
if v, ok := value.(bool); ok {
result.PPV = v
}
case "3d":
if v, ok := value.(bool); ok {
result.ThreeD = v
}
case "trash":
if v, ok := value.(bool); ok {
result.Trash = v
}
case "torrent":
if v, ok := value.(bool); ok {
result.Torrent = v
}
case "commentary":
if v, ok := value.(bool); ok {
result.Commentary = v
}
case "seasons":
if v, ok := value.([]int); ok {
result.Seasons = v
}
case "episodes":
if v, ok := value.([]int); ok {
result.Episodes = v
}
case "languages":
if v, ok := value.([]string); ok {
result.Languages = v
}
case "audio":
if v, ok := value.([]string); ok {
result.Audio = v
}
case "channels":
if v, ok := value.([]string); ok {
result.Channels = v
}
case "hdr":
if v, ok := value.([]string); ok {
result.HDR = v
}
case "volumes":
if v, ok := value.([]int); ok {
result.Volumes = v
}
case "extras":
if v, ok := value.([]string); ok {
result.Extras = v
}
}
}