From fcb01bd140739c0dbf891d4d73e98585749c4ee7 Mon Sep 17 00:00:00 2001 From: BennyThink Date: Thu, 10 Mar 2022 21:46:02 +0800 Subject: [PATCH] add IPv6 preference option --- README.md | 2 +- worker.yml | 10 ++++++++-- ytdlbot/downloader.py | 38 +++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ff58371..2ebe4bb 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ vim env/ytdl.env you can configure all the following environment variables: * PYRO_WORKERS: number of workers for pyrogram, default is 100 -* WORKERS: workers count for celery,it'll be doubled. +* WORKERS: workers count for celery * APP_ID: **REQUIRED**, get it from https://core.telegram.org/ * APP_HASH: **REQUIRED** * TOKEN: **REQUIRED** diff --git a/worker.yml b/worker.yml index b9e418c..30ba3a4 100644 --- a/worker.yml +++ b/worker.yml @@ -6,6 +6,12 @@ services: env_file: - env/ytdl.env restart: always - command: ["/usr/local/bin/supervisord", "-c" ,"/ytdlbot/conf/supervisor_worker.conf"] + command: [ "/usr/local/bin/supervisord", "-c" ,"/ytdlbot/conf/supervisor_worker.conf" ] volumes: - - ./data/instagram.com_cookies.txt:/ytdlbot/ytdlbot/instagram.com_cookies.txt \ No newline at end of file + - ./data/instagram.com_cookies.txt:/ytdlbot/ytdlbot/instagram.com_cookies.txt +# network_mode: "host" +# deploy: +# resources: +# limits: +# cpus: '0.3' +# memory: 1500M \ No newline at end of file diff --git a/ytdlbot/downloader.py b/ytdlbot/downloader.py index 6715f50..fc7801d 100644 --- a/ytdlbot/downloader.py +++ b/ytdlbot/downloader.py @@ -197,27 +197,31 @@ def ytdl_download(url, tempdir, bm) -> dict: formats = [ "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio", "bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best", - "" + None ] adjust_formats(chat_id, url, formats) add_instagram_cookies(url, ydl_opts) - for f in formats: - if f: - ydl_opts["format"] = f - try: - logging.info("Downloading for %s with format %s", url, f) - with ytdl.YoutubeDL(ydl_opts) as ydl: - ydl.download([url]) - response["status"] = True - response["error"] = "" - break - except (ValueError, DownloadError) as e: - logging.error("Download failed for %s ", url) - response["status"] = False - response["error"] = str(e) - except Exception as e: - logging.error("UNKNOWN EXCEPTION: %s", e) + address = ["::", "0.0.0.0"] if os.getenv("ipv6") else [None] + + for format_ in formats: + ydl_opts["format"] = format_ + for addr in address: + # IPv6 goes first in each format + ydl_opts["source_address"] = addr + try: + logging.info("Downloading for %s with format %s", url, format_) + with ytdl.YoutubeDL(ydl_opts) as ydl: + ydl.download([url]) + response["status"] = True + response["error"] = "" + break + except (ValueError, DownloadError) as e: + logging.error("Download failed for %s ", url) + response["status"] = False + response["error"] = str(e) + except Exception as e: + logging.error("UNKNOWN EXCEPTION: %s", e) logging.info("%s - %s", url, response) if response["status"] is False: