1-pyDownloader.py
3- __init__.py
metodo de uso
#by hackloper #skype: hackloper #youtube: https://www.youtube.com/user/hackdeveloper #gmail: hackloper.soporte@gmail.com #twitter: https://twitter.com/hackloper | @hackloper #blog: http://hackloper.blogspot.com.es/ import urllib2,urlparse,urllib,threading,time,os,re,pyffmpeg class ErrorDownload(Exception): def __init__(self,error): self._error = error def __str__(self): return repr(self._error) class pyDownloader(object): def __init__(self,url,convert_to=False,automatic_print=False): self._id = urlparse.parse_qs(urlparse.urlparse(url).query)['v'][0] self._path = "https://www.youtube.com/get_video_info?video_id=" self._content = self.get_content() try: self._streams = self.get_streams() except KeyError: raise ErrorDownload("Invalid url.") self.convert_to = convert_to self.automatic_print = automatic_print self._end_download = False def print_write_size(self,size,name,extension): d = os.getcwd() error = 0 total_name = str("%s\\%s.%s" %(d,name,extension)) time.sleep(3) while True: try: while int(os.path.getsize(total_name)) != int(size): print str(int(os.path.getsize(total_name))*100/int(size))+"%" time.sleep(0.3) print "100%" break except: if error >= 6:break error += 1 print "error" time.sleep(1) def get_size_write(self): d = os.getcwd() try: porcent = str(int(os.path.getsize(self.total_name))*100/int(self.size))+"%" return porcent except WindowsError: return "waiting" def get_info(self): return self._content def get_size_convert(self): if self.convert_to == False: return False else: if self._end_download == False: return "waiting" else: progress = pyffmpeg.progress() rest = progress.get_size_convert() return rest def get_content(self): response = urllib2.urlopen(self._path+self._id) parse_content = urlparse.parse_qs(response.read()) return parse_content def get_streams(self): streams = self._content['url_encoded_fmt_stream_map'][0] streams = streams.split(',') streams = [urlparse.parse_qs(elem) for elem in streams] return streams def select_url_stream(self,type_file): for i in self._streams: t = re.search(r'/([\w?-]+)',i['type'][0]) if t.group(1) == type_file: return i['url'][0] def download_manual(self,url_stream,type_f,to,obj): urllib.urlretrieve(url_stream,'%s.%s' %(obj.title,type_f)) obj._end_download = True if obj.convert_to == True: ffmpeg = pyffmpeg.ffmpeg(obj.title,obj.total_name,to) ffmpeg.convert() def download(self,type_f='mp4',to='mp3'): self.title = self._content['title'][0] site = urllib.urlopen(self.select_url_stream(type_f)) self.size = site.info().getheaders('Content-Length')[0] self.extension = type_f d = os.getcwd() self.total_name = str("%s\\%s.%s" %(d,self.title,type_f)) if int(self.size) == 0: raise ErrorDownload("This video can not be downloaded by copyright") else: if self.automatic_print == True: th = threading.Thread(target=self.print_write_size,args=(self.size,self.title,type_f)) th.start() urllib.urlretrieve(self.select_url_stream(type_f),'%s.%s' %(self.title,type_f)) if self.convert_to == True: ffmpeg = pyffmpeg.ffmpeg(self.title,self.total_name,to) ffmpeg.convert() else: url_stream = self.select_url_stream(type_f) thread = threading.Thread(target=self.download_manual,\ args=(url_stream,type_f,to,self)) thread.start()2-nueva carpeta "pyffmpeg"
3- __init__.py
from main import *4-main.py
#by hackloper #skype: hackloper #youtube: https://www.youtube.com/user/hackdeveloper #gmail: hackloper.soporte@gmail.com #twitter: https://twitter.com/hackloper | @hackloper #blog: http://hackloper.blogspot.com.es/ import threading,os,re,time class progress(threading.Thread): def __init__(self): threading.Thread.__init__(self) def get_seconds(self,times): list_time = [360,60,0] seconds = 0 split_time = times.split(":") for i in range(3): seconds += int(split_time[i])*list_time[i] return seconds def get_total_time(self): times = None f = self.open_file() for line in f.readlines(): if "Duration" in line: times = re.search(r"Duration: ([\w?:]+)",line).group(1) f.close() return int(self.get_seconds(times)) def get_actual_time(self,line): times = re.findall(r"time=([\w?:]+)",line) return self.get_seconds(times[-1]) def open_file(self): while True: if os.path.exists("log.txt"): f = open("log.txt","r") return f else: time.sleep(1) continue def get_size_convert(self): total_time = self.get_total_time() f = self.open_file() last_line = f.readlines()[-1] if "time=" in last_line: actual_time = self.get_actual_time(last_line) f.close() porcentaje = (100*actual_time)/total_time return str(porcentaje)+"%" time.sleep(1) elif "muxing overhead" in last_line: return "100%" else: return "waiting" def run(self): total_time = self.get_total_time() while True: f = self.open_file() last_line = f.readlines()[-1] if "time=" in last_line: actual_time = self.get_actual_time(last_line) f.close() porcentaje = (100*actual_time)/total_time print str(porcentaje)+"%" time.sleep(1) elif "muxing overhead": print "100%" break else: break class ffmpeg(object): def __init__(self,name,total_dir,fmt_out='mp3',automatic_print=False): self._fmt_out = fmt_out self._name = name self.total_dir = total_dir self.automatic_print = automatic_print def convert(self): if os.path.exists("log.txt"): os.remove("log.txt") if self.automatic_print == True: print "entra automatico" p = progress() p.daemon = True p.start() command = 'pyffmpeg\\ffmpeg.exe -i "%s" -y -f %s "%s.%s" 2> log.txt' %(self.total_dir,self._fmt_out,self._name,self._fmt_out) os.system(command)5- Tener el archivo ffmpeg.exe en la carpeta pyffmpeg
metodo de uso
obj = pyDownloader("url_completa",convert_to=True,automatic_print=False)#"convert_to" y "automatic_print" por defecto es "False" obj.download(type_f='ogg',to='flv')#"type_f" por defecto es "mp4", "to" por defecto es "mp3" def download(): while True: info_download = obj.get_size_write() if info_download == "waiting": time.sleep(1) elif info_download == "100%": print info_download break else: print info_download time.sleep(1) def convert(): while True: info_convert = obj.get_size_convert() if info_convert == False: break elif info_convert == "waiting": time.sleep(1) elif info_convert == "100%": print info_convert break else: print info_convert time.sleep(1) download() convert()
0 comentarios:
Publicar un comentario