transcoding option for web-video added
[vpproxy.git] / plugins / modules / PlaylistGenerator.py
1 '''
2 Playlist Generator
3 This module can generate .m3u playlists with tv guide
4 and groups
5 '''
6 import re
7 import urllib2
8
9 class PlaylistGenerator(object):
10
11     m3uheader = \
12         '#EXTM3U url-tvg="http://www.teleguide.info/download/new3/jtv.zip"\n'
13     m3uemptyheader = '#EXTM3U\n'
14     m3uchanneltemplate = \
15         '#EXTINF:-1 group-title="%s" tvg-name="%s" tvg-logo="%s",%s\n%s\n'
16
17     def __init__(self):
18         self.itemlist = list()
19
20     def addItem(self, itemdict):
21         '''
22         Adds item to the list
23         itemdict is a dictionary with the following fields:
24             name - item name
25             url - item URL
26             tvg - item JTV name (optional)
27             group - item playlist group (optional)
28             logo - item logo file name (optional)
29         '''
30         self.itemlist.append(itemdict)
31
32     @staticmethod
33     def _generatem3uline(item):
34         '''
35         Generates EXTINF line with url
36         '''
37         return PlaylistGenerator.m3uchanneltemplate % (
38             item.get('group', ''), item.get('tvg', ''), item.get('logo', ''),
39             item.get('name'), item.get('url'))
40
41     def exportm3u(self, hostport, prefix="get", add_ts=False, empty_header=False, archive=False):
42         '''
43         Exports m3u playlist
44         '''
45         if not empty_header:
46             itemlist = PlaylistGenerator.m3uheader
47         else:
48             itemlist = PlaylistGenerator.m3uemptyheader
49         if add_ts:
50                 # Adding ts:// after http:// for some players
51                 hostport = 'ts://' + hostport
52
53         for item in self.itemlist:
54             item['tvg'] = item.get('tvg', '') if item.get('tvg') else \
55                 item.get('name').replace(' ', '_')
56             # For .acelive and .torrent
57             item['url'] = 'http://' + hostport + '/' + prefix + '/' + item['url']
58             itemlist += PlaylistGenerator._generatem3uline(item)
59
60         return itemlist