Merge pull request #3 from Petrprogs/randomize_proxies_fix
Fixes Randomise the used proxy #2
This commit is contained in:
commit
4c61cf7191
2 changed files with 78 additions and 25 deletions
29
main.py
29
main.py
|
|
@ -1,4 +1,5 @@
|
||||||
import requests
|
import requests
|
||||||
|
import random
|
||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
import time
|
import time
|
||||||
|
|
@ -6,9 +7,11 @@ import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
import json
|
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():
|
def get_proxies():
|
||||||
request = requests.get("https://nnp.nnchan.ru/mahoproxy.php?u=https://api.sandvpn.com/fetch-free-proxys")
|
request = requests.get(PROXIES_LIST)
|
||||||
print(request.text)
|
|
||||||
if request.status_code == 200:
|
if request.status_code == 200:
|
||||||
return request.json()
|
return request.json()
|
||||||
else:
|
else:
|
||||||
|
|
@ -17,7 +20,6 @@ def get_proxies():
|
||||||
|
|
||||||
def get_best_proxy(proxy_json):
|
def get_best_proxy(proxy_json):
|
||||||
proxy_times = []
|
proxy_times = []
|
||||||
proxy_json1 = proxy_json
|
|
||||||
for i, item in enumerate(proxy_json):
|
for i, item in enumerate(proxy_json):
|
||||||
if item["host"] != "" and item["country"] != "Russia":
|
if item["host"] != "" and item["country"] != "Russia":
|
||||||
proxy_str = ""
|
proxy_str = ""
|
||||||
|
|
@ -26,12 +28,12 @@ def get_best_proxy(proxy_json):
|
||||||
proxy_str = f'{item["username"]}:{item["password"]}@{item["host"]}:{item["port"]}'
|
proxy_str = f'{item["username"]}:{item["password"]}@{item["host"]}:{item["port"]}'
|
||||||
else:
|
else:
|
||||||
proxy_str = f'{item["host"]}:{item["port"]}'
|
proxy_str = f'{item["host"]}:{item["port"]}'
|
||||||
url = "http://212.183.159.230/5MB.zip"
|
|
||||||
with io.BytesIO() as f:
|
with io.BytesIO() as f:
|
||||||
start = time.perf_counter()
|
start = time.perf_counter()
|
||||||
try:
|
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:
|
except:
|
||||||
|
print("Proxy is dead, skiping...")
|
||||||
del proxy_json[i]
|
del proxy_json[i]
|
||||||
continue
|
continue
|
||||||
total_length = r.headers.get('content-length')
|
total_length = r.headers.get('content-length')
|
||||||
|
|
@ -45,6 +47,7 @@ def get_best_proxy(proxy_json):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
done = int(30 * dl / int(total_length))
|
done = int(30 * dl / int(total_length))
|
||||||
if done > 3 and (dl//(time.perf_counter() - start) / 100000) < 1.0:
|
if done > 3 and (dl//(time.perf_counter() - start) / 100000) < 1.0:
|
||||||
|
print("\nProxy is too slow, skiping...")
|
||||||
start = 500
|
start = 500
|
||||||
break
|
break
|
||||||
sys.stdout.write("\r[%s%s] %s Mbps" % ('=' * done, ' ' * (30-done), dl//(time.perf_counter() -
|
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:
|
else:
|
||||||
del proxy_json[i]
|
del proxy_json[i]
|
||||||
continue
|
continue
|
||||||
#print( f"\n10MB = {(time.perf_counter() - start):.2f} seconds")
|
|
||||||
item.update({"time": round(time.perf_counter() - start, 2)})
|
item.update({"time": round(time.perf_counter() - start, 2)})
|
||||||
proxy_times.append(item)
|
proxy_times.append(item)
|
||||||
|
print("\n")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
del proxy_json[i]
|
del proxy_json[i]
|
||||||
print(proxy_times)
|
|
||||||
min_value = min([item["time"] for item in proxy_times])
|
best_five_proxies = sorted(proxy_times, key=lambda x: x['time'])[:5]
|
||||||
print(min_value)
|
return best_five_proxies
|
||||||
for item in proxy_times:
|
|
||||||
if item["time"] == min_value:
|
|
||||||
return item
|
|
||||||
|
|
||||||
def update_proxies():
|
def update_proxies():
|
||||||
proxy_json = get_proxies()
|
proxy_json = get_proxies()
|
||||||
best_proxy_json = get_best_proxy(proxy_json)
|
best_proxy_json = get_best_proxy(proxy_json)
|
||||||
with open(os.path.abspath(__file__).split("main.py")[0]+"proxy.json", "w") as f:
|
with open(os.path.abspath(__file__).split("main.py")[0]+"proxy.json", "w") as f:
|
||||||
f.write(json.dumps(best_proxy_json, indent=4))
|
f.write(json.dumps(best_proxy_json, indent=4))
|
||||||
|
print("All done.")
|
||||||
|
|
||||||
|
|
||||||
def run_yt_dlp():
|
def run_yt_dlp():
|
||||||
proxy_url = ""
|
proxy_url = ""
|
||||||
with open(os.path.abspath(__file__).split("main.py")[0]+"proxy.json", "r") as f:
|
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"]:
|
if proxy_dict["username"]:
|
||||||
proxy_url = f'{proxy_dict["username"]}:{proxy_dict["password"]}@{proxy_dict["host"]}:{proxy_dict["port"]}'
|
proxy_url = f'{proxy_dict["username"]}:{proxy_dict["password"]}@{proxy_dict["host"]}:{proxy_dict["port"]}'
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
74
proxy.json
74
proxy.json
|
|
@ -1,12 +1,62 @@
|
||||||
{
|
[
|
||||||
"city": "Chicago 2",
|
{
|
||||||
"country": "USA",
|
"city": "Chicago 2",
|
||||||
"host": "107.152.38.99",
|
"country": "USA",
|
||||||
"new": false,
|
"host": "107.152.38.99",
|
||||||
"paid": false,
|
"new": false,
|
||||||
"password": "L5KT7rSkCrxn8mm4HngTbK8f4k",
|
"paid": false,
|
||||||
"port": "3128",
|
"password": "L5KT7rSkCrxn8mm4HngTbK8f4k",
|
||||||
"premium": true,
|
"port": "3128",
|
||||||
"username": "chicago_squid_user",
|
"premium": true,
|
||||||
"time": 30964.36
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
Loading…
Add table
Reference in a new issue