From 8cab8a9341172e7f0f0f2e9f34f74aef6ac2c7c6 Mon Sep 17 00:00:00 2001 From: 0n1cOn3 <0n1cOn3@gmx.ch> Date: Wed, 6 Nov 2024 09:01:36 +0100 Subject: [PATCH] Summary of Changes: - Enhanced Error Handling: Added error handling in fetch_proxies and save_proxies_to_file. - Improved Progress Function: download_with_progress was refined for clarity and fixed the progress speed display. - Concatenation Fix in execute_yt_dlp_command: Corrected the command string concatenation. - Cleanup: Removed tempout after checking its contents in execute_yt_dlp_command. - Retry Delay in run_yt_dlp: Added a small delay to avoid rapid retries on proxy errors. --- main.py | 46 +++++++++++++++++++++---------- proxy.json | 70 +++++++++++++++++++++++------------------------ yt-dlp-proxy | 0 yt-dlp-proxy_link | 0 4 files changed, 67 insertions(+), 49 deletions(-) mode change 100644 => 100755 yt-dlp-proxy mode change 100644 => 100755 yt-dlp-proxy_link diff --git a/main.py b/main.py index e229a6e..beb0d57 100644 --- a/main.py +++ b/main.py @@ -12,9 +12,13 @@ SPEEDTEST_URL = "http://212.183.159.230/5MB.zip" def fetch_proxies(): """Fetch the list of proxies from the defined URL.""" - response = requests.get(PROXIES_LIST_URL) - response.raise_for_status() - return response.json() + try: + response = requests.get(PROXIES_LIST_URL) + response.raise_for_status() + return response.json() + except (requests.RequestException, json.JSONDecodeError) as e: + print(f"Failed to fetch proxies: {e}") + return [] def is_valid_proxy(proxy): """Check if the proxy is valid and not from Russia (youtube is slowed down there).""" @@ -34,7 +38,7 @@ def test_proxy(proxy): start_time = time.perf_counter() try: response = requests.get(SPEEDTEST_URL, stream=True, proxies={"http": proxy_str}, timeout=5) - response.raise_for_status() # Ensure we raise an error for bad responses + response.raise_for_status() total_length = response.headers.get('content-length') if total_length is None or int(total_length) != 5242880: @@ -43,23 +47,26 @@ def test_proxy(proxy): with io.BytesIO() as f: download_time, downloaded_bytes = download_with_progress(response, f, total_length, start_time) - return {"time": download_time, **proxy} # Include original proxy info + return {"time": download_time, **proxy} except requests.RequestException: print("Proxy is dead, skipping...") return None def download_with_progress(response, f, total_length, start_time): - """Download content from the response with progress tracking.""" + """Download content with progress tracking.""" downloaded_bytes = 0 for chunk in response.iter_content(1024): downloaded_bytes += len(chunk) f.write(chunk) done = int(30 * downloaded_bytes / int(total_length)) - if done > 3 and (downloaded_bytes//(time.perf_counter() - start_time) / 100000) < 1.0: + speed = downloaded_bytes / (time.perf_counter() - start_time) / 100000 + + # Check if download speed is too low and skip if necessary + if done > 3 and speed < 1.0: print("\nProxy is too slow, skipping...") return float('inf'), downloaded_bytes - sys.stdout.write("\r[%s%s] %s Mbps" % ('=' * done, ' ' * (30-done), downloaded_bytes//(time.perf_counter() - - start_time) / 100000)) + + sys.stdout.write(f"\r[{'=' * done}{' ' * (30 - done)}] {speed:.2f} Mbps") sys.stdout.write("\n") return round(time.perf_counter() - start_time, 2), downloaded_bytes @@ -70,8 +77,11 @@ def get_best_proxies(proxies): def save_proxies_to_file(proxies, filename="proxy.json"): """Save the best proxies to a JSON file.""" - with open(os.path.join(os.path.dirname(__file__), filename), "w") as f: - json.dump(proxies, f, indent=4) + try: + with open(os.path.join(os.path.dirname(__file__), filename), "w") as f: + json.dump(proxies, f, indent=4) + except IOError as e: + print(f"Failed to save proxies to file: {e}") def update_proxies(): """Update the proxies list and save the best ones.""" @@ -84,20 +94,28 @@ def run_yt_dlp(): """Run yt-dlp with a randomly selected proxy.""" while True: with open("proxy.json", "r") as f: - proxy = random.choice(json.load(f)) + proxies = json.load(f) + if not proxies: + print("No proxies available. Please run the update command first.") + break + + proxy = random.choice(proxies) proxy_str = construct_proxy_string(proxy) print(f"Using proxy from {proxy['city']}, {proxy['country']}") if execute_yt_dlp_command(proxy_str): break # Exit loop if command was successful print("Got 'Sign in to confirm' error. Trying again with another proxy...") + time.sleep(1) # Small delay before retrying def execute_yt_dlp_command(proxy_str): """Execute the yt-dlp command with the given proxy.""" - command = f"yt-dlp --color always --proxy '{proxy_str}' {" ".join([str(arg) for arg in sys.argv])} 2>&1 | tee tempout" + command = f"yt-dlp --color always --proxy '{proxy_str}' {' '.join([str(arg) for arg in sys.argv])} 2>&1 | tee tempout" subprocess.run(command, shell=True) with open("tempout", 'r') as log_fl: - return 'Sign in to' not in log_fl.read() + result = 'Sign in to' not in log_fl.read() + os.remove("tempout") # Clean up after checking log file + return result def main(): """Main function to handle script arguments and execute the appropriate command.""" diff --git a/proxy.json b/proxy.json index 6cc3a13..ddc524a 100644 --- a/proxy.json +++ b/proxy.json @@ -1,30 +1,42 @@ [ { - "time": 7.21, - "city": "New York 1", + "time": 2.14, + "city": "New York 3", "country": "USA", - "host": "107.152.43.179", - "new": false, - "paid": true, - "password": "pJTg94JCukVjz2b", + "host": "107.152.43.121", + "new": true, + "paid": false, + "password": "j4ho93JmhM4EyF6t4QBSJAoGS8Kr9yB8", "port": "3128", - "premium": true, - "username": "ny_premium" + "premium": false, + "username": "ny_free_2" }, { - "time": 7.99, - "city": "Chicago 2", - "country": "USA", - "host": "107.152.38.99", + "time": 2.19, + "city": "Hesse", + "country": "Germany", + "host": "162.19.230.101", "new": false, "paid": false, - "password": "L5KT7rSkCrxn8mm4HngTbK8f4k", + "password": "enfo8BM3YDbGfyANYJRCKyL7hGrH", "port": "3128", - "premium": true, - "username": "chicago_squid_user" + "premium": false, + "username": "hesse_squid_user" }, { - "time": 12.86, + "time": 2.49, + "city": "London", + "country": "UK", + "host": "83.147.17.103", + "new": false, + "paid": false, + "password": "dsRLomjPDynq7mJQmhKX7QJCLid", + "port": "3128", + "premium": false, + "username": "london_squid_user" + }, + { + "time": 4.03, "city": "New York 2", "country": "USA", "host": "107.152.44.132", @@ -36,27 +48,15 @@ "username": "nc_squid_user" }, { - "time": 13.09, - "city": "Los Angeles", + "time": 4.45, + "city": "New York 1", "country": "USA", - "host": "107.152.47.150", + "host": "107.152.43.179", "new": false, - "paid": false, - "password": "!K3o!nHrs4M5Lxtg", + "paid": true, + "password": "pJTg94JCukVjz2b", "port": "3128", - "premium": false, - "username": "la_premium" - }, - { - "time": Infinity, - "city": "Chicago 3", - "country": "USA", - "host": "107.152.38.47", - "new": false, - "paid": false, - "password": "yirXePSAqCE8jhLg88FBTfCz", - "port": "3128", - "premium": false, - "username": "chicago_squid_user_free" + "premium": true, + "username": "ny_premium" } ] \ No newline at end of file diff --git a/yt-dlp-proxy b/yt-dlp-proxy old mode 100644 new mode 100755 diff --git a/yt-dlp-proxy_link b/yt-dlp-proxy_link old mode 100644 new mode 100755