๐Ÿ“ฆ Monty / ConvertEpisodeSequences

๐Ÿ“„ compress_newEpisodes.go ยท 374 lines
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
374package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
)

// ANSI color codes for warnings and errors
const (
	yellowWarning = "\033[33mWarning\033[0m"
	redError      = "\033[31mError\033[0m"
)

// Compiled regular expressions
var (
	patRe  = regexp.MustCompile(`(?i)^(.*),\s*S(\d+)E(\d+),\s*(.*)$`)
	epRe   = regexp.MustCompile(`(?i)\bEpisode\s*(\d{1,4})`)
	ptRe   = regexp.MustCompile(`(?i)\bPart\s*(\d{1,4})`)
	epPtRe = regexp.MustCompile(`(?i)\b(Episode|Part)\s*\d{1,4}`)
	wsRe   = regexp.MustCompile(`\s+`)
)

// ParsedLine represents a parsed episode line
type ParsedLine struct {
	original string
	prefix   string
	season   string
	episode  string
	suffix   string
}

// normalizeQuotedLine removes all quotes from lines that start with '"' and end with '"""'
func normalizeQuotedLine(s string) string {
	trimmed := strings.TrimSpace(s)
	if strings.HasPrefix(s, `"`) && strings.HasSuffix(trimmed, `"""`) {
		return strings.ReplaceAll(s, `"`, "")
	}
	return s
}

// parseLine parses a normalized line into its components
func parseLine(line string) *ParsedLine {
	matches := patRe.FindStringSubmatch(line)
	if matches == nil {
		return nil
	}

	return &ParsedLine{
		original: fmt.Sprintf("%s, S%sE%s, %s", matches[1], matches[2], matches[3], matches[4]),
		prefix:   matches[1],
		season:   matches[2],
		episode:  matches[3],
		suffix:   matches[4],
	}
}

// normalizeLineForComparison removes Episode/Part numbers for comparison
func normalizeLineForComparison(s string) string {
	t := epPtRe.ReplaceAllString(s, "")
	t = wsRe.ReplaceAllString(t, " ")
	t = strings.Trim(t, " :,-")
	return strings.ToLower(t)
}

// isSameArc determines if two lines belong to the same show/season/arc
func isSameArc(a, b *ParsedLine) bool {
	return a.prefix == b.prefix &&
		a.season == b.season &&
		normalizeLineForComparison(a.suffix) == normalizeLineForComparison(b.suffix)
}

// isConsecutiveEpisode checks if all numbers (main E, Episode, Part) are sequential
func isConsecutiveEpisode(a, b *ParsedLine) bool {
	// 1. Check main episode number
	aEp, _ := strconv.Atoi(a.episode)
	bEp, _ := strconv.Atoi(b.episode)
	if bEp != aEp+1 {
		return false
	}

	// 2. Check for 'Episode N' in title
	aEpMatch := epRe.FindStringSubmatch(a.suffix)
	bEpMatch := epRe.FindStringSubmatch(b.suffix)

	if aEpMatch != nil && bEpMatch != nil {
		aNum, _ := strconv.Atoi(aEpMatch[1])
		bNum, _ := strconv.Atoi(bEpMatch[1])
		if bNum != aNum+1 {
			return false
		}
	} else if (aEpMatch == nil) != (bEpMatch == nil) {
		// One has "Episode N" and one doesn't
		return false
	}

	// 3. Check for 'Part N' in title
	aPtMatch := ptRe.FindStringSubmatch(a.suffix)
	bPtMatch := ptRe.FindStringSubmatch(b.suffix)

	if aPtMatch != nil && bPtMatch != nil {
		aNum, _ := strconv.Atoi(aPtMatch[1])
		bNum, _ := strconv.Atoi(bPtMatch[1])
		if bNum != aNum+1 {
			return false
		}
	} else if (aPtMatch == nil) != (bPtMatch == nil) {
		// One has "Part N" and one doesn't
		return false
	}

	return true
}

