4 (based on ytv plugin by ValdikSS)
14 http://ip:port/m3u/list-name
16 plain-tet channel names
20 http://ip:port/list/list-name
24 http://ip:port/play/channel-name
25 http://ip:port/play/list-name/channel-name
26 http://ip:port/play/{list-name}/(get|mp4|webm)/channel-name
33 from modules.PluginInterface import VPProxyPlugin
34 from modules.PlaylistGenerator import PlaylistGenerator
35 from modules.M3uParser import parseM3U
36 import config.m3u as config
39 class M3u(VPProxyPlugin):
41 handlers = ('m3u', 'm3ut', 'm3uw', 'm3uo', 'list', 'play', 'index')
43 logger = logging.getLogger('plugin_m3u')
46 def handle(self, connection):
48 hostport = connection.headers['Host']
50 self.splitted_path=connection.path.split('/')
52 if self.splitted_path[1]=='m3u':
54 elif self.splitted_path[1]=='m3uw':
56 elif self.splitted_path[1]=='m3ut':
58 elif self.splitted_path[1]=='m3uo':
60 elif self.splitted_path[1] in ("list","play","index"):
63 connection.dieWithError(404)
65 if len(self.splitted_path)>3 and self.splitted_path[1]!="play":
66 connection.dieWithError()
69 if self.splitted_path[1]=='index':
72 for dir in os.walk(config.m3u_directory):
73 if dir[0]==config.m3u_directory:
74 for dirname in dir[2]:
75 if dirname.endswith('.m3u'):
76 text=text+'\n'+dirname
78 connection.send_response(200)
79 connection.send_header('Content-Type', 'text/plain; charset=utf-8')
80 connection.end_headers()
82 listing = text.encode('utf-8')
83 connection.wfile.write(listing)
87 if len(self.splitted_path)<3 or (len(self.splitted_path)==3 and self.splitted_path[1]=="play"):
89 m3u_file=config.m3u_directory+'/'+config.m3u_default
93 filename = self.splitted_path[2]
95 m3u_file=config.m3u_directory+'/'+self.splitted_path[2]
97 m3u_file=config.m3u_directory+'/'+config.m3u_default
100 playlist=parseM3U(m3u_file)
102 connection.dieWithError(404)
105 if self.splitted_path[1]=="list":
106 connection.send_response(200)
107 connection.send_header('Content-Type', 'text/plain; charset=utf-8')
108 connection.end_headers()
109 elif self.splitted_path[1]=="play":
110 channel=self.splitted_path[len(self.splitted_path)-1]
111 channel=urllib.unquote(channel).decode('utf-8')
112 if len(self.splitted_path)<=4:
114 elif len(self.splitted_path)==5:
115 prefix=self.splitted_path[len(self.splitted_path)-2]
116 if prefix not in ('get','mp4','webm'):
117 connection.dieWithError()
119 connection.dieWithError()
121 for record in playlist:
122 if record.title.decode('utf-8').replace('/','')==channel:
123 url=record.path.decode('utf-8')
125 redirect='/'+prefix+'/'+url
126 connection.send_response(302)
127 connection.send_header('Location', redirect)
128 connection.end_headers()
130 connection.dieWithError(404)
133 connection.send_response(200)
134 connection.send_header('Content-Type', 'application/x-mpegurl')
135 connection.end_headers()
138 playlist=parseM3U(m3u_file)
140 connection.dieWithError(404)
144 connection.dieWithError()
147 if self.splitted_path[1]=="list":
151 for record in playlist:
153 exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
157 playlistgen = PlaylistGenerator()
159 for record in playlist:
162 channel['name']=record.title.decode('utf-8')
163 channel['url']=record.path.decode('utf-8')
165 channel['tvg']=record.attrs['tvg-name'].decode('utf-8')
169 if record.attrs['group-title'] != 'None':
170 channel['group']=record.attrs['group-title'].decode('utf-8')
174 channel['country']=record.attrs['country'].decode('utf-8')
178 channel['logo']=record.attrs['tvg-logo'].decode('utf-8')
181 playlistgen.addItem(channel)
183 exported = playlistgen.exportm3u(hostport,prefix)
185 exported = exported.encode('utf-8')
187 connection.wfile.write(exported)
189 def getparam(self, key):
190 if key in self.params:
191 return self.params[key][0]