В парсер M3U добавлена поддержка дополнительных тегов (группы, страны и т.д.)
[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" country="%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             country - country of origin (optional)
28             group - item playlist group (optional)
29             logo - item logo file name (optional)
30         '''
31         self.itemlist.append(itemdict)
32
33     @staticmethod
34     def _generatem3uline(item):
35         '''
36         Generates EXTINF line with url
37         '''
38         print(item)
39         return PlaylistGenerator.m3uchanneltemplate % (
40             item.get('group', ''), 
41             item.get('tvg', ''), 
42             item.get('logo', ''),
43             item.get('country', ''),
44             item.get('name'), 
45             item.get('url'))
46
47     def exportm3u(self, hostport, prefix="get", add_ts=False, empty_header=False, archive=False):
48         '''
49         Exports m3u playlist
50         '''
51         if not empty_header:
52             itemlist = PlaylistGenerator.m3uheader
53         else:
54             itemlist = PlaylistGenerator.m3uemptyheader
55         if add_ts:
56                 # Adding ts:// after http:// for some players
57                 hostport = 'ts://' + hostport
58
59         for item in self.itemlist:
60             item['tvg'] = item.get('tvg', '') if item.get('tvg') else \
61                 item.get('name').replace(' ', '_')
62             # For .acelive and .torrent
63             item['url'] = 'http://' + hostport + '/' + prefix + '/' + item['url']
64             itemlist += PlaylistGenerator._generatem3uline(item)
65
66         return itemlist
67
68     def dumpm3u(self):
69         '''
70         Dump m3u playlist
71         '''
72         itemlist = PlaylistGenerator.m3uemptyheader
73
74         for item in self.itemlist:
75             item['tvg'] = item.get('tvg', '') if item.get('tvg') else \
76                 item.get('name').replace(' ', '_')
77             # For .acelive and .torrent
78             itemlist += PlaylistGenerator._generatem3uline(item)
79
80         return itemlist