// getBestWarningNum determines the best number to use when warning about a gap
func getBestWarningNum(p *ParsedLine) string {
	// Check for Part number
	if ptMatch := ptRe.FindStringSubmatch(p.suffix); ptMatch != nil {
		return ptMatch[1]
	}

	// Check for Episode number
	if epMatch := epRe.FindStringSubmatch(p.suffix); epMatch != nil {
		return epMatch[1]
	}

	// Fallback to main episode number
	return p.episode
}

// appendGroup emits a compressed or single line for a group of parsed entries
func appendGroup(group []*ParsedLine, outputLines *[]string) {
	if len(group) == 0 {
		return
	}

	// Single line, nothing to compress
	if len(group) == 1 {
		*outputLines = append(*outputLines, group[0].original)
		return
	}

	first := group[0]
	last := group[len(group)-1]
	e1 := first.episode
	e2 := last.episode
	s1 := first.suffix

	// Extract episode/part numbers
	var e1Text, e2Text, p1Text, p2Text string

	if ep1Match := epRe.FindStringSubmatch(s1); ep1Match != nil {
		e1Text = ep1Match[1]
		if ep2Match := epRe.FindStringSubmatch(last.suffix); ep2Match != nil {
			e2Text = ep2Match[1]
			// Preserve padding
			if strings.HasPrefix(e1Text, "0") && len(e1Text) > 1 {
				num, _ := strconv.Atoi(e2Text)
				e2Text = fmt.Sprintf("%0*d", len(e1Text), num)
			}
		}
	}

	if pt1Match := ptRe.FindStringSubmatch(s1); pt1Match != nil {
		p1Text = pt1Match[1]
		if pt2Match := ptRe.FindStringSubmatch(last.suffix); pt2Match != nil {
			p2Text = pt2Match[1]
			// Preserve padding
			if strings.HasPrefix(p1Text, "0") && len(p1Text) > 1 {
				num, _ := strconv.Atoi(p2Text)
				p2Text = fmt.Sprintf("%0*d", len(p1Text), num)
			}
		}
	}

	// Replace Episode/Part sequences with appropriate ranges
	newSuffix := epPtRe.ReplaceAllStringFunc(s1, func(match string) string {
		parts := epPtRe.FindStringSubmatch(match)
		if len(parts) < 2 {
			return match
		}

		word := parts[1]
		wordLower := strings.ToLower(word)

		if wordLower == "episode" && e1Text != "" && e2Text != "" {
			return fmt.Sprintf("%s %s-%s", word, e1Text, e2Text)
		}
		if wordLower == "part" && p1Text != "" && p2Text != "" {
			return fmt.Sprintf("%s %s-%s", word, p1Text, p2Text)
		}
		return match
	})

	line := fmt.Sprintf("%s, S%sE%s-%s, %s", first.prefix, first.season, e1, e2, newSuffix)
	*outputLines = append(*outputLines, line)
}

// squishLines processes all normalized lines and groups/compresses consecutive entries
func squishLines(lines []string, progName string) []string {
	var outputLines []string
	var group []*ParsedLine

	for _, line := range lines {
		p := parseLine(line)
		if p != nil {
			if len(group) == 0 {
				// Always start a new group if one isn't active
				group = append(group, p)
			} else {
				lastInGroup := group[len(group)-1]
				if isSameArc(lastInGroup, p) {
					// It's the same show/season/arc
					if isConsecutiveEpisode(lastInGroup, p) {
						// It's consecutive, add it to the group
						group = append(group, p)
					} else {
						// GAP DETECTED - same show but not consecutive
						startNum := getBestWarningNum(lastInGroup)
						endNum := getBestWarningNum(p)

						fmt.Fprintf(os.Stderr, "%s: [%s] %s S%s: missing episodes between %s and %s\n",
							progName, yellowWarning, p.prefix, p.season, startNum, endNum)

						// Flush the old group
						appendGroup(group, &outputLines)

						// Start a new group with the current item
						group = []*ParsedLine{p}
					}
				} else {
					// It's a different show/season/arc - normal break
					appendGroup(group, &outputLines)
					group = []*ParsedLine{p}
				}
			}
		} else {
			// Not a parsable line (e.g., a header)
			appendGroup(group, &outputLines)
			group = nil

			// Fix missing space after comma in non-parsable lines
			if strings.Contains(line, ",") && !strings.Contains(line, ", ") {
				line = strings.ReplaceAll(line, ",", ", ")
			}

			outputLines = append(outputLines, line)
		}
	}

	appendGroup(group, &outputLines)
	return outputLines
}

