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':
71 for dir in os.walk(config.m3u_directory):
72 if dir[0]==config.m3u_directory:
73 text='\n'.join(dir[2])
75 connection.send_response(200)
76 connection.send_header('Content-Type', 'text/plain; charset=utf-8')
77 connection.end_headers()
79 listing = text.encode('utf-8')
80 connection.wfile.write(listing)
84 if len(self.splitted_path)<3 or (len(self.splitted_path)==3 and self.splitted_path[1]=="play"):
86 m3u_file=config.m3u_directory+'/'+config.m3u_default
90 filename = self.splitted_path[2]
92 m3u_file=config.m3u_directory+'/'+self.splitted_path[2]
94 m3u_file=config.m3u_directory+'/'+config.m3u_default
97 playlist=parseM3U(m3u_file)
99 connection.dieWithError(404)
102 if self.splitted_path[1]=="list":
103 connection.send_response(200)
104 connection.send_header('Content-Type', 'text/plain; charset=utf-8')
105 connection.end_headers()
106 elif self.splitted_path[1]=="play":
107 channel=self.splitted_path[len(self.splitted_path)-1]
108 channel=urllib.unquote(channel).decode('utf-8')
109 if len(self.splitted_path)<=4:
111 elif len(self.splitted_path)==5:
112 prefix=self.splitted_path[len(self.splitted_path)-2]
113 if prefix not in ('get','mp4','webm'):
114 connection.dieWithError()
116 connection.dieWithError()
118 for record in playlist:
119 if record.title.decode('utf-8').replace('/','')==channel:
120 url=record.path.decode('utf-8')
122 redirect='/'+prefix+'/'+url
123 connection.send_response(302)
124 connection.send_header('Location', redirect)
125 connection.end_headers()
127 connection.dieWithError(404)
130 connection.send_response(200)
131 connection.send_header('Content-Type', 'application/x-mpegurl')
132 connection.end_headers()
135 playlist=parseM3U(m3u_file)
137 connection.dieWithError(404)
141 connection.dieWithError()
144 if self.splitted_path[1]=="list":
148 for record in playlist:
150 exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
154 playlistgen = PlaylistGenerator()
156 for record in playlist:
159 channel['name']=record.title.decode('utf-8')
160 channel['url']=record.path.decode('utf-8')
161 playlistgen.addItem(channel)
163 exported = playlistgen.exportm3u(hostport,prefix)
165 exported = exported.encode('utf-8')
167 connection.wfile.write(exported)
169 def getparam(self, key):
170 if key in self.params:
171 return self.params[key][0]