[svn r948] Added new function to retrieve old schedules in order to reactivate them
3 __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
4 __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
18 import lib.utils as utils
19 import lib.file_handler as files
20 import lib.transcoder as transcoder
24 __all__ = ("RequestHandler")
26 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
27 """Class that implements an HTTP request handler for our server."""
28 log = logging.getLogger("gms.request")
30 transcoders = utils.PluginSet(transcoder.Transcoder)
31 transcoders_log = Log()
36 "Stop": "/stop-transcoder.do",
37 "Status": "/status.do",
38 "All Log": "/get_all_log.do",
39 "Version": "/version.do",
40 "Shutdown": "/shutdown.do"
44 def load_plugins_transcoders(cls, directory):
45 cls.transcoders.load_from_directory(directory)
47 if cls.def_transcoder is None and cls.transcoders:
48 cls.def_transcoder = cls.transcoders[0].name
49 # load_plugins_transcoders()
52 def do_dispatch(self, body):
54 pieces = urlparse.urlparse(self.path)
56 self.query = cgi.parse_qs(pieces[4])
60 "/shutdown.do": self.serve_shutdown,
61 "/stop-transcoder.do": self.serve_stop_transcoder,
62 "/status.do": self.serve_status,
63 "/version.do": self.serve_version,
64 "/new_id.do": self.serve_new_id,
65 "/get_log.do": self.serve_get_log,
66 "/get_all_log.do": self.serve_get_all_log,
67 "/stream.do": self.serve_stream,
68 "/transcode.do": self.serve_transcode,
69 "/list.do": self.serve_list,
70 "/get_file_info.do": self.serve_file_info,
77 action = self.query.get("action", None)
78 if action and "stream.do" in action:
79 self.serve_stream(body)
80 elif os.path.exists("html/%s" % self.path):
81 data = open("html/%s" % self.path)
82 self.wfile.write(data.read())
84 self.send_error(404, "File not found")
92 self.do_dispatch(True)
97 self.do_dispatch(False)
101 def _nav_items(self):
103 for name, url in self.menu.items():
104 ret += utils.getHTML("menu", {"name": name, "url": url})
109 def serve_main(self, body):
110 self.send_response(200)
111 self.send_header("Content-Type", "text/html")
112 self.send_header('Connection', 'close')
115 self.wfile.write(utils.getHTML("index", {"menu": self._nav_items()}))
119 def serve_version(self, body):
120 self.send_response(200)
121 self.send_header("Content-Type", "text/html")
122 self.send_header('Connection', 'close')
125 self.wfile.write("Version: %s" % __version__)
129 def serve_shutdown(self, body):
130 self.send_response(200)
131 self.send_header("Content-Type", "text/html")
132 self.send_header('Connection', 'close')
135 self.wfile.write(utils.getHTML("shutdown"))
136 self.server.server_close()
140 def serve_list(self, body):
141 self.send_response(200)
142 self.send_header("Content-Type", "text/html")
143 self.send_header('Connection', 'close')
148 files.list_media_files(utils.config.get_transcoded_location(), file_list)
149 output = files.FileList(map(lambda x, y: x+y, file_list,
150 ["<br>"]*len(file_list)))
151 self.wfile.write(output)
156 def serve_stop_all_transcoders(self, body):
157 self.send_response(200)
158 self.send_header("Content-Type", "text/html")
159 self.send_header('Connection', 'close')
162 self.server.stop_transcoders()
163 self.wfile.write(utils.getHTML("stop_all",
164 {"menu": self._nav_items()}))
165 # serve_stop_all_transcoders()
168 def serve_stop_selected_transcoders(self, body, tids=[]):
169 self.send_response(200)
170 self.send_header("Content-Type", "text/html")
171 self.send_header('Connection', 'close')
175 transcoders = self.server.get_transcoders()
178 for t, r in transcoders:
179 if t.tid == int(tid):
183 self.log.info("Plugin already stopped")
185 opts += utils._create_html_item("%s" % t)
189 self.wfile.write(utils.getHTML("stop_selected",
190 {"menu": self._nav_items(),
192 # serve_stop_selected_transcoders()
195 def serve_stop_transcoder(self, body):
196 req = self.query.get("request", None)
197 tid = self.query.get("tid", None)
198 if req and "all" in req:
199 self.serve_stop_all_transcoders(body)
201 self.serve_stop_selected_transcoders(body, tid[0].split(";"))
203 self.serve_status(body)
204 # serve_stop_transcoder()
207 def serve_status(self, body):
208 self.send_response(200)
209 self.send_header("Content-Type", "text/html")
210 self.send_header('Connection', 'close')
217 tl = self.server.get_transcoders()
218 if not tl and not self.query.get("tid", None) and \
219 not self.query.get("running", None):
220 running = "<p>No running transcoder.</p>\n"
222 elif not tl and self.query.get("tid", None):
223 tids = self.query.get("tid")
225 stat = self.transcoders_log.get_status(int(tid))
226 self.wfile.write("%s<br>" % stat)
229 elif self.query.get("running", None):
230 for transcoder, request in tl:
231 outf = transcoder.params_first("outfile")
233 self.wfile.write("%s:%s<br>" % (tid, outf))
236 elif self.query.get("tid", None):
237 req_tid = self.query.get("tid")
238 for transcoder, request in tl:
239 if str(transcoder.tid) in req_tid:
240 self.wfile.write("Status:%s:%s %%" % (\
241 transcoder.tid, transcoder.status))
243 stat = self.transcoders_log.get_status(int(req_tid[0]))
244 self.wfile.write("%s<br>" % stat)
248 running = "<p>Running transcoders:</p>\n"
249 stopall = utils._create_html_item("<a href='%s?request=all'>"
253 for transcoder, request in tl:
254 stopone += utils._create_html_item("%s;"
255 "<a href='%s?tid=%s'>"
257 transcoder, self.menu["Stop"], transcoder.tid)
259 self.wfile.write(utils.getHTML("status",
260 {"menu": self._nav_items(),
263 "stopone": stopone}))
267 def _get_transcoder(self):
268 # get transcoder option: mencoder is the default
269 request_transcoders = self.query.get("transcoder", ["mencoder"])
271 for t in request_transcoders:
272 transcoder = self.transcoders.get(t)
277 return self.transcoders[self.def_transcoder]
281 def _get_new_id(self, tid):
282 self.server.last_tid = utils.create_tid(tid)
283 self.tid_queue.append(self.server.last_tid)
284 return self.server.last_tid
288 def serve_new_id(self, body):
289 self.send_response(200)
290 self.send_header("Content-Type", "text/html")
291 self.send_header('Connection', 'close')
295 self.wfile.write("%s" % self._get_new_id(self.server.last_tid))
298 def serve_get_log(self, body):
299 self.send_response(200)
300 self.send_header("Content-Type", "text/html")
301 self.send_header('Connection', 'close')
305 if self.query.get("tid", None):
306 tid = int(self.query.get("tid")[0])
307 stat = self.transcoders_log.get_status(tid)
308 self.wfile.write("Status: %s" % stat)
310 stat = self.transcoders_log.get_status()
311 for rtid, status in stat.iteritems():
312 self.wfile.write("<b>%s</b>: %s<br><br>" % (rtid, status))
315 def serve_get_all_log(self, body):
316 self.send_response(200)
317 self.send_header("Content-Type", "text/html")
318 self.send_header('Connection', 'close')
322 if self.query.get("tid", None):
323 tid = int(self.query.get("tid")[0])
324 stat = self.transcoders_log.get_status(tid, True)
326 self.wfile.write("%s<br><br>" % status)
328 stat = self.transcoders_log.get_status(None, True)
329 for rtid, history in stat.iteritems():
330 for status in history:
331 self.wfile.write("<b>%s</b>: %s<br>" % (rtid, status))
332 self.wfile.write("<br><br>")
333 # serve_get_all_log()
336 def serve_file_info(self, body):
339 file_dat = self.query.get("file", None)
342 self.send_response(200)
343 self.send_header("Content-Type", "text/html")
344 self.send_header('Connection', 'close')
348 opts = files.TranscodedFile(file_dat[0], self.query).opts
349 for key in opts.keys():
350 self.wfile.write("%s=%s<br>" % (key, opts.get(key, "None")[0]))
353 self.send_error(500, str(e))
357 def serve_stream(self, body):
358 args = self.query.get("file", None)
360 self.send_error(404, "File not found")
365 self.send_error(404, "File not found")
368 #Only stream files on .transcode dir
369 filename = os.path.join (utils.config.get_transcoded_location(),
370 os.path.basename(filename))
371 self.log.error("Stream file: %s" % filename)
372 if not os.path.exists (filename):
373 self.send_error(404, "File not found")
376 size = int(os.path.getsize(filename))
377 self.send_response(200)
378 self.send_header("Content-Type", mimetypes.guess_type(filename)[0])
379 self.send_header("Cache-Control","no-cache")
380 self.send_header("Content-Length", size)
383 media = open(filename)
387 test_tid = int(self.query.get("tid", "0")[0])
388 if test_tid == 0 or test_tid not in self.tid_queue:
389 test_tid = self._get_new_id(self.server.last_tid)
391 self.transcoders_log.insert(test_tid, "gms.Stream: %s" % filename)
396 data_in = media.read(4096)
400 self.wfile.write(file_data)
401 #status = utils.progress_bar(total_read, size, 50)
402 #msg_status = "Status:%s:%s%%" % (test_tid, status)
403 #self.transcoders_log._update_status(test_tid, msg_status)
405 self.transcoders_log._update_status(test_tid, "OK: Done")
408 self.log.error("Stream error: %s" %e)
409 self.transcoders_log._update_status(test_tid, "Error: %s" % e)
412 def serve_transcode(self, body):
413 type = self.query.get("type", None)[0]
414 #if type.upper() == "FILE":
415 # self.send_error(404, "Transcode local files not allowed")
418 transcoder = self._get_transcoder()
420 obj = transcoder(self.query)
422 self.send_error(500, str(e))
425 self.send_response(200)
426 self.send_header("Content-Type", obj.get_mimetype())
427 self.send_header("Cache-Control","no-cache")
431 test_tid = int(self.query.get("tid", "0")[0])
432 if test_tid == 0 or test_tid not in self.tid_queue:
433 test_tid = self._get_new_id(self.server.last_tid)
435 if self.query.get("transcoder", None):
436 self.transcoders_log.insert(test_tid, "gms.%s" % obj.name)
438 obj.log = self.transcoders_log
440 self.server.add_transcoders(self, obj)
441 if obj.start(self.wfile):
442 self.transcoders_log.info(test_tid, "OK")
444 self.transcoders_log.info(test_tid, "Fail")
446 self.server.del_transcoders(self, obj)
447 files.TranscodedFile("", self.query)
452 def log_request(self, code='-', size='-'):
453 self.log.info('"%s" %s %s', self.requestline, str(code), str(size))
457 def log_error(self, format, *args):
458 self.log.error("%s: %s" % (self.address_string(), format % args))
462 def log_message(self, format, *args):
463 self.log.info("%s: %s" % (self.address_string(), format % args))