def download_urls(urls, title, ext, total_size, output_dir='.', refer=None, merge=True): assert urls assert ext in ('flv', 'mp4') if not total_size: try: total_size = urls_size(urls) except: import traceback import sys traceback.print_exc(file=sys.stdout) pass title = to_native_string(title) title = escape_file_path(title) filename = '%s.%s' % (title, ext) filepath = os.path.join(output_dir, filename) if total_size: if os.path.exists( filepath) and os.path.getsize(filepath) >= total_size * 0.9: print 'Skip %s: file already exists' % filepath return bar = SimpleProgressBar(total_size, len(urls)) else: bar = PiecesProgressBar(total_size, len(urls)) if len(urls) == 1: url = urls[0] print 'Downloading %s ...' % filename url_save(url, filepath, bar, refer=refer) bar.done() else: flvs = [] print 'Downloading %s.%s ...' % (title, ext) for i, url in enumerate(urls): filename = '%s[%02d].%s' % (title, i, ext) filepath = os.path.join(output_dir, filename) flvs.append(filepath) #print 'Downloading %s [%s/%s]...' % (filename, i+1, len(urls)) bar.update_piece(i + 1) url_save(url, filepath, bar, refer=refer) bar.done() if not merge: return if ext == 'flv': from flv_join import concat_flvs concat_flvs(flvs, os.path.join(output_dir, title + '.flv')) for flv in flvs: os.remove(flv) elif ext == 'mp4': from mp4_join import concat_mp4s concat_mp4s(flvs, os.path.join(output_dir, title + '.mp4')) for flv in flvs: os.remove(flv) else: print "Can't join %s files" % ext
def download_urls(urls, title, ext, total_size, output_dir='.', refer=None, merge=True): assert urls assert ext in ('flv', 'mp4') if not total_size: try: total_size = urls_size(urls) except: import traceback import sys traceback.print_exc(file=sys.stdout) pass title = to_native_string(title) title = escape_file_path(title) filename = '%s.%s' % (title, ext) filepath = os.path.join(output_dir, filename) if total_size: if os.path.exists(filepath) and os.path.getsize(filepath) >= total_size * 0.9: print('Skip %s: file already exists' % filepath) return bar = SimpleProgressBar(total_size, len(urls)) else: bar = PiecesProgressBar(total_size, len(urls)) if len(urls) == 1: url = urls[0] print('Downloading %s ...' % filename) url_save(url, filepath, bar, refer=refer) bar.done() else: flvs = [] print('Downloading %s.%s ...' % (title, ext)) for i, url in enumerate(urls): filename = '%s[%02d].%s' % (title, i, ext) filepath = os.path.join(output_dir, filename) flvs.append(filepath) #print 'Downloading %s [%s/%s]...' % (filename, i+1, len(urls)) bar.update_piece(i+1) url_save(url, filepath, bar, refer=refer) bar.done() if not merge: return if ext == 'flv': from flv_join import concat_flvs concat_flvs(flvs, os.path.join(output_dir, title+'.flv')) for flv in flvs: os.remove(flv) elif ext == 'mp4': from mp4_join import concat_mp4s concat_mp4s(flvs, os.path.join(output_dir, title+'.mp4')) for flv in flvs: os.remove(flv) else: print "Can't join %s files" % ext
def download_urls(urls, title, ext, total_size, output_dir=".", refer=None): assert urls assert ext in ("flv", "mp4") if not total_size: try: total_size = urls_size(urls) except: pass title = escape_file_path(title) title = to_native_string(title) filename = "%s.%s" % (title, ext) filepath = os.path.join(output_dir, filename) if total_size: if os.path.exists(filepath) and os.path.getsize(filepath) >= total_size * 0.9: print "Skip %s: file already exists" % filepath return bar = SimpleProgressBar(total_size, len(urls)) else: bar = PiecesProgressBar(total_size, len(urls)) if len(urls) == 1: url = urls[0] print "Downloading %s ..." % filename url_save(url, filepath, bar, refer=refer) bar.done() else: flvs = [] print "Downloading %s.%s ..." % (title, ext) for i, url in enumerate(urls): filename = "%s[%02d].%s" % (title, i, ext) filepath = os.path.join(output_dir, filename) flvs.append(filepath) # print 'Downloading %s [%s/%s]...' % (filename, i+1, len(urls)) bar.update_piece(i + 1) url_save(url, filepath, bar, refer=refer) bar.done() if ext == "flv": from flv_join import concat_flvs concat_flvs(flvs, os.path.join(output_dir, title + ".flv")) for flv in flvs: os.remove(flv) elif ext == "mp4": from mp4_join import concat_mp4s concat_mp4s(flvs, os.path.join(output_dir, title + ".mp4")) for flv in flvs: os.remove(flv) else: print "Can't join %s files" % ext
def youku_download(url, output_dir="", stream_type=None): id2, title, subtitle = parse_page(url) if subtitle: title += "-" + subtitle if type(title) == unicode: encoding = sys.getfilesystemencoding() if encoding.lower() == "ascii": encoding = "utf-8" title = title.encode(encoding) info = get_info(id2) urls, sizes = zip(*find_video(info, stream_type)) total_size = sum(sizes) bar = SimpleProgressBar(total_size, len(urls)) assert urls if len(urls) == 1: url = urls[0] filename = "%s.%s" % (title, file_type_of_url(url)) filepath = os.path.join(output_dir, filename) print "Downloading %s ..." % filename url_save(url, filepath, bar) bar.done() else: flvs = [] file_type = file_type_of_url(urls[0]) print "Downloading %s.%s ..." % (title, file_type) for i, url in enumerate(urls): filename = "%s[%02d].%s" % (title, i, file_type_of_url(url)) filepath = os.path.join(output_dir, filename) flvs.append(filepath) # print 'Downloading %s [%s/%s]...' % (filename, i+1, len(urls)) bar.update_piece(i + 1) url_save(url, filepath, bar) bar.done() if file_type == "flv": from flv_join import concat_flvs concat_flvs(flvs, os.path.join(output_dir, title + ".flv")) for flv in flvs: os.remove(flv) elif file_type == "mp4": from mp4_join import concat_mp4s concat_mp4s(flvs, os.path.join(output_dir, title + ".mp4")) for flv in flvs: os.remove(flv) else: print "Can't join %s files" % file_type
def youku_download(url, output_dir='', stream_type=None): id2, title, subtitle = parse_page(url) if subtitle: title += '-' + subtitle if type(title) == unicode: encoding = sys.getfilesystemencoding() if encoding.lower() == 'ascii': encoding = 'utf-8' title = title.encode(encoding) info = get_info(id2) urls, sizes = zip(*find_video(info, stream_type)) total_size = sum(sizes) bar = SimpleProgressBar(total_size, len(urls)) assert urls if len(urls) == 1: url = urls[0] filename = '%s.%s' % (title, file_type_of_url(url)) filepath = os.path.join(output_dir, filename) print 'Downloading %s ...' % filename url_save(url, filepath, bar) bar.done() else: flvs = [] file_type = file_type_of_url(urls[0]) print 'Downloading %s.%s ...' % (title, file_type) for i, url in enumerate(urls): filename = '%s[%02d].%s' % (title, i, file_type_of_url(url)) filepath = os.path.join(output_dir, filename) flvs.append(filepath) #print 'Downloading %s [%s/%s]...' % (filename, i+1, len(urls)) bar.update_piece(i + 1) url_save(url, filepath, bar) bar.done() if file_type == 'flv': from flv_join import concat_flvs concat_flvs(flvs, os.path.join(output_dir, title + '.flv')) for flv in flvs: os.remove(flv) elif file_type == 'mp4': from mp4_join import concat_mp4s concat_mp4s(flvs, os.path.join(output_dir, title + '.mp4')) for flv in flvs: os.remove(flv) else: print "Can't join %s files" % file_type
def download_urls(urls, title, ext, total_size, n=10, output_dir=".", refer=None, merge=True): assert urls assert ext in ("flv", "mp4") if not total_size: try: total_size = urls_size(urls) except: import traceback import sys traceback.print_exc(file=sys.stdout) pass title = to_native_string(title) title = escape_file_path(title) filename = "%s.%s" % (title, ext) file_path = os.path.join(output_dir, filename) if total_size: if os.path.exists(file_path) and os.path.getsize(file_path) >= total_size * 0.9: print "Skip %s: file already exists" % file_path return bar = SimpleProgressBar(total_size, len(urls)) else: bar = PiecesProgressBar(total_size, len(urls)) if len(urls) == 1: _dl_methods(url=urls[0], file_path=file_path + "!", n_per_file=n, refer=refer) os.rename(file_path + "!", file_path) else: files = [] multithread = [] print "Downloading %s.%s ..." % (title, ext) tmp_path = os.path.join(output_dir, ".dlvideo") if not os.path.isdir(tmp_path): os.mkdir(tmp_path) for i, url in enumerate(urls): filename = "%s[%02d].%s" % (title, i, ext) file_path = os.path.join(tmp_path, filename) files.append(file_path) bar.update_piece(i + 1) print "[url] ", url multithread.append(DownloadThread(url, file_path, bar, refer)) for t in multithread: t.join() bar.done() if not merge: print "not Merge?" return if ext == "flv": from flv_join import concat_flvs concat_flvs(files, os.path.join(output_dir, title + ".flv")) for f in files: os.remove(f) elif ext == "mp4": from mp4_join import concat_mp4s concat_mp4s(files, os.path.join(output_dir, title + ".mp4")) for f in files: os.remove(f) else: print "Can't join %s files" % ext os.system('say "Can\'t join %s files"' % ext)