636ad4e19d6f72e348d4aebf18992496c687f6b5
[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', 'm3uo', '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='ogg'  
56         elif self.splitted_path[1]=='m3ut':
57           prefix='mp4'  
58         elif self.splitted_path[1]=='m3uo':
59           prefix='ogv'  
60         elif self.splitted_path[1] in ("list","play","index"):
61           None  
62         else:
63           connection.dieWithError(404)  
64           
65         if len(self.splitted_path)>3 and self.splitted_path[1]!="play":
66             connection.dieWithError()
67             return
68
69         if self.splitted_path[1]=='index':
70
71             for dir in os.walk(config.m3u_directory):
72                 if dir[0]==config.m3u_directory:
73                     text='\n'.join(dir[2])
74
75             connection.send_response(200)
76             connection.send_header('Content-Type', 'text/plain; charset=utf-8')
77             connection.end_headers()
78
79             listing = text.encode('utf-8')
80             connection.wfile.write(listing) 
81                 
82             return
83             
84         if len(self.splitted_path)<3 or (len(self.splitted_path)==3 and self.splitted_path[1]=="play"):
85
86            m3u_file=config.m3u_directory+'/'+config.m3u_default
87
88         else:
89         
90            filename = self.splitted_path[2]
91            if filename:
92              m3u_file=config.m3u_directory+'/'+self.splitted_path[2]
93            else:
94              m3u_file=config.m3u_directory+'/'+config.m3u_default
95
96         try:
97             playlist=parseM3U(m3u_file)
98         except:
99             connection.dieWithError(404)
100             return    
101
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:
110               prefix="get"
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()
115             else:
116               connection.dieWithError()  
117             url=None
118             for record in playlist:
119                 if record.title.decode('utf-8').replace('/','')==channel:
120                     url=record.path.decode('utf-8')
121             if url:
122                 redirect='/'+prefix+'/'+url
123                 connection.send_response(302)
124                 connection.send_header('Location', redirect)
125                 connection.end_headers()
126             else:
127                 connection.dieWithError(404)
128             
129         else:
130             connection.send_response(200)
131             connection.send_header('Content-Type', 'application/x-mpegurl')
132             connection.end_headers()
133
134         try:
135             playlist=parseM3U(m3u_file)
136         except:
137             connection.dieWithError(404)
138             return    
139
140         if not playlist:
141             connection.dieWithError()
142             return
143
144         if self.splitted_path[1]=="list":
145  
146              exported = ""
147             
148              for record in playlist:
149                  if record.title:
150                      exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
151         
152         else:    
153
154             playlistgen = PlaylistGenerator()
155
156             for record in playlist:
157                 if record.title:
158                     channel=dict()
159                     channel['name']=record.title.decode('utf-8')
160                     channel['url']=record.path.decode('utf-8')            
161                     playlistgen.addItem(channel)
162
163             exported = playlistgen.exportm3u(hostport,prefix)
164
165         exported = exported.encode('utf-8')
166         
167         connection.wfile.write(exported)
168
169     def getparam(self, key):
170         if key in self.params:
171             return self.params[key][0]
172         else:
173             return None