From dcb1cc6cc8b41a7899e61c81f769b08083505678 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Thu, 17 Mar 2022 22:30:23 +0000 Subject: [PATCH] mkm3u: Overhaul video filename parsing again. Organization: Straylight/Edgeware From: Mark Wooding Now we have a list of (much simpler!) patterns which we try one by one. This handling oddball filenames significantly easier. --- mkm3u | 75 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/mkm3u b/mkm3u index 9b308aa..08ef4ff 100755 --- a/mkm3u +++ b/mkm3u @@ -149,28 +149,26 @@ class VideoSeason (object): raise ExpectedError("season %d episode %d already taken" % (me.i, i)) me.episodes[i] = disc; disc.neps += 1 -def some_group(m, *gg): - for g in gg: - s = m.group(g) +def match_group(m, *groups, dflt = None, mustp = False): + for g in groups: + try: s = m.group(g) + except IndexError: continue if s is not None: return s - return None + if mustp: raise ValueError("no match found") + else: return dflt class VideoDir (object): - _R_ISO_PRE = RX.compile(r""" ^ - (?: S (?P \d+) - (?: \. \ (?P .*) — (?: D \d+ \. \ )? | - D \d+ \. \ | - (?= E \d+ \. \ ) | - \. \ ) | - (?P \d+) \. \ ) - (?: (?P - (?: S \d+ \ )? E \d+ (?: – \d+)? - (?: , \ (?: S \d+ \ )? E \d+ (?: – \d+)?)*) | - (?P E \d+) \. \ .* | - .*) - \. iso $ - """, RX.X) + _R_ISO_PRE = list(map(lambda pat: RX.compile("^" + pat + r"\.iso$", RX.X), + [r""" S? (?P \d+) [A-Z]? \. \ (?P .*) — + (?: D \d+ \. \ )? + (?P .*) """, + r""" S (?P \d+) (?: D \d+)? \. \ (?P .*) """, + r""" S (?P \d+) \. \ (?P E \d+ .*) """, + r""" S (?P \d+) \. \ (?P E \d+ .*) """, + r""" S (?P \d+) (?P E \d+) \. \ .* """, + r""" \d+ \. \ (?P [ES] \d+ .*) """, + r""" (?P \d+ ) \. \ .* """])) _R_ISO_EP = RX.compile(r""" ^ (?: S (?P \d+) \ )? @@ -186,22 +184,26 @@ class VideoDir (object): for fn in fns: path = OS.path.join(dir, fn) if not fn.endswith(".iso"): continue - m = me._R_ISO_PRE.match(fn) - if not m: - #print(";; `%s' ignored (regex mismatch)" % path, file = SYS.stderr) + #print(";; `%s'" % path, file = SYS.stderr) + for r in me._R_ISO_PRE: + m = r.match(fn) + if m: break + else: + #print(";;\tignored (regex mismatch)", file = SYS.stderr) continue - i = filter(m.group("si"), int) - stitle = m.group("st") - check(i is not None or stitle is None, + si = filter(match_group(m, "si"), int) + stitle = match_group(m, "stitle") + + check(si is not None or stitle is None, "explicit season title without number in `%s'" % fn) - if i is not None: - if season is None or i != season.i: - check(season is None or i == season.i + 1, + if si is not None: + if season is None or si != season.i: + check(season is None or si == season.i + 1, "season %d /= %d" % - (i, season is None and -1 or season.i + 1)) - check(i not in seasons, "season %d already seen" % i) - seasons[i] = season = VideoSeason(i, stitle) + (si, season is None and -1 or season.i + 1)) + check(si not in seasons, "season %d already seen" % si) + seasons[si] = season = VideoSeason(si, stitle) else: check(stitle == season.title, "season title `%s' /= `%s'" % (stitle, season.title)) @@ -209,14 +211,9 @@ class VideoDir (object): disc = VideoDisc(path) ts = season any, bad = False, False - epname = m.group("epname") - epexpr = m.group("eplist") - epnum = m.group("epnum") - if epname is not None: eplist = [epname] - elif epexpr is not None: eplist = epexpr.split(", ") - elif epnum is not None: eplist = ["E" + epnum] - else: continue - #print(";; `%s'" % path, file = SYS.stderr) + epnum = match_group(m, "epnum") + if epnum is not None: eplist = ["E" + epnum] + else: eplist = match_group(m, "epex", mustp = True).split(", ") for eprange in eplist: mm = me._R_ISO_EP.match(eprange) if mm is None: @@ -235,7 +232,7 @@ class VideoDir (object): ts.set_episode_disc(k, disc) #print(";;\tepisode %d.%d" % (ts.i, k), file = SYS.stderr) if not any: - #print(";;\tignored" % path, file = SYS.stderr) + #print(";;\tignored", file = SYS.stderr) pass elif bad: raise ExpectedError("bad ep list in `%s'", fn) -- [mdw]