Merge pull request #3 from Petrprogs/randomize_proxies_fix

Fixes Randomise the used proxy #2
This commit is contained in:
Peter 2024-10-18 11:56:21 +03:00 committed by GitHub
commit 4c61cf7191
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 25 deletions

29
main.py
View file

@ -1,4 +1,5 @@
import requests
import random
import os
import io
import time
@ -6,9 +7,11 @@ import sys
import subprocess
import json
PROXIES_LIST = "https://nnp.nnchan.ru/mahoproxy.php?u=https://api.sandvpn.com/fetch-free-proxys"
SPEEDTEST_URL = "http://212.183.159.230/5MB.zip"
def get_proxies():
request = requests.get("https://nnp.nnchan.ru/mahoproxy.php?u=https://api.sandvpn.com/fetch-free-proxys")
print(request.text)
request = requests.get(PROXIES_LIST)
if request.status_code == 200:
return request.json()
else:
@ -17,7 +20,6 @@ def get_proxies():
def get_best_proxy(proxy_json):
proxy_times = []
proxy_json1 = proxy_json
for i, item in enumerate(proxy_json):
if item["host"] != "" and item["country"] != "Russia":
proxy_str = ""
@ -26,12 +28,12 @@ def get_best_proxy(proxy_json):
proxy_str = f'{item["username"]}:{item["password"]}@{item["host"]}:{item["port"]}'
else:
proxy_str = f'{item["host"]}:{item["port"]}'
url = "http://212.183.159.230/5MB.zip"
with io.BytesIO() as f:
start = time.perf_counter()
try:
r = requests.get(url, stream=True, proxies={"http": proxy_str}, timeout=20)
r = requests.get(SPEEDTEST_URL, stream=True, proxies={"http": proxy_str}, timeout=20)
except:
print("Proxy is dead, skiping...")
del proxy_json[i]
continue
total_length = r.headers.get('content-length')
@ -45,6 +47,7 @@ def get_best_proxy(proxy_json):
f.write(chunk)
done = int(30 * dl / int(total_length))
if done > 3 and (dl//(time.perf_counter() - start) / 100000) < 1.0:
print("\nProxy is too slow, skiping...")
start = 500
break
sys.stdout.write("\r[%s%s] %s Mbps" % ('=' * done, ' ' * (30-done), dl//(time.perf_counter() -
@ -52,29 +55,29 @@ def get_best_proxy(proxy_json):
else:
del proxy_json[i]
continue
#print( f"\n10MB = {(time.perf_counter() - start):.2f} seconds")
item.update({"time": round(time.perf_counter() - start, 2)})
proxy_times.append(item)
print("\n")
else:
del proxy_json[i]
print(proxy_times)
min_value = min([item["time"] for item in proxy_times])
print(min_value)
for item in proxy_times:
if item["time"] == min_value:
return item
best_five_proxies = sorted(proxy_times, key=lambda x: x['time'])[:5]
return best_five_proxies
def update_proxies():
proxy_json = get_proxies()
best_proxy_json = get_best_proxy(proxy_json)
with open(os.path.abspath(__file__).split("main.py")[0]+"proxy.json", "w") as f:
f.write(json.dumps(best_proxy_json, indent=4))
print("All done.")
def run_yt_dlp():
proxy_url = ""
with open(os.path.abspath(__file__).split("main.py")[0]+"proxy.json", "r") as f:
proxy_dict = json.load(f)
proxy_dict = random.choice(json.load(f))
print(f"Using proxy based in {proxy_dict['city']}, {proxy_dict['country']}")
if proxy_dict["username"]:
proxy_url = f'{proxy_dict["username"]}:{proxy_dict["password"]}@{proxy_dict["host"]}:{proxy_dict["port"]}'
else:

View file

@ -1,12 +1,62 @@
{
"city": "Chicago 2",
"country": "USA",
"host": "107.152.38.99",
"new": false,
"paid": false,
"password": "L5KT7rSkCrxn8mm4HngTbK8f4k",
"port": "3128",
"premium": true,
"username": "chicago_squid_user",
"time": 30964.36
}
[
{
"city": "Chicago 2",
"country": "USA",
"host": "107.152.38.99",
"new": false,
"paid": false,
"password": "L5KT7rSkCrxn8mm4HngTbK8f4k",
"port": "3128",
"premium": true,
"username": "chicago_squid_user",
"time": 15.72
},
{
"city": "New York 2",
"country": "USA",
"host": "107.152.44.132",
"new": false,
"paid": false,
"password": "Xs6QjgCMeiGsX68KMdjnRJbmLmD",
"port": "3128",
"premium": true,
"username": "nc_squid_user",
"time": 17.03
},
{
"city": "New York 1",
"country": "USA",
"host": "107.152.43.179",
"new": false,
"paid": true,
"password": "pJTg94JCukVjz2b",
"port": "3128",
"premium": true,
"username": "ny_premium",
"time": 20.92
},
{
"city": "Hesse",
"country": "Germany",
"host": "162.19.230.101",
"new": false,
"paid": false,
"password": "enfo8BM3YDbGfyANYJRCKyL7hGrH",
"port": "3128",
"premium": false,
"username": "hesse_squid_user",
"time": 62594.22
},
{
"city": "London",
"country": "UK",
"host": "83.147.17.103",
"new": false,
"paid": false,
"password": "dsRLomjPDynq7mJQmhKX7QJCLid",
"port": "3128",
"premium": false,
"username": "london_squid_user",
"time": 62614.32
}
]