X-Git-Url: https://git.distorted.org.uk/~mdw/epls/blobdiff_plain/d1f1c5785c7fcd7a9a6df471f1937c56e78e8d65..017188769528344839a7699c4528ad7cd26e443e:/mkm3u?ds=inline diff --git a/mkm3u b/mkm3u index 3eb16b1..9c50c3e 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 @@ -77,7 +81,7 @@ 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 @@ -412,6 +416,7 @@ class Episode (object): 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) @@ -537,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() @@ -853,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") @@ -908,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 == "-":