def my_hook(d): if d['status'] == 'downloading': speed_info = "" if 'filename' in d: filename = os.path.basename(d['filename']) else: filename = "" if '_eta_str' in d and '_speed_str' in d: speed_info = " (" + d['_speed_str'] + " ETA: " + d['_eta_str'] + ")" filename = filename.encode('ascii', 'ignore').decode('ascii') dz.begin("Downloading " + filename + speed_info + "...") total_bytes = 0 if 'downloaded_bytes' in d: if 'total_bytes' in d: total_bytes = d['total_bytes'] elif 'total_bytes_estimate' in d: total_bytes = d['total_bytes_estimate'] percent = int(100 * d['downloaded_bytes'] / total_bytes) if percent > 0: utils.set_determinate_progress(True) utils.set_progress_percent(percent) if d['status'] == 'finished': utils.set_determinate_progress(False) utils.reset_progress() print('Download complete')
def my_hook(d): if d['status'] == 'downloading': speed_info = "" if 'filename' in d: filename = os.path.basename(d['filename']) else: filename = "" if '_eta_str' in d and '_speed_str' in d: speed_info = " (" + d['_speed_str'] + " ETA: " + d['_eta_str'] + ")" filename = filename.encode('ascii', 'ignore').decode('ascii') dz.begin("Downloading " + filename + speed_info + "...") total_bytes = 0 if 'downloaded_bytes' in d: if 'total_bytes' in d: total_bytes = d['total_bytes'] elif 'total_bytes_estimate' in d: total_bytes = d['total_bytes_estimate'] percent = int(100 * d['downloaded_bytes'] / total_bytes) if percent > 0: utils.set_determinate_progress(True) utils.set_progress_percent(percent) if d['status'] == 'finished': utils.set_determinate_progress(False) utils.reset_progress() print('Download complete')
def download_url(url): regex = re.compile( r'^(?:http)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if not regex.match(url): dz.fail("Not a valid video URL") dz.begin("Checking youtube-dl library is up to date...") utils.set_determinate_progress(False) updater.update_youtubedl() dz.begin("Preparing to download video...") utils.set_determinate_progress(False) utils.reset_progress() # Put ffmpeg in PATH for merging videos audio and video if 'apple_silicon' in os.environ: os.environ["PATH"] += os.pathsep + os.path.join( os.getcwd(), 'ffmpeg-arm') else: os.environ["PATH"] += os.pathsep + os.path.join(os.getcwd(), 'ffmpeg') # Download URL from clipboard sys.path.append("yt-dlp") from yt_dlp import YoutubeDL ydl_opts = { 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', 'outtmpl': os.path.join(os.environ['EXTRA_PATH'], '%(title)s.%(ext)s'), 'logger': MyLogger(), 'progress_hooks': [my_hook] } try: with YoutubeDL(ydl_opts) as ydl: ydl.download([url]) except Exception as e: print(traceback.format_exc()) dz.error("Video Download Failed", "Downloading video failed with the error:\n\n" + e.message) dz.finish("Video Download Complete") dz.url(False)
def download_url(url): regex = re.compile( r'^(?:http)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if not regex.match(url): dz.fail("Not a valid video URL") dz.begin("Checking youtube-dl library is up to date...") utils.set_determinate_progress(False) updater.update_youtubedl() dz.begin("Preparing to download video...") utils.set_determinate_progress(False) utils.reset_progress() # Put ffmpeg in PATH for merging videos audio and video os.environ["PATH"] += os.pathsep + os.path.join(os.getcwd(), 'ffmpeg') # Download URL from clipboard sys.path.append("youtube-dl") import youtube_dl ydl_opts = { 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', 'outtmpl': os.path.join(os.environ['EXTRA_PATH'], '%(title)s.%(ext)s'), 'logger': MyLogger(), 'progress_hooks': [my_hook] } try: with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) except Exception as e: print(traceback.format_exc()) dz.error("Video Download Failed", "Downloading video failed with the error:\n\n" + e.message) dz.finish("Video Download Complete") dz.url(False)
def reporthook(blocknum, blocksize, totalsize): percent = int(blocknum * blocksize * 1e2 / totalsize) if percent > 0 and percent <= 100: utils.set_determinate_progress(True) utils.set_progress_percent(percent)
def update_youtubedl(): current_version = get_yt_downloader_version() VERSION_JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest' python_version = sys.version_info[0] # Check if there is a new version try: if python_version == 3: versions_result = urllib.request.urlopen( VERSION_JSON_URL).read().decode('utf-8').strip() else: versions_result = urllib2.urlopen(VERSION_JSON_URL).read().decode( 'utf-8').strip() versions_info = json.loads(versions_result) except Exception: print(traceback.format_exc()) print( 'ERROR: Could not download latest version info. Please try again later.' ) return version_id = versions_info['tag_name'] print(f'Latest version: {version_id}, Current version: {current_version}') if version_tuple(current_version) >= version_tuple(version_id): print(f'yt-dlp is up to date ({current_version})') return version_download_url = (get_version(versions_info))['browser_download_url'] print('Updating to version ' + version_id + ' ...') filename = "update.tar.gz" dz.begin("Updating YouTube downloader...") utils.set_determinate_progress(False) try: os.remove(filename) except OSError: pass try: shutil.rmtree('yt-tmp') except Exception: pass try: def reporthook(blocknum, blocksize, totalsize): percent = int(blocknum * blocksize * 1e2 / totalsize) if percent > 0 and percent <= 100: utils.set_determinate_progress(True) utils.set_progress_percent(percent) if python_version == 3: urlretrieve(version_download_url, filename, reporthook) else: urllib.urlretrieve(version_download_url, filename, reporthook) except Exception: print(traceback.format_exc()) print('ERROR: unable to download latest version') return with open(filename, 'rb') as updatefile: newcontent = updatefile.read() newcontent_hash = hashlib.sha256(newcontent).hexdigest() if newcontent_hash != get_sha256sum(versions_info): print('ERROR: the downloaded file hash does not match. Aborting.') return os.mkdir('yt-dlp-tmp') tar = tarfile.open(filename) tar.extractall("./yt-dlp-tmp") tar.close() # Sanity check - does downloaded yt-dlp contain an __init.py__ check_path = 'yt-dlp-tmp/yt-dlp/yt_dlp/__init__.py' if os.path.exists(check_path): print('Extracted update looks good.') else: print(check_path + ' could not be found. Aborting update.') return # Delete existing library and move new one into place shutil.rmtree('yt-dlp') shutil.move('yt-dlp-tmp/yt-dlp', 'yt-dlp') shutil.rmtree('yt-dlp-tmp') os.remove('update.tar.gz') # Get new version from youtube_dl, we can't reload all youtube_dl dependencies so we will do actual download in another python instance old_version = current_version new_version = get_yt_downloader_version() print('Successfully updated yt-dlp library version from ' + old_version + ' to ' + new_version)
def update_youtubedl(): current_version = get_yt_downloader_version() UPDATE_URL = 'https://rg3.github.io/youtube-dl/update/' VERSION_URL = UPDATE_URL + 'LATEST_VERSION' JSON_URL = UPDATE_URL + 'versions.json' # Check if there is a new version try: newversion = urllib2.urlopen(VERSION_URL).read().decode( 'utf-8').strip() except Exception: print(traceback.format_exc()) print( 'ERROR: can\'t find the current version. Please try again later.') return if newversion == current_version: print('youtube-dl is up-to-date (v' + current_version + ')') return try: versions_info = urllib2.urlopen(JSON_URL).read().decode('utf-8') versions_info = json.loads(versions_info) except Exception: print(traceback.format_exc()) print( 'ERROR: Could not download versions.json. Please try again later.') return print('Attempting to update youtube-dl') version_id = versions_info['latest'] print('Updating to version ' + version_id + ' ...') version = versions_info['versions'][version_id] filename = "update.tar.gz" dz.begin("Updating YouTube downloader...") utils.set_determinate_progress(False) try: os.remove(filename) except OSError: pass try: shutil.rmtree('yt-tmp') except Exception: pass try: def reporthook(blocknum, blocksize, totalsize): percent = int(blocknum * blocksize * 1e2 / totalsize) if percent > 0 and percent <= 100: utils.set_determinate_progress(True) utils.set_progress_percent(percent) urllib.urlretrieve(version['tar'][0], filename, reporthook) except Exception: print(traceback.format_exc()) print('ERROR: unable to download latest version') return with open(filename, 'rb') as updatefile: newcontent = updatefile.read() newcontent_hash = hashlib.sha256(newcontent).hexdigest() if newcontent_hash != version['tar'][1]: print('ERROR: the downloaded file hash does not match. Aborting.') return os.mkdir('yt-tmp') tar = tarfile.open(filename) tar.extractall("./yt-tmp") tar.close() # Sanity check - does downloaded youtube-dl contain an __init.py__ check_path = 'yt-tmp/youtube-dl/youtube_dl/__init__.py' if os.path.exists(check_path): print('Extracted update looks good.') else: print(check_path + ' could not be found. Aborting update.') return # Delete existing library and move new one into place shutil.rmtree('youtube-dl') shutil.move('yt-tmp/youtube-dl', 'youtube-dl') shutil.rmtree('yt-tmp') os.remove('update.tar.gz') # Get new version from youtube_dl, we can't reload all youtube_dl dependencies so we will do actual download in another python instance old_version = current_version new_version = get_yt_downloader_version() print('Successfully updated youtube-dl library version from ' + old_version + ' to ' + new_version)
def reporthook(blocknum, blocksize, totalsize): percent = int(blocknum * blocksize * 1e2 / totalsize) if percent > 0 and percent <= 100: utils.set_determinate_progress(True) utils.set_progress_percent(percent)
def update_youtubedl(): current_version = get_yt_downloader_version() UPDATE_URL = 'https://rg3.github.io/youtube-dl/update/' VERSION_URL = UPDATE_URL + 'LATEST_VERSION' JSON_URL = UPDATE_URL + 'versions.json' # Check if there is a new version try: newversion = urllib2.urlopen(VERSION_URL).read().decode('utf-8').strip() except Exception: print(traceback.format_exc()) print('ERROR: can\'t find the current version. Please try again later.') return if newversion == current_version: print('youtube-dl is up-to-date (v' + current_version + ')') return try: versions_info = urllib2.urlopen(JSON_URL).read().decode('utf-8') versions_info = json.loads(versions_info) except Exception: print(traceback.format_exc()) print('ERROR: Could not download versions.json. Please try again later.') return print('Attempting to update youtube-dl') version_id = versions_info['latest'] print('Updating to version ' + version_id + ' ...') version = versions_info['versions'][version_id] filename = "update.tar.gz" dz.begin("Updating YouTube downloader...") utils.set_determinate_progress(False) try: os.remove(filename) except OSError: pass try: shutil.rmtree('yt-tmp') except Exception: pass try: def reporthook(blocknum, blocksize, totalsize): percent = int(blocknum * blocksize * 1e2 / totalsize) if percent > 0 and percent <= 100: utils.set_determinate_progress(True) utils.set_progress_percent(percent) urllib.urlretrieve(version['tar'][0], filename, reporthook) except Exception: print(traceback.format_exc()) print('ERROR: unable to download latest version') return with open(filename, 'rb') as updatefile: newcontent = updatefile.read() newcontent_hash = hashlib.sha256(newcontent).hexdigest() if newcontent_hash != version['tar'][1]: print('ERROR: the downloaded file hash does not match. Aborting.') return os.mkdir('yt-tmp'); tar = tarfile.open(filename) tar.extractall("./yt-tmp") tar.close() # Sanity check - does downloaded youtube-dl contain an __init.py__ check_path = 'yt-tmp/youtube-dl/youtube_dl/__init__.py' if os.path.exists(check_path): print('Extracted update looks good.') else: print(check_path + ' could not be found. Aborting update.') return # Delete existing library and move new one into place shutil.rmtree('youtube-dl') shutil.move('yt-tmp/youtube-dl', 'youtube-dl') shutil.rmtree('yt-tmp') os.remove('update.tar.gz') # Get new version from youtube_dl, we can't reload all youtube_dl dependencies so we will do actual download in another python instance old_version = current_version new_version = get_yt_downloader_version() print('Successfully updated youtube-dl library version from ' + old_version + ' to ' + new_version)