// readLines reads all lines from a reader
func readLines(reader io.Reader) ([]string, error) {
	var lines []string
	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		line := normalizeQuotedLine(scanner.Text())
		lines = append(lines, line)
	}
	return lines, scanner.Err()
}

// isTerminal checks if a file descriptor is a terminal
func isTerminal(file *os.File) bool {
	stat, err := file.Stat()
	if err != nil {
		return false
	}
	return (stat.Mode() & os.ModeCharDevice) != 0
}

// printHelp prints the help message
func printHelp(progName string) {
	fmt.Printf(`Reads show episode listings from stdin or a file and compresses consecutive episode sequences.

For example:
  Bannan, S08E01, Episode 01
  Bannan, S08E02, Episode 02
becomes:
  Bannan, S08E01-02, Episode 01-02

Usage:
  %s [input_file]
  %s -h, --help

Arguments:
  input_file    Optional input filename (reads from stdin if omitted)

Options:
  -h, --help    Show this help message and exit

Examples:
  ./newEpisodes.sh | ./%s > newEpisodes.txt
  ./%s episodes.txt > newEpisodes.txt
`, progName, progName, progName, progName)
}

func main() {
	progName := filepath.Base(os.Args[0])

	// Parse arguments
	args := os.Args[1:]

	// Check for help flags
	if len(args) > 0 {
		for _, arg := range args {
			if arg == "-h" || arg == "--help" {
				printHelp(progName)
				os.Exit(0)
			}
		}
	}

	// Check for unknown flags (starting with -)
	for _, arg := range args {
		if strings.HasPrefix(arg, "-") {
			fmt.Fprintf(os.Stderr, "%s: [%s] Unrecognized argument '%s'\n", progName, redError, arg)
			os.Exit(2)
		}
	}

	var reader io.Reader

	if len(args) == 0 {
		// No arguments, check if stdin is a terminal
		if isTerminal(os.Stdin) {
			fmt.Fprintf(os.Stderr, "%s: [%s] No input file or piped input\n", progName, redError)
			fmt.Fprintf(os.Stderr, "Usage: ./%s <file> or use a pipe, e.g.:\n", progName)
			fmt.Fprintf(os.Stderr, "  ./%s episodes.txt\n", progName)
			fmt.Fprintf(os.Stderr, "  ./newEpisodes.sh | ./%s\n", progName)
			os.Exit(2)
		}
		reader = os.Stdin
	} else if len(args) == 1 {
		// One argument - treat as filename
		file, err := os.Open(args[0])
		if err != nil {
			if os.IsNotExist(err) {
				fmt.Fprintf(os.Stderr, "%s: [%s] File '%s' does not exist.\n", progName, redError, args[0])
				os.Exit(1)
			}
			fmt.Fprintf(os.Stderr, "%s: [%s] Error opening file: %v\n", progName, redError, err)
			os.Exit(1)
		}
		defer file.Close() // nolint:errcheck
		reader = file
	} else {
		fmt.Fprintf(os.Stderr, "%s: [%s] Too many arguments\n", progName, redError)
		os.Exit(2)
	}

	// Read and process lines
	lines, err := readLines(reader)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: [%s] Error reading input: %v\n", progName, redError, err)
		os.Exit(1)
	}

	squished := squishLines(lines, progName)

	// Write output
	for _, line := range squished {
		fmt.Println(line)
	}
}