aboutsummaryrefslogtreecommitdiffstats
path: root/parser/parser.go
blob: a2087bf621c54dcf54391935b200cd98a125a389 (plain) (blame)
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
package parser

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"text/template"
)

var (
	moviePattern    = regexp.MustCompile(`(.*?)\.(\d{4})`)
	episodePatterns = [4]*regexp.Regexp{
		regexp.MustCompile(`^(?P<name>.+?)\.S(?P<season>\d{2})(?:E(?P<episode>\d{2}))?`), // S01, S01E04
		regexp.MustCompile(`^(?P<name>.+?)\.E(?P<episode>\d{2})`),                        // E04
		regexp.MustCompile(`^(?P<name>.+?)\.(?P<season>\d{1,2})x(?P<episode>\d{2})`),     // 1x04, 01x04
		regexp.MustCompile(`^(?P<name>.+?)\.P(?:ar)?t\.?(?P<episode>([^.]+))`),           // P(ar)t(.)11, Pt(.)XI
	}
	splitPattern = regexp.MustCompile(`[-_.]`)
)

type Parser func(s string) (Media, error)

type Media struct {
	Release    string
	Name       string
	Year       int
	Season     int
	Episode    int
	Resolution string
	Codec      string
}

func (m *Media) IsEmpty() bool {
	return m.Name == ""
}

func (m *Media) ReplaceName(re *regexp.Regexp, repl string) {
	m.Name = re.ReplaceAllString(m.Name, repl)
}

func (m *Media) Equal(o Media) bool {
	if m.IsEmpty() {
		return false
	}
	return m.Name == o.Name &&
		m.Season == o.Season &&
		m.Episode == o.Episode &&
		m.Year == o.Year &&
		m.Resolution == o.Resolution &&
		m.Codec == o.Codec
}

func (m *Media) PathIn(dir *template.Template) (string, error) {
	var b bytes.Buffer
	if err := dir.Execute(&b, m); err != nil {
		return "", err
	}
	path := b.String()
	// When path has a trailing slash, the actual destination path will be a directory inside LocalPath (same
	// behaviour as rsync)
	if strings.HasSuffix(path, string(os.PathSeparator)) {
		path = filepath.Join(path, m.Release)
	}
	return path, nil
}

func Default(s string) (Media, error) {
	return Media{Release: s}, nil
}

func Movie(s string) (Media, error) {
	matches := moviePattern.FindStringSubmatch(s)
	if len(matches) < 3 {
		return Media{}, fmt.Errorf("invalid input: %q", s)
	}
	name := matches[1]
	year, err := strconv.Atoi(matches[2])
	if err != nil {
		return Media{}, fmt.Errorf("invalid input: %q: %s", s, err)
	}
	return Media{
		Release:    s,
		Name:       name,
		Year:       year,
		Resolution: resolution(s),
		Codec:      codec(s),
	}, nil
}

func Show(s string) (Media, error) {
	for _, p := range episodePatterns {
		matches := p.FindStringSubmatch(s)
		if len(matches) == 0 {
			continue
		}
		groupNames := p.SubexpNames()
		var (
			name    string
			season  = 1
			episode = 0
			err     error
		)
		for i, group := range matches {
			if group == "" {
				continue
			}
			switch groupNames[i] {
			case "name":
				name = group
			case "season":
				season, err = strconv.Atoi(group)
				if err != nil {
					return Media{}, fmt.Errorf("invalid input: %q: %w", s, err)
				}
			case "episode":
				episode, err = strconv.Atoi(group)
				if err != nil {
					episode, err = rtoi(group)
					if err != nil {
						return Media{}, fmt.Errorf("invalid input: %q: %w", s, err)
					}
				}
			}
		}
		return Media{
			Release:    s,
			Name:       name,
			Season:     season,
			Episode:    episode,
			Resolution: resolution(s),
			Codec:      codec(s),
		}, nil
	}
	return Media{}, fmt.Errorf("invalid input: %q", s)
}

func findPart(s string, partFunc func(part string) bool) string {
	s = strings.ToLower(s)
	parts := splitPattern.Split(s, -1)
	for _, part := range parts {
		if partFunc(part) {
			return part
		}
	}
	return ""
}

func resolution(s string) string {
	return findPart(s, func(part string) bool {
		switch part {
		case "720p", "1080p", "2160p":
			return true
		}
		return false
	})
}

func codec(s string) string {
	return findPart(s, func(part string) bool {
		switch part {
		case "xvid", "x264", "x265":
			return true
		}
		return false
	})
}

var numerals = []numeral{
	// units
	{"IX", 9},
	{"VIII", 8},
	{"VII", 7},
	{"VI", 6},
	{"IV", 4},
	{"III", 3},
	{"II", 2},
	{"V", 5},
	{"I", 1},
	// tens
	{"XC", 90},
	{"LXXX", 80},
	{"LXX", 70},
	{"LX", 60},
	{"XL", 40},
	{"XXX", 30},
	{"XX", 20},
	{"L", 50},
	{"X", 10},
	// hundreds
	{"CM", 900},
	{"DCCC", 800},
	{"DCC", 700},
	{"DC", 600},
	{"CD", 400},
	{"CCC", 300},
	{"CC", 200},
	{"D", 500},
	{"C", 100},
	// thousands
	{"MMM", 3000},
	{"MM", 2000},
	{"M", 1000},
}

type numeral struct {
	s string
	n int
}

func (n numeral) parse(s string, lastIndex int) (string, int, int) {
	i := strings.LastIndex(s, n.s)
	if i > -1 && i < lastIndex {
		rest := s[:i] + s[i+len(n.s):]
		return rest, i, n.n
	}
	return s, lastIndex, 0
}

func rtoi(s string) (int, error) {
	// needlessly complete parsing of roman numerals
	sum := 0
	lastIndex := len(s)
	for _, num := range numerals {
		var n int
		s, lastIndex, n = num.parse(s, lastIndex)
		sum += n
		if s == "" {
			break
		}
	}
	if s != "" {
		return 0, fmt.Errorf("invalid roman numeral: %q", s)
	}
	return sum, nil
}