forked from gjrtimmer/nzb
/
subject.go
100 lines (79 loc) · 2.69 KB
/
subject.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
package nzb
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
subjectName = regexp.MustCompile(`^([^-]*)`)
subjectFilenameRegex = regexp.MustCompile(`"([^"]*)"`)
subjectPartsRegex = regexp.MustCompile(`\[([0-9]+)\/([0-9]+)\]`)
subjectYEncPartsRegex = regexp.MustCompile(`\(([0-9]+)\/([0-9]+)\)`)
)
// Subject of NZB File
type Subject string
func (s Subject) extractName() (string, error) {
match := subjectName.FindStringSubmatch(string(s))
if match == nil {
return "", fmt.Errorf("unable to extract download name from '%s'", string(s))
}
name := strings.TrimSpace(match[1])
return name, nil
}
// ExtractFilename from subject
func (s Subject) ExtractFilename() (string, error) {
match := subjectFilenameRegex.FindStringSubmatch(string(s))
if match == nil {
return "", fmt.Errorf("unable to extract filename from '%s'", string(s))
}
// Filename extracted
return match[1], nil
}
// ExtractPartNumber from subject
// Example: ...[12/77]..., this will extract 12
// only if it is available within the subject
func (s Subject) ExtractPartNumber() (int, error) {
match := subjectPartsRegex.FindStringSubmatch(string(s))
if match == nil {
return 0, fmt.Errorf("unable to extract part number from '%s'", string(s))
}
p, _ := strconv.Atoi(match[1])
return p, nil
}
// ExtractTotalParts from subject
// Example: ...[12/77]..., this will extract 77
// only if it is available within the subject
func (s Subject) ExtractTotalParts() (int, error) {
match := subjectPartsRegex.FindStringSubmatch(string(s))
if match == nil {
return 0, fmt.Errorf("unable to extract total parts from '%s'", string(s))
}
p, _ := strconv.Atoi(match[2])
return p, nil
}
// ExtractYEncPartNumber from subject
// Example: ...(001/420)..., this will extract 001
// yEnc encoding parts can almost
// alwwys be found at the end of a subject string
func (s Subject) ExtractYEncPartNumber() (int, error) {
match := subjectYEncPartsRegex.FindStringSubmatch(string(s))
if match == nil {
return 0, fmt.Errorf("unable to extract yEnc part from '%s'", string(s))
}
p, _ := strconv.Atoi(match[1])
return p, nil
}
// ExtractYEncTotalParts from subject
// Example: ...(001/420)..., this will extract 420
// yEnc encoding parts can almost
// alwwys be found at the end of a subject string
func (s Subject) ExtractYEncTotalParts() (int, error) {
match := subjectYEncPartsRegex.FindStringSubmatch(string(s))
if match == nil {
return 0, fmt.Errorf("unable to extract yEnc total parts from '%s'", string(s))
}
p, _ := strconv.Atoi(match[2])
return p, nil
}
// EOF