X-Git-Url: https://git.distorted.org.uk/~mdw/epls/blobdiff_plain/3b9c615b866f7b2ea2681cd036ed2d933779f933..900d7426eb89b95b223ed7ad88a5c322d1fb5fd6:/mkm3u diff --git a/mkm3u b/mkm3u index 7df1891..e4a32c7 100755 --- a/mkm3u +++ b/mkm3u @@ -44,6 +44,10 @@ def getbool(s): 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 @@ -135,28 +139,28 @@ class Source (object): 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 _duration(me, title, start_chapter, end_chapter): return -1 - def url_and_duration(me, title = None, - start_chapter = None, end_chapter = None): - if title == "-": + + 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) @@ -172,8 +176,7 @@ class Source (object): SELECT device, inode, size, mtime, duration FROM duration WHERE path = ? AND title = ? AND start_chapter = ? AND end_chapter = ? - """, [me.fn, title, start_chapter is None and -1 or start_chapter, - end_chapter is None and -1 or end_chapter]) + """, [me.fn, title, start_chapter, end_chapter]) row = c.fetchone() foundp = False if row is None: @@ -183,8 +186,7 @@ class Source (object): (path, title, start_chapter, end_chapter, device, inode, size, mtime, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) - """, [me.fn, title, start_chapter is None and -1 or start_chapter, - end_chapter is None and -1 or end_chapter, + """, [me.fn, title, start_chapter, end_chapter, st.st_dev, st.st_ino, st.st_size, st.st_mtime, duration]) else: @@ -199,26 +201,27 @@ class Source (object): 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 is None and -1 or start_chapter, - end_chapter is None and -1 or 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 is not None: + 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, duration @@ -237,11 +240,11 @@ class VideoDisc (Source): if not 1 <= title <= ntitle: raise ExpectedError("bad title %d for `%s': must be in 1 .. %d" % (title, me.fn, ntitle)) - if start_chapter is None: + if start_chapter == -1: durq = "duration:%d" % title else: nch = int(program_output(["dvd-info", path, "chapters:%d" % title])) - if end_chapter is None: end_chapter = nch + 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: " @@ -407,12 +410,13 @@ class Chapter (object): class Episode (object): def __init__(me, season, i, neps, title, src, series_title_p = True, - tno = None, startch = None, endch = None): + 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.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) @@ -486,9 +490,10 @@ class MovieSeason (BaseSeason): 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): @@ -510,6 +515,8 @@ class Playlist (object): me.episodes = [] me.epname, me.epnames = "Episode", "Episodes" me.nseries = 0 + me.single_series_p = False + me.series_title = None def add_episode(me, episode): me.episodes.append(episode) @@ -535,7 +542,33 @@ class Playlist (object): else: for ch in ep.chapters: f.write("#EXTINF:%d,,%s: %s\n%s\n" % - (ch.duration, label, ch.title, ch.url)) + (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 + " " + 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() @@ -644,7 +677,19 @@ class EpisodeListParser (object): 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 @@ -652,8 +697,17 @@ class EpisodeListParser (object): check(name is not None, "missing series name") check(name not in me._series, "series `%s' already defined" % name) 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 @@ -686,7 +740,7 @@ class EpisodeListParser (object): name = ww.rest(); check(name is not None, "missing episode name") 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": @@ -732,13 +786,13 @@ class EpisodeListParser (object): 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) @@ -751,7 +805,7 @@ class EpisodeListParser (object): 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") @@ -763,7 +817,7 @@ class EpisodeListParser (object): season = series.ensure_season() if epi is None: epi = season.ep_i - if ti == "-": + if ti == -1: check(season.implicitp or season.i is None, "audio source, but explicit non-movie season") dir = lookup(me._audirs, series.name, @@ -830,9 +884,15 @@ class EpisodeListParser (object): return me._pl op = OP.OptionParser \ - (usage = "%prog [-c] [-M DEPS] [-d CACHE] [-o OUT] [-s SERIES] EPLS\n" - "%prog -i -d CACHE", + (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") @@ -885,10 +945,15 @@ try: 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 == "-": - pl.write(SYS.stdout) + outfn(SYS.stdout) else: - with open(opts.output, "w") as f: pl.write(f) + with open(opts.output, "w") as f: outfn(f) if opts.deps: if opts.deps == "-":