Automatic retry of the yt-dlp download

This commit is contained in:
Peter 2024-10-18 22:12:18 +03:00
parent 4c61cf7191
commit 74f4ff8c40

16
main.py
View file

@ -75,6 +75,8 @@ def update_proxies():
def run_yt_dlp():
proxy_url = ""
error = True
while error:
with open(os.path.abspath(__file__).split("main.py")[0]+"proxy.json", "r") as f:
proxy_dict = random.choice(json.load(f))
print(f"Using proxy based in {proxy_dict['city']}, {proxy_dict['country']}")
@ -82,10 +84,18 @@ def run_yt_dlp():
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)
subprocess.run(f"yt-dlp --color always --proxy '{proxy_url}' {" ".join([str(arg) for arg in sys.argv])} 2>&1 | tee tempout", shell=True)
with open("tempout", 'r') as log_fl:
if 'Sign in to' in log_fl.readlines()[-1]:
error = True
print("Got 'Sign in to confirm' error. Trying again with another proxy...")
else:
error = False
os.remove("tempout")
def main():
try:
if "update" in sys.argv:
update_proxies()
elif len(sys.argv) == 1:
@ -93,5 +103,7 @@ def main():
else:
sys.argv.pop(0)
run_yt_dlp()
except KeyboardInterrupt:
print("Canceled by user")
main()