-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqualityparser.go
351 lines (280 loc) · 8.72 KB
/
qualityparser.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
package releaseinfo
import (
"path/filepath"
"strconv"
"strings"
"github.com/dlclark/regexp2"
)
var (
sourceRegex = regexp2.MustCompile(
`\b(?:(?<bluray>BluRay|Blu-Ray|HDDVD|BD)|(?<webdl>WEB[-_. ]DL|WEBDL|WebRip|iTunesHD|WebHD|[. ]WEB[. ](?:[xh]26[45]|DD5[. ]1)|\d+0p[. ]WEB[. ])|(?<hdtv>HDTV)|(?<bdrip>BDRip)|(?<brrip>BRRip)|(?<dvd>DVD|DVDRip|NTSC|PAL|xvidvd)|(?<dsr>WS[-_. ]DSR|DSR)|(?<pdtv>PDTV)|(?<sdtv>SDTV)|(?<tvrip>TVRip))\b`,
regexp2.Compiled|regexp2.IgnoreCase)
rawHDRegex = regexp2.MustCompile(
`\b(?<rawhd>RawHD|1080i[-_. ]HDTV|Raw[-_. ]HD|MPEG[-_. ]?2)\b`,
regexp2.Compiled|regexp2.IgnoreCase)
versionRegex = regexp2.MustCompile(
`\dv(?<version>\d)\b|\[v(?<version>\d)\]`,
regexp2.Compiled|regexp2.IgnoreCase)
properRegex = regexp2.MustCompile(
`\b(?<real>real[-_. ])*(?<proper>proper|repack|rerip)(?<real>[-_. ]real)*\b`,
regexp2.Compiled|regexp2.IgnoreCase)
resolutionRegex = regexp2.MustCompile(
`\b(?:(?<_480p>480p|640x480|848x480)|(?<_576p>576p)|(?<_720p>720p|1280x720)|(?<_1080p>1080p|1920x1080)|(?<_2160p>2160p))\b`,
regexp2.Compiled|regexp2.IgnoreCase)
codecRegex = regexp2.MustCompile(
`\b(?:(?<x264>x264)|(?<h264>h264)|(?<xvidhd>XvidHD)|(?<xvid>Xvid)|(?<divx>divx))\b`,
regexp2.Compiled|regexp2.IgnoreCase)
otherSourceRegex = regexp2.MustCompile(
`(?<hdtv>HD[-_. ]TV)|(?<sdtv>SD[-_. ]TV)`,
regexp2.Compiled|regexp2.IgnoreCase)
animeBlurayRegex = regexp2.MustCompile(
`bd(?:720|1080)|(?<=[-_. (\[])bd(?=[-_. )\]])`,
regexp2.Compiled|regexp2.IgnoreCase)
highDefPdtvRegex = regexp2.MustCompile(`hr[-_. ]ws`,
regexp2.Compiled|regexp2.IgnoreCase)
)
type sourceMatches map[string][]string
func (s sourceMatches) Has(key string) bool {
return len(s[key]) > 0
}
func findSourceMatches(name string) sourceMatches {
matches := sourceMatches{}
sourceMatch, _ := sourceRegex.FindStringMatch(name)
for sourceMatch != nil {
for _, group := range sourceMatch.Groups() {
switch group.Name {
case "bluray", "webdl", "hdtv", "bdrip", "brrip", "dvd", "dsr", "pdtv", "sdtv", "tvrip":
for _, capture := range group.Captures {
if _, exists := matches[group.Name]; !exists {
matches[group.Name] = []string{}
}
matches[group.Name] = append(matches[group.Name], capture.String())
}
}
}
sourceMatch, _ = sourceRegex.FindNextMatch(sourceMatch)
}
return matches
}
func ParseQuality(name string) QualityModel {
// log.Printf("Parsing quality for %q", name)
normalizedName := removeSpace(name)
normalizedName = strings.Replace(normalizedName, "_", " ", -1)
normalizedName = removeSpace(normalizedName)
normalizedName = strings.ToLower(normalizedName)
result := parseQualityModifiers(name, normalizedName)
if match, _ := rawHDRegex.FindStringMatch(normalizedName); match != nil {
result.Quality = QualityRAWHD
return result
}
sourceMatches := findSourceMatches(normalizedName)
resolution := ParseResolution(normalizedName)
codecRegex, _ := codecRegex.FindStringMatch(normalizedName)
if len(sourceMatches) > 0 {
if sourceMatches.Has("bluray") {
if codecRegex != nil {
if hasGroup(codecRegex, "xvid") || hasGroup(codecRegex, "divx") {
result.Quality = QualityDVD
return result
}
}
if resolution == Resolution2160p {
result.Quality = QualityBluray2160p
return result
}
if resolution == Resolution1080p {
result.Quality = QualityBluray1080p
return result
}
if resolution == Resolution480p || resolution == Resolution576p {
result.Quality = QualityDVD
return result
}
result.Quality = QualityBluray720p
return result
}
if sourceMatches.Has("webdl") {
if resolution == Resolution2160p {
result.Quality = QualityWEBDL2160p
return result
}
if resolution == Resolution1080p {
result.Quality = QualityWEBDL1080p
return result
}
if resolution == Resolution720p {
result.Quality = QualityWEBDL720p
return result
}
if strings.Contains(name, "[WEBDL]") {
result.Quality = QualityWEBDL720p
return result
}
result.Quality = QualityWEBDL480p
return result
}
if sourceMatches.Has("hdtv") {
if resolution == Resolution2160p {
result.Quality = QualityHDTV2160p
return result
}
if resolution == Resolution1080p {
result.Quality = QualityHDTV1080p
return result
}
if resolution == Resolution720p {
result.Quality = QualityHDTV720p
return result
}
if strings.Contains(name, "[HDTV]") {
result.Quality = QualityHDTV720p
return result
}
result.Quality = QualitySDTV
return result
}
if sourceMatches.Has("bdrip") || sourceMatches.Has("brrip") {
switch resolution {
case Resolution720p:
result.Quality = QualityBluray720p
return result
case Resolution1080p:
result.Quality = QualityBluray1080p
return result
default:
result.Quality = QualityDVD
return result
}
}
if sourceMatches.Has("dvd") {
result.Quality = QualityDVD
return result
}
if sourceMatches.Has("pdtv") ||
sourceMatches.Has("sdtv") ||
sourceMatches.Has("dsr") ||
sourceMatches.Has("tvrip") {
if match, _ := highDefPdtvRegex.FindStringMatch(normalizedName); match != nil {
result.Quality = QualityHDTV720p
return result
}
result.Quality = QualitySDTV
return result
}
}
//Anime Bluray matching
if match, _ := animeBlurayRegex.FindStringMatch(normalizedName); match != nil {
if resolution == Resolution480p || resolution == Resolution576p || strings.Contains(normalizedName, "480p") {
result.Quality = QualityDVD
return result
}
if resolution == Resolution1080p || strings.Contains(normalizedName, "1080p") {
result.Quality = QualityBluray1080p
return result
}
result.Quality = QualityBluray720p
return result
}
if resolution == Resolution2160p {
result.Quality = QualityHDTV2160p
return result
}
if resolution == Resolution1080p {
result.Quality = QualityHDTV1080p
return result
}
if resolution == Resolution720p {
result.Quality = QualityHDTV720p
return result
}
if resolution == Resolution480p {
result.Quality = QualitySDTV
return result
}
if codecRegex != nil && hasGroup(codecRegex, "x264") {
result.Quality = QualitySDTV
return result
}
if strings.Contains(normalizedName, "848x480") {
if strings.Contains(normalizedName, "dvd") {
result.Quality = QualityDVD
}
result.Quality = QualitySDTV
}
if strings.Contains(normalizedName, "1280x720") {
if strings.Contains(normalizedName, "bluray") {
result.Quality = QualityBluray720p
}
result.Quality = QualityHDTV720p
}
if strings.Contains(normalizedName, "1920x1080") {
if strings.Contains(normalizedName, "bluray") {
result.Quality = QualityBluray1080p
}
result.Quality = QualityHDTV1080p
}
if strings.Contains(normalizedName, "bluray720p") {
result.Quality = QualityBluray720p
}
if strings.Contains(normalizedName, "bluray1080p") {
result.Quality = QualityBluray1080p
}
if otherSourceMatch := otherSourceMatch(normalizedName); otherSourceMatch != QualityUnknown {
result.Quality = otherSourceMatch
}
//Based on extension
if result.Quality == QualityUnknown {
result.Quality = getQualityForExtension(filepath.Ext(normalizedName))
result.QualitySource = "extension"
}
return result
}
func ParseResolution(name string) Resolution {
match, _ := resolutionRegex.FindStringMatch(name)
switch {
case hasGroup(match, "_480p"):
return Resolution480p
case hasGroup(match, "_576p"):
return Resolution576p
case hasGroup(match, "_720p"):
return Resolution720p
case hasGroup(match, "_1080p"):
return Resolution1080p
case hasGroup(match, "_2160p"):
return Resolution2160p
}
return ResolutionUnknown
}
func otherSourceMatch(name string) Quality {
match, _ := otherSourceRegex.FindStringMatch(name)
switch {
case hasGroup(match, "sdtv"):
return QualitySDTV
case hasGroup(match, "hdtv"):
return QualityHDTV720p
}
return QualityUnknown
}
func parseQualityModifiers(name string, normalizedName string) QualityModel {
result := QualityModel{Quality: QualityUnknown, QualitySource: "name"}
versionRegexResult, _ := versionRegex.FindStringMatch(normalizedName)
if versionRegexResult != nil {
version, _ := strconv.Atoi(versionRegexResult.GroupByName("version").String())
result.Revision = version - 1
}
properRegexResult, _ := properRegex.FindStringMatch(normalizedName)
if properRegexResult != nil {
result.Revision += len(properRegexResult.GroupByName("proper").Captures)
result.Revision += len(properRegexResult.GroupByName("real").Captures)
}
return result
}
type Resolution string
const (
Resolution480p Resolution = "480p"
Resolution576p Resolution = "576p"
Resolution720p Resolution = "720p"
Resolution1080p Resolution = "1080p"
Resolution2160p Resolution = "2160p"
ResolutionUnknown Resolution = "unknown"
)