Freeiptv m3u.py

From MythTV Official Wiki
Revision as of 18:17, 16 March 2011 by Lazyet (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Released under GPLv2

""" Quick and dirty m3u parser/writer intended to translate free.fr m3u for use with mythtv """

import re

class freem3uchan:
        def __init__(self):
                self.name = None
                self.payload = []

class freem3ulist:
        def __init__(self):
                self._list = {}

        def parse(self,infile):
                ln = None
                self._list = {}
                while (ln != "" and ln != u"#EXTM3U\n"):
                        ln = infile.readline()
                ln = infile.readline()
                while (ln != ""):
                        while (ln != "" and ln.find(u"#EXTINF") == -1):
                                ln = infile.readline()
                        (num, name) = re.match(ur"#EXTINF:\d+,(\d+) - (.*)", ln).groups()
                        num = int(num)
                        nitem = freem3uchan()
                        nitem.name = name
                        ln = infile.readline()
                        while (ln != "" and ln.find(u"#EXTINF") == -1):
                                nitem.payload.append(ln)
                                ln = infile.readline()
                        mo = re.match(ur"rtsp://mafreebox\.freebox\.fr/fbxtv_pub/stream\?namespace=\d+&service=\d+&flavour=(\w+)", nitem.payload[-1])
                        if (mo is None): flavour = "sd"
                        else: flavour = mo.groups()[0]
                        if num not in self._list:
                                self._list[num] = {}
                        self._list[num][flavour] = nitem

        def free(self):
                self._list = None

        def write_chan(self, of, num, name, payload):
                of.write("#EXTINF:0,%d - %s\n" % (num, name))
                for ln in payload:
                        of.write(ln)

        def dump_with_sliced_channums(self, of):
                of.write("#EXTM3U\n")
                for (num, chan) in self._list.iteritems():
                        if (num > 99999):
                                print("Ooops, there is a need to update the intervals!")
                                raise ValueError("Channel number out of expected values")
                        if "hd" in chan:
                                self.write_chan(of, num, chan["hd"].name, chan["hd"].payload)
                        if "sd" in chan:
                                self.write_chan(of, num+100000, chan["sd"].name, chan["sd"].payload)
                        if "ld" in chan:
                                self.write_chan(of, num+200000, chan["ld"].name, chan["ld"].payload)
                return True

if __name__ == "__main__":
        import sys
        import codecs
        if (len(sys.argv) != 3):
                print "Usage: script infile.m3u outfile.m3u"
                sys.exit(1)
        try:
                fsrc = codecs.open(sys.argv[1], mode="r", encoding="utf-8")
                lst = freem3ulist()
                lst.parse(fsrc)
                fsrc.close()
        except Exception as e:
                print "Error when processing source file : ", e
                sys.exit(1)
        try:
                fdst = codecs.open(sys.argv[2], mode="w", encoding="utf-8")
                lst.dump_with_sliced_channums(fdst)
                fdst.close()
        except Exception as e:
                print "Error when generating destination file : ", e
                sys.exit(1)
</code>