Exemplo n.º 1
0
	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()