diff --git a/README b/README new file mode 100644 index 0000000..c25f1c4 --- /dev/null +++ b/README @@ -0,0 +1,8 @@ +yt-dlp-proxy is a script to find the best proxy for yt-dlp to avoid throttling and bans + +How to use: + 1. (Optional, do only if you have problems with next step) Open terminal and run "yt-dlp-proxy update". This will make a speed test for each free proxy and choose the best one. + 2. Use script as you use yt-dlp! Just pass all arguments to yt-dlp-proxy, instead of yt-dlp. For example, "yt-dlp-proxy --format bv[vcodec^=avc!]+ba https://www.youtube.com/watch?v=bQB0_4BG-9F" + 3. After some time proxy may become slow. In this case just run command from 1st step. + +Also you can copy symlink "yt-dlp-proxy_link" to the dir you want for easier access to the script diff --git a/main.py b/main.py new file mode 100644 index 0000000..0772e94 --- /dev/null +++ b/main.py @@ -0,0 +1,94 @@ +import requests +import os +import io +import time +import sys +import subprocess +import json + +def get_proxies(): + request = requests.get("https://nnp.nnchan.ru/mahoproxy.php?u=https://api.sandvpn.com/fetch-free-proxys") + print(request.text) + if request.status_code == 200: + return request.json() + else: + request.raise_for_status() + + +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 = "" + print(f'Testing {item["host"]}:{item["port"]}') + if item["username"]: + 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) + except: + del proxy_json[i] + continue + total_length = r.headers.get('content-length') + dl = 0 + if total_length is None: # no content length header + print("no content") + f.write(r.content) + elif int(total_length) == 5242880: + for chunk in r.iter_content(1024): + dl += len(chunk) + f.write(chunk) + done = int(30 * dl / int(total_length)) + if done > 3 and (dl//(time.perf_counter() - start) / 100000) < 1.0: + start = 500 + break + sys.stdout.write("\r[%s%s] %s Mbps" % ('=' * done, ' ' * (30-done), dl//(time.perf_counter() - + start) / 100000)) + 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) + + 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 + +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)) + +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) + if proxy_dict["username"]: + proxy_url = f'{proxy_dict["username"]}:{proxy_dict["password"]}@{proxy_dict["host"]}:{proxy_dict["port"]}' + else: + proxy_url = f'{proxy_dict["host"]}:{proxy_dict["port"]}' + args = ["/bin/yt-dlp", "--proxy", proxy_url]+[str(arg) for arg in sys.argv] + subprocess.run(args) + +def main(): + if "update" in sys.argv: + update_proxies() + elif len(sys.argv) == 1: + print("usage: main.py update | \n\nScript for starting yt-dlp with best free proxy\n\nCommands:\n update Update best proxy\n") + else: + sys.argv.pop(0) + run_yt_dlp() + +main() diff --git a/proxy.json b/proxy.json new file mode 100644 index 0000000..1654b2a --- /dev/null +++ b/proxy.json @@ -0,0 +1,12 @@ +{ + "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 +} \ No newline at end of file diff --git a/yt-dlp-proxy b/yt-dlp-proxy new file mode 100644 index 0000000..862bd0a --- /dev/null +++ b/yt-dlp-proxy @@ -0,0 +1,2 @@ +#!/bin/bash +python3 "/mnt/sda2/Work/Projects/yt-dlp-proxy/main.py" $@ diff --git a/yt-dlp-proxy_link b/yt-dlp-proxy_link new file mode 100644 index 0000000..862bd0a --- /dev/null +++ b/yt-dlp-proxy_link @@ -0,0 +1,2 @@ +#!/bin/bash +python3 "/mnt/sda2/Work/Projects/yt-dlp-proxy/main.py" $@