3d2de5c5335cd4f6d329f43aec1cb0e558560748
[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='ogg'  
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             if len(self.splitted_path)<=4:
108               prefix="get"
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()
113             else:
114               connection.dieWithError()  
115             url=None
116             for record in playlist:
117                 if record.title.decode('utf-8').replace('/','')==channel:
118                     url=record.path.decode('utf-8')
119             if url:
120                 redirect='/'+prefix+'/'+url
121                 connection.send_response(302)
122                 connection.send_header('Location', redirect)
123                 connection.end_headers()
124             else:
125                 connection.dieWithError(404)
126             
127         else:
128             connection.send_response(200)
129             connection.send_header('Content-Type', 'application/x-mpegurl')
130             connection.end_headers()
131
132         try:
133             playlist=parseM3U(m3u_file)
134         except:
135             connection.dieWithError(404)
136             return    
137
138         if not playlist:
139             connection.dieWithError()
140             return
141
142         if self.splitted_path[1]=="list":
143  
144              exported = ""
145             
146              for record in playlist:
147                  if record.title:
148                      exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
149         
150         else:    
151
152             playlistgen = PlaylistGenerator()
153
154             for record in playlist:
155                 print record
156                 channel=dict()
157                 channel['name']=record.title.decode('utf-8')
158                 channel['url']=record.path.decode('utf-8')            
159                 playlistgen.addItem(channel)
160
161             exported = playlistgen.exportm3u(hostport,prefix)
162
163         exported = exported.encode('utf-8')
164         
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