"/" handling in channel names
[vpproxy.git] / plugins / m3u_plugin.py
1 __author__ = 'rvb'
2 '''
3 Local Playlist Plugin
4 (based on ytv plugin by ValdikSS)
5
6 plsylist index
7
8 http://ip:port/index
9
10 m3u playlists
11
12 http://ip:port/m3u
13 http://ip:port/m3u/
14 http://ip:port/m3u/list-name
15
16 plain-tet channel names
17
18 http://ip:port/list
19 http://ip:port/list/
20 http://ip:port/list/list-name
21
22 forward to player
23
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
27
28 '''
29 import json
30 import logging
31 import urlparse
32 import urllib
33 from modules.PluginInterface import VPProxyPlugin
34 from modules.PlaylistGenerator import PlaylistGenerator
35 from modules.M3uParser import parseM3U
36 import config.m3u as config
37 import os
38
39 class M3u(VPProxyPlugin):
40
41     handlers = ('m3u', 'm3ut', 'm3uw', "list", "play", "index")
42
43     logger = logging.getLogger('plugin_m3u')
44     playlist = None
45
46     def handle(self, connection):
47
48         hostport = connection.headers['Host']
49
50         self.splitted_path=connection.path.split('/')
51
52         if self.splitted_path[1]=='m3u':
53           prefix='get'
54         elif self.splitted_path[1]=='m3uw':
55           prefix='webm'  
56         elif self.splitted_path[1]=='m3ut':
57           prefix='mp4'  
58         elif self.splitted_path[1] in ("list","play","index"):
59           None  
60         else:
61           connection.dieWithError(404)  
62           
63         if len(self.splitted_path)>3 and self.splitted_path[1]!="play":
64             connection.dieWithError()
65             return
66
67         if self.splitted_path[1]=='index':
68
69             for dir in os.walk(config.m3u_directory):
70                 if dir[0]==config.m3u_directory:
71                     text='\n'.join(dir[2])
72
73             connection.send_response(200)
74             connection.send_header('Content-Type', 'text/plain; charset=utf-8')
75             connection.end_headers()
76
77             listing = text.encode('utf-8')
78             connection.wfile.write(listing) 
79                 
80             return
81             
82         if len(self.splitted_path)<3 or (len(self.splitted_path)==3 and self.splitted_path[1]=="play"):
83
84            m3u_file=config.m3u_directory+'/'+config.m3u_default
85
86         else:
87         
88            filename = self.splitted_path[2]
89            if filename:
90              m3u_file=config.m3u_directory+'/'+self.splitted_path[2]
91            else:
92              m3u_file=config.m3u_directory+'/'+config.m3u_default
93
94         try:
95             playlist=parseM3U(m3u_file)
96         except:
97             connection.dieWithError(404)
98             return    
99
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             print channel
108             if len(self.splitted_path)<=4:
109               prefix="get"
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()
114             else:
115               connection.dieWithError()  
116             url=None
117             for record in playlist:
118                 if record.title.decode('utf-8').replace('/','')==channel:
119                     url=record.path.decode('utf-8')
120                     print url
121             if url:
122                 redirect='/'+prefix+'/'+url
123                 print redirect
124                 connection.send_response(302)
125                 connection.send_header('Location', redirect)
126                 connection.end_headers()
127             else:
128                 connection.dieWithError(404)
129             
130         else:
131             connection.send_response(200)
132             connection.send_header('Content-Type', 'application/x-mpegurl')
133             connection.end_headers()
134
135         try:
136             playlist=parseM3U(m3u_file)
137         except:
138             connection.dieWithError(404)
139             return    
140
141         if not playlist:
142             connection.dieWithError()
143             return
144
145         if self.splitted_path[1]=="list":
146  
147              exported = ""
148             
149              for record in playlist:
150                  exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
151         
152         else:    
153
154             playlistgen = PlaylistGenerator()
155
156             for record in playlist:
157                 channel=dict()
158                 channel['name']=record.title.decode('utf-8')
159                 channel['url']=record.path.decode('utf-8')            
160                 playlistgen.addItem(channel)
161
162             exported = playlistgen.exportm3u(hostport,prefix)
163             
164         exported = exported.encode('utf-8')
165         connection.wfile.write(exported)
166
167     def getparam(self, key):
168         if key in self.params:
169             return self.params[key][0]
170         else:
171             return None