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', "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] in ("list","play","index"):
61 connection.dieWithError(404)
63 if len(self.splitted_path)>3 and self.splitted_path[1]!="play":
64 connection.dieWithError()
67 if self.splitted_path[1]=='index':
69 for dir in os.walk(config.m3u_directory):
70 if dir[0]==config.m3u_directory:
71 text='\n'.join(dir[2])
73 connection.send_response(200)
74 connection.send_header('Content-Type', 'text/plain; charset=utf-8')
75 connection.end_headers()
77 listing = text.encode('utf-8')
78 connection.wfile.write(listing)
82 if len(self.splitted_path)<3 or (len(self.splitted_path)==3 and self.splitted_path[1]=="play"):
84 m3u_file=config.m3u_directory+'/'+config.m3u_default
88 filename = self.splitted_path[2]
90 m3u_file=config.m3u_directory+'/'+self.splitted_path[2]
92 m3u_file=config.m3u_directory+'/'+config.m3u_default
95 playlist=parseM3U(m3u_file)
97 connection.dieWithError(404)
100 if self.splitted_path[1]=="list":
101 connection.send_response(200)
102 connection.send_header('Content-Type', 'text/plain; charset=utf-8')
103 connection.end_headers()
104 elif self.splitted_path[1]=="play":
105 channel=self.splitted_path[len(self.splitted_path)-1]
106 channel=urllib.unquote(channel).decode('utf-8')
107 if len(self.splitted_path)<=4:
109 elif len(self.splitted_path)==5:
110 prefix=self.splitted_path[len(self.splitted_path)-2]
111 if prefix not in ('get','mp4','webm'):
112 connection.dieWithError()
114 connection.dieWithError()
116 for record in playlist:
117 if record.title.decode('utf-8').replace('/','')==channel:
118 url=record.path.decode('utf-8')
120 redirect='/'+prefix+'/'+url
121 connection.send_response(302)
122 connection.send_header('Location', redirect)
123 connection.end_headers()
125 connection.dieWithError(404)
128 connection.send_response(200)
129 connection.send_header('Content-Type', 'application/x-mpegurl')
130 connection.end_headers()
133 playlist=parseM3U(m3u_file)
135 connection.dieWithError(404)
139 connection.dieWithError()
142 if self.splitted_path[1]=="list":
146 for record in playlist:
148 exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
152 playlistgen = PlaylistGenerator()
154 for record in playlist:
157 channel['name']=record.title.decode('utf-8')
158 channel['url']=record.path.decode('utf-8')
159 playlistgen.addItem(channel)
161 exported = playlistgen.exportm3u(hostport,prefix)
163 exported = exported.encode('utf-8')
165 connection.wfile.write(exported)
167 def getparam(self, key):
168 if key in self.params:
169 return self.params[key][0]