def start(self): if DB_TYPE == 'sqlite': self.DB = MyDatabaseAPI(DB_FILE, connect=False, quiet=True, check_version=False) else: self.DB = DB ADDON.log("Service starting...", 1) monitor = xbmc.Monitor() self.clear_cache() ADDON.log("Waiting to Transmogrify...",1) if ADDON.get_setting('network_bind') == 'Localhost': address = "127.0.0.1" else: address = "0.0.0.0" ADDON.log("Launching WebInterface on: %s:%s" % (address, CONTROL_PORT)) server_class = ThreadedHTTPServer httpd = server_class((address, CONTROL_PORT), RequestHandler) webserver = Thread(target=httpd.serve_forever) webserver.start() self.DB.connect() while not monitor.abortRequested(): if monitor.waitForAbort(1) or get_property('service.abort'): break filename, url, raw_url, id, file_id, video_type, save_dir = self.poll_queue() if id: ADDON.log("Starting to Transmogrify: %s" % filename,1) self.id=id started = time.time() set_property("caching.file_id", file_id) TM = Transmogrifier(id, url, raw_url, filename, file_id, video_type=video_type, save_dir=save_dir) TM.start() if get_property("abort_all")=="true": self.DB.execute("UPDATE queue SET status=-1 WHERE id=?", [self.id]) else: set_property("caching.complete", file_id) self.DB.execute("UPDATE queue SET status=3 WHERE id=?", [self.id]) try: if NOTIFICATION: from subprocess import call wav = vfs.join(ROOT_PATH, 'resources/notifications/'+NOTIFICATION+'.wav') if PLATFORM.startswith('linux'): call(["aplay", wav]) elif PLATFORM.startswith('darwin'): call(["afplay"], wav) elif PLATFORM.startswith('win'): import winsound winsound.PlaySound(wav, winsound.SND_FILENAME) except: pass self.DB.commit() del TM httpd.socket.close() ADDON.log("Service stopping...", 1)
def do_HEAD(self): ADDON.log(str(self.headers), LOG_LEVEL.VERBOSE) arguments, data, path = self.process_cgi() file_id = arguments[3] if get_property("streaming.file_id") != file_id: set_property("streaming.file_id", file_id) streaming_url = base64.b64decode(arguments[2]) set_property("streaming.url", streaming_url) TM = Transmogrifier(0, get_property("streaming.url"), '', 'stream.avi', get_property("streaming.file_id"), video_type='stream') TM.get_target_info() set_property("streaming.total_bytes", TM.total_bytes) set_property("streaming.total_blocks", TM.total_blocks) del TM total_bytes = int(get_property("streaming.total_bytes")) self.generate_respose_headers() self._response_headers['Content-Length'] = total_bytes self.send_response(200) for header in self._response_headers.keys(): self.send_header(header, self._response_headers[header]) self.end_headers() ADDON.log(str(self._response_headers), LOG_LEVEL.VERBOSE)
def do_GET(self): arguments, data, path = self.process_cgi() if True: #try: if arguments[1] == 'query': if arguments[2] == 'log': logfile = vfs.join('special://logpath', 'kodi.log') f = vfs.open(logfile) contents = f.read() f.close() contents = re.sub('<host>(.+?)</host>', '<pass>******</pass>', contents) contents = re.sub('<name>(.+?)</name>', '<name>******</name>', contents) contents = re.sub('<user>(.+?)</user>', '<user>******</user>', contents) contents = re.sub('<pass>(.+?)</pass>', '<pass>******</pass>', contents) self._send_response(contents, mime="text/plain") elif arguments[2] == 'download': if data['media'][0] == 'movie': path = vfs.join(MOVIE_DIRECTORY, data['file'][0]) else: path = vfs.join(TVSHOW_DIRECTORY, data['file'][0]) if not vfs.exists(path): self.send_error(404,'File Not Found: %s' % self.path) return False file_size = vfs.get_size(path) f = vfs.open(path, "r") self.send_response(200) self.send_header("Pragma", "public") self.send_header("Expires", "0") self.send_header("Cache-Control", "must-revalidate, post-check=0, pre-check=0") self.send_header("Content-Type", "application/force-download") self.send_header("Content-Type", "application/octet-stream") self.send_header("Content-Type", "application/download") self.send_header("Content-Disposition", 'attachment; filename="%s"' % data['file'][0]); self.send_header("Content-Length", file_size) self.end_headers() self.wfile.write(f.read()) f.close() else: self.send_error(400,'Bad Request') elif arguments[1] == 'stream': ADDON.log(str(self.headers), LOG_LEVEL.VERBOSE) hash_url = arguments[2] file_id = arguments[3] url = base64.b64decode(hash_url) self.generate_respose_headers() self._response_headers['Content-Length'] = get_property("streaming.total_bytes") TM = Transmogrifier(0, get_property("streaming.url"), '', 'stream.avi', get_property("streaming.file_id"), video_type='stream') try: TM.total_bytes = int(get_property("streaming.total_bytes")) TM.total_blocks = int(get_property("streaming.total_blocks")) except: self.send_error(500,'Internal Server Error') current_byte = 0 try: range_reqeust = str(self.headers.getheader("Range")) temp=range_reqeust.split("=")[1].split("-") current_byte=int(temp[0]) end_byte = TM.total_bytes - 1 if temp[1] == "" else temp[1] except: current_byte = 0 end_byte = TM.total_bytes - 1 content_range = "%s-%s/%s" % (current_byte, end_byte, TM.total_bytes) self._response_headers['Content-Range'] = content_range self.send_response(206) for header in self._response_headers.keys(): self.send_header(header, self._response_headers[header]) self.end_headers() if (end_byte - current_byte) < 65537 : ADDON.log("Kodi wants the last 2 byte chunks. Lets get them and send it", LOG_LEVEL.VERBOSE) tail_bytes = TM.get_tail_byte() self.wfile.write(tail_bytes) else: ADDON.log(str(self._response_headers), LOG_LEVEL.VERBOSE) if not get_property("streaming.started"): set_property("streaming.started", "true") ADDON.log("Start streaming at %s bytes" % current_byte, LOG_LEVEL.VERBOSE) TM.stream(current_byte) else: ADDON.log("Attempt to seek to %s bytes (%s)" % (current_byte, end_byte - current_byte), LOG_LEVEL.VERBOSE) TM.seek(current_byte) self.send_video(TM, current_byte, TM.total_bytes) del TM else: if self.path=='/': self.path='/index.html' fileName, fileExtension = os.path.splitext(self.path) headers = { '.css': 'text/css', '.js': 'application/javascript', '.json': 'application/json', '.ico': 'image/x-icon', '.jpg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif', '.html': 'text/html', '.txt': 'text/plain', } if fileExtension not in headers.keys(): self.send_error(403,'Forbidden') return False full_path = WEB_ROOT + os.sep + self.path if not vfs.exists(full_path): self.send_error(404,'File Not Found: %s' % self.path) return False self.send_response(200) self.send_header('Content-type', headers[fileExtension]) f = open(full_path) self.end_headers() self.wfile.write(f.read()) f.close() self.wfile.flush() self.wfile.close() return True return True #except IOError: # self.send_error(500,'Internal Server Error') # self.wfile.close() # return False self.wfile.close()