-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_season.go
319 lines (251 loc) · 10.1 KB
/
parser_season.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
package habari
import (
"strconv"
"strings"
)
// parseSeason looks for season/volume/part prefixes and numbers.
// It will also check for episode numbers.
// e.g. S01E01, Season 1, S1, 1st Season, 1st Volume, 1st Part, 1st Season - 03, 1st Season - 03v2, 1st Season - 03' etc...
func (p *parser) parseSeason() {
for _, tkn := range *p.tokenManager.tokens {
if !tkn.isUnknown() { // Don't bother if token is already known
continue // Skip to next token
}
// Parse S01E01 by checking if the token follows the pattern
if strings.HasPrefix(tkn.getNormalizedValue(), "S") && len(tkn.getValue()) > 3 {
// Extract season and episode
if season, sep, episode, ok := extractSeasonAndEpisode(tkn.getValue()); ok {
seasonPrefixTkn := newToken("S")
seasonPrefixTkn.setIdentifiedKeywordCategory(keywordCatSeasonPrefix, keywordKindCombinedWithNumber)
seasonPrefixTkn.setKind(tokenKindCharacter)
seasonTkn := newToken(season)
seasonTkn.setMetadataCategory(metadataSeason)
seasonTkn.setKind(tokenKindNumber)
sepTkn := newToken(sep)
sepTkn.setIdentifiedKeywordCategory(keywordCatEpisodePrefix, keywordKindCombinedWithNumber)
sepTkn.setKind(tokenKindCharacter)
episodeTkn := newToken(episode)
episodeTkn.setMetadataCategory(metadataEpisodeNumber)
episodeTkn.setKind(tokenKindNumberLike)
if isNumber(episode) {
episodeTkn.setKind(tokenKindNumber)
p.tokenManager.tokens.checkNumberWithDecimal(episodeTkn) // Check if number is decimal
}
p.tokenManager.tokens.overwriteAndInsertManyAt(p.tokenManager.tokens.getIndexOf(tkn), []*token{seasonPrefixTkn, seasonTkn, sepTkn, episodeTkn})
episodeIsZeroPadded := isNumberZeroPadded(episodeTkn.getValue())
// Check range
// e.g. S1-3, S01-03, S1 ~ 3, S01 ~ 03
if nextNumTkn, found, kind := checkNumberRangeAfterToken(p, seasonTkn, episodeIsZeroPadded); found {
// e.g. S1-3, S01-03
if kind == 0 {
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
p.tokenManager.tokens.checkNumberWithDecimal(nextNumTkn) // Check if number is decimal
continue // Skip to next token
}
}
continue // Skip to next token
}
}
// Combined or separated seasons/volumes/parts
if strings.HasPrefix(tkn.getNormalizedValue(), "S") ||
strings.HasPrefix(tkn.getNormalizedValue(), "P") ||
strings.HasPrefix(tkn.getNormalizedValue(), "V") {
keywords, found := p.tokenManager.keywordManager.findKeywordsBy(func(kw *keyword) bool {
return (kw.isSeasonPrefix() || kw.isVolumePrefix() || kw.isPartPrefix()) && // Season/Part/Volume prefix
strings.HasPrefix(tkn.getNormalizedValue(), kw.value) // Token starts with season prefix
})
if !found {
continue // Skip to next token
}
keywordLoop:
for _, keyword := range keywords {
// e.g. S01
if keyword.isCombinedWithNumber() {
metadataCat := getMetadataCategoryFromKeywordPrefix(keyword.category)
// Check if token is after file metadata
if p.tokenManager.tokens.isTokenAfterFileMetadata(tkn) {
break keywordLoop // Skip to next token
}
// Check if prefix is followed by a number or number-like (e.g. 01, 01v2)
remaining := strings.TrimPrefix(tkn.getNormalizedValue(), keyword.value)
if len(remaining) > 0 && isNumberOrLike(remaining) {
// e.g. S
seasonPrefixTkn := newToken(keyword.value)
seasonPrefixTkn.setIdentifiedKeywordCategory(keyword.category, keyword.kind)
seasonPrefixTkn.setKind(tokenKindWord)
// e.g. 01, 1, 3
seasonTkn := newToken(tkn.getValue()[len(keyword.value):])
seasonTkn.setMetadataCategory(metadataCat)
seasonTkn.setKind(tokenKindNumberLike)
if isNumber(remaining) {
seasonTkn.setKind(tokenKindNumber)
p.tokenManager.tokens.checkNumberWithDecimal(seasonTkn) // Check if number is decimal
}
firstSeasonIsZeroPadded := isNumberZeroPadded(remaining)
// Overwrite and insert tokens
p.tokenManager.tokens.overwriteAndInsertManyAt(p.tokenManager.tokens.getIndexOf(tkn), []*token{seasonPrefixTkn, seasonTkn})
// Check if the number identified is a decimal number
// e.g. "S1" "." "5" -> "S1.5"
if isNumber(remaining) { // e.g. S1.5, don't bother if S1v2
p.tokenManager.tokens.checkNumberWithDecimal(seasonTkn) // Check if number is decimal
}
// Check range
// e.g. S1-3, S01-03, S1 ~ 3, S01 ~ 03
if nextNumTkn, found, kind := checkNumberRangeAfterToken(p, seasonTkn, firstSeasonIsZeroPadded); found {
// e.g. S1-3, S01-03
if kind == 0 {
nextNumTkn.setMetadataCategory(metadataCat)
p.tokenManager.tokens.checkNumberWithDecimal(nextNumTkn) // Check if number is decimal
break keywordLoop // Skip to next token
}
// e.g. S01 - 03
if kind == 1 {
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
p.tokenManager.tokens.checkNumberWithDecimal(nextNumTkn) // Check if number is decimal
break keywordLoop // Skip to next token
}
}
}
}
// e.g. Season 01
if keyword.isSeparatedWithNumber() {
metadataCat := getMetadataCategoryFromKeywordPrefix(keyword.category)
// Get next token, by skipping delimiters
// Check if next token is a number or number-like
if nextTkn, found, _ := p.tokenManager.tokens.getTokenAfterSD(tkn); found &&
(nextTkn.isNumberOrLikeKind() && nextTkn.isUnknown()) {
tkn.setIdentifiedKeywordCategory(keyword.category, keyword.kind)
nextTkn.setMetadataCategory(metadataCat)
// Check range
firstSeasonIsZeroPadded := isNumberZeroPadded(nextTkn.getValue())
// e.g. Season 1-3, Season 01-03, Season 1 ~ 3, Season 01 ~ 03
if nextNumTkn, found, kind := checkNumberRangeAfterToken(p, nextTkn, firstSeasonIsZeroPadded); found {
// e.g. Season 1-3, Season 01-03
if kind == 0 {
nextNumTkn.setMetadataCategory(metadataCat)
break keywordLoop // Skip to next token
}
// e.g. Season 01 - 03
if kind == 1 {
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
break keywordLoop // Skip to next token
}
}
break keywordLoop // Skip to next token
}
}
// e.g. 1st Season, first season
if keyword.isOrdinalSuffix() {
// Get previous token, by skipping delimiters
// Check if next token is an ordinal number
if nextTkn, found, _ := p.tokenManager.tokens.getTokenBeforeSD(tkn); found &&
(nextTkn.isOrdinalNumber() && nextTkn.isUnknown()) {
tkn.setIdentifiedKeywordCategory(keyword.category, keyword.kind)
if num, ok := getNumberFromOrdinal(nextTkn.getValue()); ok {
nextTkn.setValue(strconv.Itoa(num))
nextTkn.setMetadataCategory(metadataSeason)
nextTkn.setKind(tokenKindNumber)
// Check range ONLY for episode
// e.g. 1st Season - 03
if nextNumTkn, found, kind := checkNumberRangeAfterToken(p, tkn, false); found {
// e.g. S01 - 03
if kind == 1 {
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
break keywordLoop // Skip to next token
}
}
break keywordLoop // Skip to next token
}
}
}
}
}
// Parse 01x01
if strings.Contains(tkn.getNormalizedValue(), "X") && len(tkn.getValue()) > 3 {
// Extract season and episode
if season, sep, episode, ok := extractSeasonAndEpisode(tkn.getValue()); ok {
if len(season) > 2 {
continue // Skip to next token
}
seasonTkn := newToken(season)
seasonTkn.setMetadataCategory(metadataSeason)
seasonTkn.setKind(tokenKindNumber)
sepTkn := newToken(sep)
sepTkn.setIdentifiedKeywordCategory(keywordCatEpisodePrefix, keywordKindCombinedWithNumber)
sepTkn.setKind(tokenKindCharacter)
episodeTkn := newToken(episode)
episodeTkn.setMetadataCategory(metadataEpisodeNumber)
if isNumber(episode) {
episodeTkn.setKind(tokenKindNumber)
} else {
episodeTkn.setKind(tokenKindNumberLike)
}
p.tokenManager.tokens.overwriteAndInsertManyAt(p.tokenManager.tokens.getIndexOf(tkn), []*token{seasonTkn, sepTkn, episodeTkn})
continue // Skip to next token
}
}
}
}
func checkNumberRangeAfterToken(p *parser, tkn *token, prevNumberIsPadded bool) (*token, bool, int) {
var nextNumTkn *token
found := false
var kind int // 0 = season, 1 = episode
// Check range
// e.g. S1-3, S01-03, S1 ~ 3, S01 ~ 03
for {
if rangeTkns, ok, dlSkipped := p.tokenManager.tokens.getCategorySequenceAfter(p.tokenManager.tokens.getIndexOf(tkn), []tokenCategory{
tokenCatSeparator, // -
tokenCatUnknown, // 05
}, true); ok {
nextNumTkn = rangeTkns[1]
// Check episode
if rangeTkns[1].isNumberOrLikeKind() && rangeTkns[0].isDashSeparator() {
// e.g. S1 - 03, S01- 03
if dlSkipped > 0 {
if intVal, err := strconv.Atoi(nextNumTkn.getValue()); err == nil {
// e.g. if < 10 -> 01, 02, 03. if > 10 -> 11, 12, 13
if (intVal < 10 && isNumberZeroPadded(nextNumTkn.getValue())) || (intVal >= 10) {
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
kind = 1
found = true
break
}
} else { // /!\ might need to do some additional checks on the number
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
kind = 1
found = true
break
}
// e.g. S1-03 (dlSkipped = 0) Where 03 might be an episode. This is not very likely
} else if !prevNumberIsPadded && isNumberZeroPadded(nextNumTkn.getValue()) {
nextNumTkn.setMetadataCategory(metadataSeason)
kind = 1
found = true
break
}
}
// Avoid this case: S1-2v2
if !nextNumTkn.isNumberKind() {
found = false
break
}
intVal, err := strconv.Atoi(nextNumTkn.getValue())
if err != nil {
found = false
break
}
// Avoid this case: S01 - 3
if intVal < 10 && (prevNumberIsPadded && !isNumberZeroPadded(nextNumTkn.getValue())) {
found = false
break
}
// e.g. S1-3
nextNumTkn.setMetadataCategory(metadataSeason)
kind = 0
found = true
break
}
break
}
return nextNumTkn, found, kind
}