### -*- mode: python; coding: utf-8 -*-
from contextlib import contextmanager
+import errno as E
import optparse as OP
import os as OS
import re as RX
+import sqlite3 as SQL
+import subprocess as SP
import sys as SYS
class ExpectedError (Exception): pass
if not s.isdigit(): raise ExpectedError("bad integer `%s'" % s)
return int(s)
+def getbool(s):
+ if s == "t": return True
+ elif s == "nil": return False
+ else: raise ExpectedError("bad boolean `%s'" % s)
+
+def quote(s):
+ if s is None: return "-"
+ else: return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"'
+
class Words (object):
def __init__(me, s):
me._s = s
if begin < 0: return None
else: return s[begin:].rstrip()
+def program_output(*args, **kw):
+ try: return SP.check_output(*args, **kw)
+ except SP.CalledProcessError as e:
+ raise ExpectedError("program `%s' failed with code %d" %
+ (e.cmd, e.returncode))
+
URL_SAFE_P = 256*[False]
for ch in \
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
b"abcdefghijklmnopqrstuvwxyz" \
- b"0123456789" b"!$%-.,/":
+ b"0123456789" b"!$%_-.,/":
URL_SAFE_P[ch] = True
def urlencode(s):
return "".join((URL_SAFE_P[ch] and chr(ch) or "%%%02x" % ch
LOC = DummyLocation()
+ROOT = "/mnt/dvd/archive/"
+DB = None
+
+def init_db(fn):
+ global DB
+ DB = SQL.connect(fn)
+ DB.cursor().execute("PRAGMA journal_mode = WAL")
+
+def setup_db(fn):
+ try: OS.unlink(fn)
+ except OSError as e:
+ if e.errno == E.ENOENT: pass
+ else: raise
+ init_db(fn)
+ DB.cursor().execute("""
+ CREATE TABLE duration
+ (path TEXT NOT NULL,
+ title INTEGER NOT NULL,
+ start_chapter INTEGER NOT NULL,
+ end_chapter INTEGER NOT NULL,
+ inode INTEGER NOT NULL,
+ device INTEGER NOT NULL,
+ size INTEGER NOT NULL,
+ mtime REAL NOT NULL,
+ duration REAL NOT NULL,
+ PRIMARY KEY (path, title, start_chapter, end_chapter));
+ """)
+
class Source (object):
+
PREFIX = ""
TITLEP = CHAPTERP = False
+
def __init__(me, fn):
me.fn = fn
me.neps = None
- me.used_titles = dict()
+ me.used_titles = set()
me.used_chapters = set()
me.nuses = 0
- def url(me, title = None, start_chapter = None, end_chapter = None):
- if title == "-":
+
+ def _duration(me, title, start_chapter, end_chapter):
+ return -1
+
+ def url_and_duration(me, title = -1, start_chapter = -1, end_chapter = -1):
+ if title == -1:
if me.TITLEP: raise ExpectedError("missing title number")
- if start_chapter is not None or end_chapter is not None:
+ if start_chapter != -1 or end_chapter != -1:
raise ExpectedError("can't specify chapter without title")
suffix = ""
elif not me.TITLEP:
raise ExpectedError("can't specify title with `%s'" % me.fn)
- elif start_chapter is None:
- if end_chapter is not None:
+ elif start_chapter == -1:
+ if end_chapter != -1:
raise ExpectedError("can't specify end chapter without start chapter")
suffix = "#%d" % title
elif not me.CHAPTERP:
raise ExpectedError("can't specify chapter with `%s'" % me.fn)
- elif end_chapter is None:
+ elif end_chapter == -1:
suffix = "#%d:%d" % (title, start_chapter)
else:
suffix = "#%d:%d-%d:%d" % (title, start_chapter, title, end_chapter - 1)
- if end_chapter is not None:
+
+ duration = None
+ if DB is None:
+ duration = me._duration(title, start_chapter, end_chapter)
+ else:
+ st = OS.stat(OS.path.join(ROOT, me.fn))
+ duration = None
+ c = DB.cursor()
+ c.execute("""
+ SELECT device, inode, size, mtime, duration FROM duration
+ WHERE path = ? AND title = ? AND
+ start_chapter = ? AND end_chapter = ?
+ """, [me.fn, title, start_chapter, end_chapter])
+ row = c.fetchone()
+ foundp = False
+ if row is None:
+ duration = me._duration(title, start_chapter, end_chapter)
+ c.execute("""
+ INSERT OR REPLACE INTO duration
+ (path, title, start_chapter, end_chapter,
+ device, inode, size, mtime, duration)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ """, [me.fn, title, start_chapter, end_chapter,
+ st.st_dev, st.st_ino, st.st_size, st.st_mtime,
+ duration])
+ else:
+ dev, ino, sz, mt, d = row
+ if (dev, ino, sz, mt) == \
+ (st.st_dev, st.st_ino, st.st_size, st.st_mtime):
+ duration = d
+ else:
+ duration = me._duration(title, start_chapter, end_chapter)
+ c.execute("""
+ UPDATE duration
+ SET device = ?, inode = ?, size = ?, mtime = ?, duration = ?
+ WHERE path = ? AND title = ? AND
+ start_chapter = ? AND end_chapter = ?
+ """, [st.st_dev, st.st_dev, st.st_size, st.st_mtime, duration,
+ me.fn, title, start_chapter, end_chapter])
+ DB.commit()
+
+ if end_chapter != -1:
keys = [(title, ch) for ch in range(start_chapter, end_chapter)]
set = me.used_chapters
else:
keys, set = [title], me.used_titles
for k in keys:
if k in set:
- if title == "-":
+ if title == -1:
raise ExpectedError("`%s' already used" % me.fn)
- elif end_chapter is None:
+ elif end_chapter == -1:
raise ExpectedError("`%s' title %d already used" % (me.fn, title))
else:
raise ExpectedError("`%s' title %d chapter %d already used" %
(me.fn, title, k[1]))
- if end_chapter is not None:
+ if end_chapter == -1:
+ me.used_titles.add(title)
+ else:
for ch in range(start_chapter, end_chapter):
me.used_chapters.add((title, ch))
- return me.PREFIX + ROOT + urlencode(me.fn) + suffix
+ return me.PREFIX + ROOT + urlencode(me.fn) + suffix, duration
-class VideoDisc (Source):
+class DVDFile (Source):
PREFIX = "dvd://"
TITLEP = CHAPTERP = True
super().__init__(fn, *args, **kw)
me.neps = 0
-class VideoSeason (object):
+ def _duration(me, title, start_chapter, end_chapter):
+ path = OS.path.join(ROOT, me.fn)
+ ntitle = int(program_output(["dvd-info", path, "titles"]))
+ if not 1 <= title <= ntitle:
+ raise ExpectedError("bad title %d for `%s': must be in 1 .. %d" %
+ (title, me.fn, ntitle))
+ if start_chapter == -1:
+ durq = "duration:%d" % title
+ else:
+ nch = int(program_output(["dvd-info", path, "chapters:%d" % title]))
+ if end_chapter == -1: end_chapter = nch
+ else: end_chapter -= 1
+ if not 1 <= start_chapter <= end_chapter <= nch:
+ raise ExpectedError("bad chapter range %d .. %d for `%s' title %d: "
+ "must be in 1 .. %d" %
+ (start_chapter, end_chapter, me.fn, title, nch))
+ durq = "duration:%d.%d-%d" % (title, start_chapter, end_chapter)
+ duration = int(program_output(["dvd-info", path, durq]))
+ return duration
+
+class DVDSeason (object):
def __init__(me, i, title):
me.i = i
me.title = title
if mustp: raise ValueError("no match found")
else: return dflt
-class VideoDir (object):
+class DVDDir (object):
_R_ISO_PRE = list(map(lambda pats:
list(map(lambda pat:
"season %d /= %d" %
(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)
+ seasons[si] = season = DVDSeason(si, stitle)
else:
check(stitle == season.title,
"season title `%s' /= `%s'" % (stitle, season.title))
- disc = VideoDisc(path)
+ disc = DVDFile(path)
ts = season
any, bad = False, False
epnum = match_group(m, "epnum")
i = filter(mm.group("si"), int)
if i is not None:
try: ts = seasons[i]
- except KeyError: ts = seasons[i] = VideoSeason(i, None)
+ except KeyError: ts = seasons[i] = DVDSeason(i, None)
if ts is None:
- ts = season = seasons[1] = VideoSeason(1, None)
+ ts = season = seasons[1] = DVDSeason(1, None)
start = filter(mm.group("ei"), int)
end = filter(mm.group("ej"), int, start)
for k in range(start, end + 1):
raise ExpectedError("bad ep list in `%s'", fn)
me.seasons = seasons
-class AudioDisc (Source):
- PREFIX = "file://"
- TITLEP = CHAPTERP = False
-
-class AudioEpisode (Source):
- PREFIX = "file://"
- TITLEP = CHAPTERP = False
- def __init__(me, fn, i, *args, **kw):
- super().__init__(fn, *args, **kw)
- me.i = i
-
-class AudioDir (object):
+class SingleFileDir (object):
- _R_FLAC = RX.compile(r""" ^
- E (\d+)
- (?: \. \ (.*))?
- \. flac $
- """, RX.X)
+ _CHECK_COMPLETE = True
def __init__(me, dir):
me.dir = dir
fns.sort()
episodes = {}
last_i = 0
+ rx = RX.compile(r"""
+ E (\d+)
+ (?: \. \ (.*))?
+ %s $
+ """ % RX.escape(me._EXT), RX.X)
+
for fn in fns:
path = OS.path.join(dir, fn)
- if not fn.endswith(".flac"): continue
- m = me._R_FLAC.match(fn)
+ if not fn.endswith(me._EXT): continue
+ m = rx.match(fn)
if not m: continue
i = filter(m.group(1), int)
etitle = m.group(2)
- check(i == last_i + 1, "episode %d /= %d" % (i, last_i + 1))
- episodes[i] = AudioEpisode(path, i)
+ if me._CHECK_COMPLETE:
+ check(i == last_i + 1, "episode %d /= %d" % (i, last_i + 1))
+ episodes[i] = me._mkepisode(path, i)
last_i = i
me.episodes = episodes
+class AudioFile (Source):
+ PREFIX = "file://"
+ TITLEP = CHAPTERP = False
+
+ def _duration(me, title, start_chapter, end_chaptwr):
+ out = program_output(["metaflac",
+ "--show-total-samples", "--show-sample-rate",
+ OS.path.join(ROOT, me.fn)])
+ nsamples, hz = map(float, out.split())
+ return int(nsamples/hz)
+
+class AudioEpisode (AudioFile):
+ def __init__(me, fn, i, *args, **kw):
+ super().__init__(fn, *args, **kw)
+ me.i = i
+
+class AudioDir (SingleFileDir):
+ _EXT = ".flac"
+
+ def _mkepisode(me, path, i):
+ return AudioEpisode(path, i)
+
+class VideoFile (Source):
+ PREFIX = "file://"
+ TITLEP = CHAPTERP = False
+
+ def _duration(me, title, start_chapter, end_chaptwr):
+ out = program_output(["mediainfo", "--output=General;%Duration%",
+ OS.path.join(ROOT, me.fn)])
+ return int(out)//1000
+
+class VideoEpisode (VideoFile):
+ def __init__(me, fn, i, *args, **kw):
+ super().__init__(fn, *args, **kw)
+ me.i = i
+
+class VideoDir (SingleFileDir):
+ _EXT = ".mp4"
+ _CHECK_COMPLETE = False
+
+ def _mkepisode(me, path, i):
+ return VideoEpisode(path, i)
+
class Chapter (object):
def __init__(me, episode, title, i):
me.title, me.i = title, i
- me.url = episode.source.url(episode.tno, i, i + 1)
+ me.url, me.duration = \
+ episode.source.url_and_duration(episode.tno, i, i + 1)
class Episode (object):
- def __init__(me, season, i, neps, title, src, tno = None,
- startch = None, endch = None):
+ def __init__(me, season, i, neps, title, src, series_title_p = True,
+ tno = -1, startch = -1, endch = -1):
me.season = season
me.i, me.neps, me.title = i, neps, title
me.chapters = []
me.source, me.tno = src, tno
- me.url = src.url(tno, startch, endch)
+ me.series_title_p = series_title_p
+ me.tno, me.start_chapter, me.end_chapter = tno, startch, endch
+ me.url, me.duration = src.url_and_duration(tno, startch, endch)
def add_chapter(me, title, j):
ch = Chapter(me, title, j)
me.chapters.append(ch)
me.episodes = []
me.implicitp = implicitp
me.ep_i, episodes = 1, []
- def add_episode(me, j, neps, title, src, tno, startch, endch):
- ep = Episode(me, j, neps, title, src, tno, startch, endch)
+ def add_episode(me, j, neps, title, src, series_title_p,
+ tno, startch, endch):
+ ep = Episode(me, j, neps, title, src, series_title_p,
+ tno, startch, endch)
me.episodes.append(ep)
src.nuses += neps; me.ep_i += neps
return ep
def _epnames(me, i, neps):
playlist = me.series.playlist
- if neps == 1: return playlist.epname, "%d" % i
- elif neps == 2: return playlist.epnames, "%d, %d" % (i, i + 1)
- else: return playlist.epnames, "%d–%d" % (i, i + neps - 1)
+ if neps == 1: return playlist.epname, ["%d" % i]
+ elif neps == 2: return playlist.epnames, ["%d" % i, "%d" % (i + 1)]
+ else: return playlist.epnames, ["%d–%d" % (i, i + neps - 1)]
class Season (BaseSeason):
def __init__(me, series, title, i, *args, **kw):
def _eplabel(me, i, neps, title):
epname, epn = me._epnames(i, neps)
if title is None:
- if me.implicitp: label = "%s %s" % (epname, epn)
- elif me.title is None: label = "%s %d.%s" % (epname, me.i, epn)
- else: label = "%s—%s %s" % (me.title, epname, epn)
+ if me.implicitp:
+ label = "%s %s" % (epname, ", ".join(epn))
+ elif me.title is None:
+ label = "%s %s" % \
+ (epname, ", ".join("%d.%s" % (me.i, e) for e in epn))
+ else:
+ label = "%s: %s %s" % (me.title, epname, ", ".join(epn))
else:
- if me.implicitp: label = "%s. %s" % (epn, title)
- elif me.title is None: label = "%d.%s. %s" % (me.i, epn, title)
- else: label = "%s—%s. %s" % (me.title, epn, title)
+ if me.implicitp:
+ label = "%s. %s" % (", ".join(epn), title)
+ elif me.title is None:
+ label = "%s. %s" % \
+ (", ".join("%d.%s" % (me.i, e) for e in epn), title)
+ else:
+ label = "%s: %s. %s" % (me.title, ", ".join(epn), title)
return label
class MovieSeason (BaseSeason):
super().__init__(series, *args, **kw)
me.title = title
me.i = None
- def add_episode(me, j, neps, title, src, tno, startch, endch):
+ def add_episode(me, j, neps, title, src, series_title_p,
+ tno, startch, endch):
if me.title is None and title is None:
raise ExpectedError("movie or movie season must have a title")
- return super().add_episode(j, neps, title, src, tno, startch, endch)
+ return super().add_episode(j, neps, title, src, series_title_p,
+ tno, startch, endch)
def _eplabel(me, i, neps, title):
if me.title is None:
label = title
elif title is None:
epname, epn = me._epnames(i, neps)
- label = "%s—%s %s" % (me.title, epname, epn)
+ label = "%s: %s %s" % (me.title, epname, ", ".join(epn))
else:
- label = "%s—%s" % (me.title, title)
+ label = "%s: %s" % (me.title, title)
return label
class Series (object):
- def __init__(me, playlist, name, title = None, wantedp = True):
+ def __init__(me, playlist, name, title = None,
+ full_title = None, wantedp = True):
me.playlist = playlist
- me.name, me.title = name, title
+ me.name, me.title, me.full_title = name, title, full_title
me.cur_season = None
me.wantedp = wantedp
def _add_season(me, season):
me.cur_season = None
class Playlist (object):
+
def __init__(me):
me.seasons = []
me.episodes = []
me.epname, me.epnames = "Episode", "Episodes"
me.nseries = 0
+ me.single_series_p = False
+ me.series_title = None
+ me.series_sep = ""
+
def add_episode(me, episode):
me.episodes.append(episode)
+
def done_season(me):
if me.episodes:
me.seasons.append(me.episodes)
me.episodes = []
+
def write(me, f):
f.write("#EXTM3U\n")
for season in me.seasons:
f.write("\n")
for ep in season:
label = ep.label()
- if me.nseries > 1: label = ep.season.series.title + " " + label
+ if me.nseries > 1 and ep.series_title_p and \
+ ep.season.series.title is not None:
+ if ep.season.i is None: sep = ":"
+ else: sep = me.series_sep
+ label = ep.season.series.title + sep + " " + label
if not ep.chapters:
- f.write("#EXTINF:0,,%s\n%s\n" % (label, ep.url))
+ f.write("#EXTINF:%d,,%s\n%s\n" % (ep.duration, label, ep.url))
else:
for ch in ep.chapters:
- f.write("#EXTINF:0,,%s: %s\n%s\n" %
- (label, ch.title, ch.url))
+ f.write("#EXTINF:%d,,%s: %s\n%s\n" %
+ (ch.duration, label, ch.title, ch.url))
+
+ def dump(me, f):
+ if opts.list_name is not None: f.write("LIST %s\n" % opts.list_name)
+ if me.series_title is not None and \
+ me.nseries > 1 and not me.single_series_p:
+ raise ExpectedError("can't force series name for multi-series list")
+ series = set()
+ if me.single_series_p:
+ f.write("SERIES - %s\n" % quote(me.series_title))
+ for season in me.seasons:
+ for ep in season:
+ label = ep.label()
+ title = ep.season.series.full_title
+ if me.single_series_p:
+ stag = "-"
+ if title is not None: label = title + me.series_sep + " " + label
+ else:
+ if title is None: title = me.series_title
+ stag = ep.season.series.name
+ if stag is None: stag = "-"
+ if stag not in series:
+ f.write("SERIES %s %s\n" % (stag, quote(title)))
+ series.add(stag)
+ f.write("ENTRY %s %s %s %d %d %d %g\n" %
+ (stag, quote(label), quote(ep.source.fn),
+ ep.tno, ep.start_chapter, ep.end_chapter, ep.duration))
+
+ def write_deps(me, f, out):
+ deps = set()
+ for season in me.seasons:
+ for ep in season: deps.add(ep.source.fn)
+ f.write("### -*-makefile-*-\n")
+ f.write("%s: $(call check-deps, %s," % (out, out))
+ for dep in sorted(deps):
+ f.write(" \\\n\t'%s'" %
+ OS.path.join(ROOT, dep)
+ .replace(",", "$(comma)")
+ .replace("'", "'\\''"))
+ f.write(")\n")
+
+DEFAULT_EXPVAR = 0.05
+R_DURMULT = RX.compile(r""" ^
+ (\d+ (?: \. \d+)?) x
+$ """, RX.X)
+R_DUR = RX.compile(r""" ^
+ (?: (?: (\d+) :)? (\d+) :)? (\d+)
+ (?: / (\d+ (?: \. \d+)?) \%)?
+$ """, RX.X)
+def parse_duration(s, base = None, basevar = DEFAULT_EXPVAR):
+ if base is not None:
+ m = R_DURMULT.match(s)
+ if m is not None: return base*float(m.group(1)), basevar
+ m = R_DUR.match(s)
+ if not m: raise ExpectedError("invalid duration spec `%s'" % s)
+ hr, min, sec = map(lambda g: filter(m.group(g), int, 0), [1, 2, 3])
+ var = filter(m.group(4), lambda x: float(x)/100.0)
+ if var is None: var = DEFAULT_EXPVAR
+ return 3600*hr + 60*min + sec, var
+def format_duration(d):
+ if d >= 3600: return "%d:%02d:%02d" % (d//3600, (d//60)%60, d%60)
+ elif d >= 60: return "%d:%02d" % (d//60, d%60)
+ else: return "%d s" % d
MODE_UNSET = 0
MODE_SINGLE = 1
def __init__(me, series_wanted = None, chapters_wanted_p = False):
me._pl = Playlist()
me._cur_episode = me._cur_chapter = None
- me._series = {}; me._vdirs = {}; me._audirs = {}; me._isos = {}
+ me._series = {}; me._vdirs = {}; me._sfdirs = {}; me._isos = {}
me._series_wanted = series_wanted
me._chaptersp = chapters_wanted_p
+ me._explen, me._expvar = None, DEFAULT_EXPVAR
if series_wanted is None: me._mode = MODE_UNSET
else: me._mode = MODE_MULTI
except ValueError: opts = None
else: cmd, opts = cmd[:sep], cmd[sep + 1:]
- if cmd == "series":
+ if cmd == "title":
+ for k, v in me._keyvals(opts): me._bad_keyval("title", k, v)
+ title = ww.rest(); check(title is not None, "missing title")
+ check(me._pl.series_title is None, "already set a title")
+ me._pl.series_title = title
+
+ elif cmd == "single":
+ for k, v in me._keyvals(opts): me._bad_keyval("single", k, v)
+ check(ww.rest() is None, "trailing junk")
+ check(not me._pl.single_series_p, "single-series already set")
+ me._pl.single_series_p = True
+
+ elif cmd == "series":
name = None
for k, v in me._keyvals(opts):
if k is None: name = v
else: me._bad_keyval(cmd, k, v)
check(name is not None, "missing series name")
check(name not in me._series, "series `%s' already defined" % name)
- title = ww.rest(); check(title is not None, "missing title")
+ title = ww.rest()
+ if title is None:
+ full = None
+ else:
+ try: sep = title.index("::")
+ except ValueError: full = title
+ else:
+ full = title[sep + 2:].strip()
+ if sep == 0: title = None
+ else: title = title[:sep].strip()
me._set_mode(MODE_MULTI)
- me._series[name] = series = Series(me._pl, name, title,
+ me._series[name] = series = Series(me._pl, name, title, full,
me._series_wanted is None or
name in me._series_wanted)
if series.wantedp: me._pl.nseries += 1
me._cur_episode = me._cur_chapter = None
me._pl.done_season()
+ elif cmd == "explen":
+ w = ww.rest(); check(w is not None, "missing duration spec")
+ if w == "-":
+ me._explen, me._expvar = None, DEFAULT_EXPVAR
+ else:
+ d, v = parse_duration(w)
+ me._explen = d
+ if v is not None: me._expvar = v
+
elif cmd == "epname":
for k, v in me._keyvals(opts): me._bad_keyval("epname", k, v)
name = ww.rest(); check(name is not None, "missing episode name")
- try: sep = name.index(",")
+ try: sep = name.index("::")
except ValueError: names = name + "s"
- else: name, names = name[:sep], name[sep + 1:]
+ else: name, names = name[:sep], name[sep + 2:]
me._pl.epname, me._pl.epnames = name, names
elif cmd == "epno":
if not series.wantedp: return
series.ensure_season().ep_i = epi
- elif cmd == "iso":
+ elif cmd == "dvd":
series = me._opts_series(cmd, opts)
fn = ww.rest(); check(fn is not None, "missing filename")
if not series.wantedp: return
if fn == "-": forget(me._isos, series.name)
else:
check(OS.path.exists(OS.path.join(ROOT, fn)),
- "iso file `%s' not found" % fn)
- me._isos[series.name] = VideoDisc(fn)
+ "dvd iso file `%s' not found" % fn)
+ me._isos[series.name] = DVDFile(fn)
- elif cmd == "vdir":
+ elif cmd == "dvddir":
series = me._opts_series(cmd, opts)
dir = ww.rest(); check(dir is not None, "missing directory")
if not series.wantedp: return
if dir == "-": forget(me._vdirs, series.name)
- else: me._vdirs[series.name] = VideoDir(dir)
+ else: me._vdirs[series.name] = DVDDir(dir)
+
+ elif cmd == "vdir":
+ series = me._opts_series(cmd, opts)
+ dir = ww.rest(); check(dir is not None, "missing directory")
+ if not series.wantedp: return
+ if dir == "-": forget(me._sfdirs, series.name)
+ else: me._sfdirs[series.name] = VideoDir(dir)
elif cmd == "adir":
series = me._opts_series(cmd, opts)
dir = ww.rest(); check(dir is not None, "missing directory")
if not series.wantedp: return
- if dir == "-": forget(me._audirs, series.name)
- else: me._audirs[series.name] = AudioDir(dir)
+ if dir == "-": forget(me._sfdirs, series.name)
+ else: me._sfdirs[series.name] = AudioDir(dir)
elif cmd == "displaced":
series = me._opts_series(cmd, opts)
w = ww.rest(); check(w is not None, "missing count"); n = getint(w)
src = me._auto_epsrc(series)
src.nuses += n
+
+ elif cmd == "sep":
+ sep = ww.rest(); check(sep is not None, "missing separator")
+ me._pl.series_sep = sep
+
else:
raise ExpectedError("unknown command `%s'" % cmd)
def _process_episode(me, ww):
opts = ww.nextword(); check(opts is not None, "missing title/options")
- ti = None; sname = None; neps = 1; epi = None; loch = hich = None
+ ti = -1; sname = None; neps = 1; epi = None; loch = hich = -1
+ explen, expvar, explicitlen = me._explen, me._expvar, False
+ series_title_p = True
for k, v in me._keyvals(opts):
if k is None:
if v.isdigit(): ti = int(v)
- elif v == "-": ti = "-"
+ elif v == "-": ti = -1
else: sname = v
elif k == "s": sname = v
elif k == "n": neps = getint(v)
elif k == "ep": epi = getint(v)
+ elif k == "st": series_title_p = getbool(v)
+ elif k == "l":
+ if v == "-": me._explen, me._expvar = None, DEFAULT_EXPVAR
+ else:
+ explen, expvar = parse_duration(v, explen, expvar)
+ explicitlen = True
elif k == "ch":
try: sep = v.index("-")
- except ValueError: loch, hich = getint(v), None
+ except ValueError: loch, hich = getint(v), -1
else: loch, hich = getint(v[:sep]), getint(v[sep + 1:]) + 1
else: raise ExpectedError("unknown episode option `%s'" % k)
check(ti is not None, "missing title number")
season = series.ensure_season()
if epi is None: epi = season.ep_i
- if ti == "-":
- check(season.implicitp, "audio source, but explicit season")
- dir = lookup(me._audirs, series.name,
- "no title, and no audio directory")
+ if ti == -1:
+ check(season.implicitp or season.i is None,
+ "audio source, but explicit non-movie season")
+ dir = lookup(me._sfdirs, series.name,
+ "no title, and no single-file directory")
src = lookup(dir.episodes, season.ep_i,
- "episode %d not found in audio dir `%s'" % (epi, dir.dir))
+ "episode %d not found in single-file dir `%s'" %
+ (epi, dir.dir))
else:
try: src = me._isos[series.name]
except KeyError: src = me._auto_epsrc(series)
- episode = season.add_episode(epi, neps, title, src, ti, loch, hich)
+ episode = season.add_episode(epi, neps, title, src,
+ series_title_p, ti, loch, hich)
+
+ if episode.duration != -1 and explen is not None:
+ if not explicitlen: explen *= neps
+ if not explen*(1 - expvar) <= episode.duration <= explen*(1 + expvar):
+ if season.i is None: epid = "episode %d" % epi
+ else: epid = "episode %d.%d" % (season.i, epi)
+ raise ExpectedError \
+ ("%s duration %s %g%% > %g%% from expected %s" %
+ (epid, format_duration(episode.duration),
+ abs(100*(episode.duration - explen)/explen), 100*expvar,
+ format_duration(explen)))
me._pl.add_episode(episode)
me._cur_episode = episode
for s in vdir.seasons.values():
for d in s.episodes.values():
discs.add(d)
- for adir in me._audirs.values():
- for d in adir.episodes.values():
+ for sfdir in me._sfdirs.values():
+ for d in sfdir.episodes.values():
discs.add(d)
for d in sorted(discs, key = lambda d: d.fn):
if d.neps is not None and d.neps != d.nuses:
(d.fn, d.neps, d.nuses))
return me._pl
-ROOT = "/mnt/dvd/archive/"
-
op = OP.OptionParser \
- (usage = "%prog [-c] [-s SERIES] EPLS",
+ (usage = "%prog [-Dc] [-L NAME] [-M DEPS] [-d CACHE] [-o OUT] [-s SERIES] EPLS\n"
+ "%prog -i -d CACHE",
description = "Generate M3U playlists from an episode list.")
+op.add_option("-D", "--dump",
+ dest = "dump", action = "store_true", default = False,
+ help = "Dump playlist in machine-readable form")
+op.add_option("-L", "--list-name", metavar = "NAME",
+ dest = "list_name", type = "str", default = None,
+ help = "Set the playlist name")
+op.add_option("-M", "--make-deps", metavar = "DEPS",
+ dest = "deps", type = "str", default = None,
+ help = "Write a `make' fragment for dependencies")
op.add_option("-c", "--chapters",
dest = "chaptersp", action = "store_true", default = False,
help = "Output individual chapter names")
-op.add_option("-s", "--series",
+op.add_option("-i", "--init-db",
+ dest = "initdbp", action = "store_true", default = False,
+ help = "Initialize the database")
+op.add_option("-d", "--database", metavar = "CACHE",
+ dest = "database", type = "str", default = None,
+ help = "Set filename for cache database")
+op.add_option("-o", "--output", metavar = "OUT",
+ dest = "output", type = "str", default = None,
+ help = "Write output playlist to OUT")
+op.add_option("-O", "--fake-output", metavar = "OUT",
+ dest = "fakeout", type = "str", default = None,
+ help = "Pretend output goes to OUT for purposes of `-M'")
+op.add_option("-s", "--series", metavar = "SERIES",
dest = "series", type = "str", default = None,
help = "Output only the listed SERIES (comma-separated)")
-opts, argv = op.parse_args()
-if len(argv) != 1: op.print_usage(file = SYS.stderr); SYS.exit(2)
-if opts.series is None:
- series_wanted = None
-else:
- series_wanted = set()
- for name in opts.series.split(","): series_wanted.add(name)
try:
- ep = EpisodeListParser(series_wanted, opts.chaptersp)
- ep.parse_file(argv[0])
- pl = ep.done()
- pl.write(SYS.stdout)
+ opts, argv = op.parse_args()
+
+ if opts.initdbp:
+ if opts.chaptersp or opts.series is not None or \
+ opts.output is not None or opts.deps is not None or \
+ opts.fakeout is not None or \
+ opts.database is None or len(argv):
+ op.print_usage(file = SYS.stderr); SYS.exit(2)
+ setup_db(opts.database)
+
+ else:
+ if len(argv) != 1: op.print_usage(file = SYS.stderr); SYS.exit(2)
+ if opts.database is not None: init_db(opts.database)
+ if opts.series is None:
+ series_wanted = None
+ else:
+ series_wanted = set()
+ for name in opts.series.split(","): series_wanted.add(name)
+ if opts.deps is not None:
+ if (opts.output is None or opts.output == "-") and opts.fakeout is None:
+ raise ExpectedError("can't write dep fragment without output file")
+ if opts.fakeout is None: opts.fakeout = opts.output
+ else:
+ if opts.fakeout is not None:
+ raise ExpectedError("fake output set but no dep fragment")
+
+ ep = EpisodeListParser(series_wanted, opts.chaptersp)
+ ep.parse_file(argv[0])
+ pl = ep.done()
+
+ if opts.list_name is None:
+ opts.list_name, _ = OS.path.splitext(OS.path.basename(argv[0]))
+
+ if opts.dump: outfn = pl.dump
+ else: outfn = pl.write
+ if opts.output is None or opts.output == "-":
+ outfn(SYS.stdout)
+ else:
+ with open(opts.output, "w") as f: outfn(f)
+
+ if opts.deps:
+ if opts.deps == "-":
+ pl.write_deps(SYS.stdout, opts.fakeout)
+ else:
+ with open(opts.deps, "w") as f: pl.write_deps(f, opts.fakeout)
+
except (ExpectedError, IOError, OSError) as e:
LOC.report(e)
SYS.exit(2)