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')
108 if len(self.splitted_path)<=4:
110 elif len(self.splitted_path)==5:
111 prefix=self.splitted_path[len(self.splitted_path)-2]
112 if prefix not in ('get','mp4','webm'):
113 connection.dieWithError()
115 connection.dieWithError()
117 for record in playlist:
118 if record.title.decode('utf-8')==channel:
119 url=record.path.decode('utf-8')
122 redirect='/'+prefix+'/'+url
124 connection.send_response(302)
125 connection.send_header('Location', redirect)
126 connection.end_headers()
128 connection.dieWithError(404)
131 connection.send_response(200)
132 connection.send_header('Content-Type', 'application/x-mpegurl')
133 connection.end_headers()
136 playlist=parseM3U(m3u_file)
138 connection.dieWithError(404)
142 connection.dieWithError()
145 if self.splitted_path[1]=="list":
149 for record in playlist:
150 exported = exported + "" + record.title.decode('utf-8') + "\n"
154 playlistgen = PlaylistGenerator()
156 for record in playlist:
158 channel['name']=record.title.decode('utf-8')
159 channel['url']=record.path.decode('utf-8')
160 playlistgen.addItem(channel)
162 exported = playlistgen.exportm3u(hostport,prefix)
164 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]