mkm3u: Overhaul video filename parsing again.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 17 Mar 2022 22:30:23 +0000 (22:30 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 17 Mar 2022 22:30:23 +0000 (22:30 +0000)
Now we have a list of (much simpler!) patterns which we try one by one.
This handling oddball filenames significantly easier.

mkm3u

diff --git a/mkm3u b/mkm3u
index 9b308aa..08ef4ff 100755 (executable)
--- 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<si> \d+)
-              (?: \. \ (?P<st> .*) — (?: D \d+ \. \ )? |
-                  D \d+ \. \ |
-                  (?= E \d+ \. \ ) |
-                  \. \ ) |
-            (?P<epnum> \d+) \. \ )
-        (?: (?P<eplist>
-             (?: S \d+ \ )? E \d+ (?: – \d+)?
-             (?: , \ (?: S \d+ \ )? E \d+ (?: – \d+)?)*) |
-            (?P<epname> E \d+) \. \ .* |
-            .*)
-        \. iso $
-  """, RX.X)
+  _R_ISO_PRE = list(map(lambda pat: RX.compile("^" + pat + r"\.iso$", RX.X),
+    [r""" S? (?P<si> \d+) [A-Z]? \. \ (?P<stitle> .*) —
+            (?: D \d+ \. \ )?
+          (?P<epex> .*) """,
+     r""" S (?P<si> \d+) (?: D \d+)? \. \ (?P<epex> .*) """,
+     r""" S (?P<si> \d+) \. \ (?P<epex> E \d+ .*) """,
+     r""" S (?P<si> \d+) \. \ (?P<epex> E \d+ .*) """,
+     r""" S (?P<si> \d+) (?P<epex> E \d+) \. \ .* """,
+     r""" \d+ \. \ (?P<epex> [ES] \d+ .*) """,
+     r""" (?P<epnum> \d+ ) \. \ .* """]))
 
   _R_ISO_EP = RX.compile(r""" ^
         (?: S (?P<si> \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)