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.
This commit is contained in:
parent
bf026c64dc
commit
8cab8a9341
4 changed files with 67 additions and 49 deletions
46
main.py
46
main.py
|
|
@ -12,9 +12,13 @@ SPEEDTEST_URL = "http://212.183.159.230/5MB.zip"
|
||||||
|
|
||||||
def fetch_proxies():
|
def fetch_proxies():
|
||||||
"""Fetch the list of proxies from the defined URL."""
|
"""Fetch the list of proxies from the defined URL."""
|
||||||
response = requests.get(PROXIES_LIST_URL)
|
try:
|
||||||
response.raise_for_status()
|
response = requests.get(PROXIES_LIST_URL)
|
||||||
return response.json()
|
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):
|
def is_valid_proxy(proxy):
|
||||||
"""Check if the proxy is valid and not from Russia (youtube is slowed down there)."""
|
"""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()
|
start_time = time.perf_counter()
|
||||||
try:
|
try:
|
||||||
response = requests.get(SPEEDTEST_URL, stream=True, proxies={"http": proxy_str}, timeout=5)
|
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')
|
total_length = response.headers.get('content-length')
|
||||||
if total_length is None or int(total_length) != 5242880:
|
if total_length is None or int(total_length) != 5242880:
|
||||||
|
|
@ -43,23 +47,26 @@ def test_proxy(proxy):
|
||||||
|
|
||||||
with io.BytesIO() as f:
|
with io.BytesIO() as f:
|
||||||
download_time, downloaded_bytes = download_with_progress(response, f, total_length, start_time)
|
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:
|
except requests.RequestException:
|
||||||
print("Proxy is dead, skipping...")
|
print("Proxy is dead, skipping...")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def download_with_progress(response, f, total_length, start_time):
|
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
|
downloaded_bytes = 0
|
||||||
for chunk in response.iter_content(1024):
|
for chunk in response.iter_content(1024):
|
||||||
downloaded_bytes += len(chunk)
|
downloaded_bytes += len(chunk)
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
done = int(30 * downloaded_bytes / int(total_length))
|
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...")
|
print("\nProxy is too slow, skipping...")
|
||||||
return float('inf'), downloaded_bytes
|
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")
|
sys.stdout.write("\n")
|
||||||
return round(time.perf_counter() - start_time, 2), downloaded_bytes
|
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"):
|
def save_proxies_to_file(proxies, filename="proxy.json"):
|
||||||
"""Save the best proxies to a JSON file."""
|
"""Save the best proxies to a JSON file."""
|
||||||
with open(os.path.join(os.path.dirname(__file__), filename), "w") as f:
|
try:
|
||||||
json.dump(proxies, f, indent=4)
|
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():
|
def update_proxies():
|
||||||
"""Update the proxies list and save the best ones."""
|
"""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."""
|
"""Run yt-dlp with a randomly selected proxy."""
|
||||||
while True:
|
while True:
|
||||||
with open("proxy.json", "r") as f:
|
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)
|
proxy_str = construct_proxy_string(proxy)
|
||||||
print(f"Using proxy from {proxy['city']}, {proxy['country']}")
|
print(f"Using proxy from {proxy['city']}, {proxy['country']}")
|
||||||
|
|
||||||
if execute_yt_dlp_command(proxy_str):
|
if execute_yt_dlp_command(proxy_str):
|
||||||
break # Exit loop if command was successful
|
break # Exit loop if command was successful
|
||||||
print("Got 'Sign in to confirm' error. Trying again with another proxy...")
|
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):
|
def execute_yt_dlp_command(proxy_str):
|
||||||
"""Execute the yt-dlp command with the given proxy."""
|
"""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)
|
subprocess.run(command, shell=True)
|
||||||
with open("tempout", 'r') as log_fl:
|
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():
|
def main():
|
||||||
"""Main function to handle script arguments and execute the appropriate command."""
|
"""Main function to handle script arguments and execute the appropriate command."""
|
||||||
|
|
|
||||||
70
proxy.json
70
proxy.json
|
|
@ -1,30 +1,42 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"time": 7.21,
|
"time": 2.14,
|
||||||
"city": "New York 1",
|
"city": "New York 3",
|
||||||
"country": "USA",
|
"country": "USA",
|
||||||
"host": "107.152.43.179",
|
"host": "107.152.43.121",
|
||||||
"new": false,
|
"new": true,
|
||||||
"paid": true,
|
"paid": false,
|
||||||
"password": "pJTg94JCukVjz2b",
|
"password": "j4ho93JmhM4EyF6t4QBSJAoGS8Kr9yB8",
|
||||||
"port": "3128",
|
"port": "3128",
|
||||||
"premium": true,
|
"premium": false,
|
||||||
"username": "ny_premium"
|
"username": "ny_free_2"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"time": 7.99,
|
"time": 2.19,
|
||||||
"city": "Chicago 2",
|
"city": "Hesse",
|
||||||
"country": "USA",
|
"country": "Germany",
|
||||||
"host": "107.152.38.99",
|
"host": "162.19.230.101",
|
||||||
"new": false,
|
"new": false,
|
||||||
"paid": false,
|
"paid": false,
|
||||||
"password": "L5KT7rSkCrxn8mm4HngTbK8f4k",
|
"password": "enfo8BM3YDbGfyANYJRCKyL7hGrH",
|
||||||
"port": "3128",
|
"port": "3128",
|
||||||
"premium": true,
|
"premium": false,
|
||||||
"username": "chicago_squid_user"
|
"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",
|
"city": "New York 2",
|
||||||
"country": "USA",
|
"country": "USA",
|
||||||
"host": "107.152.44.132",
|
"host": "107.152.44.132",
|
||||||
|
|
@ -36,27 +48,15 @@
|
||||||
"username": "nc_squid_user"
|
"username": "nc_squid_user"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"time": 13.09,
|
"time": 4.45,
|
||||||
"city": "Los Angeles",
|
"city": "New York 1",
|
||||||
"country": "USA",
|
"country": "USA",
|
||||||
"host": "107.152.47.150",
|
"host": "107.152.43.179",
|
||||||
"new": false,
|
"new": false,
|
||||||
"paid": false,
|
"paid": true,
|
||||||
"password": "!K3o!nHrs4M5Lxtg",
|
"password": "pJTg94JCukVjz2b",
|
||||||
"port": "3128",
|
"port": "3128",
|
||||||
"premium": false,
|
"premium": true,
|
||||||
"username": "la_premium"
|
"username": "ny_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"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
0
yt-dlp-proxy
Normal file → Executable file
0
yt-dlp-proxy
Normal file → Executable file
0
yt-dlp-proxy_link
Normal file → Executable file
0
yt-dlp-proxy_link
Normal file → Executable file
Loading…
Add table
Reference in a new issue