add IPv6 preference option

This commit is contained in:
BennyThink 2022-03-10 21:46:02 +08:00
parent a215bcf245
commit fcb01bd140
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
3 changed files with 30 additions and 20 deletions

View File

@ -99,7 +99,7 @@ vim env/ytdl.env
you can configure all the following environment variables: you can configure all the following environment variables:
* PYRO_WORKERS: number of workers for pyrogram, default is 100 * 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_ID: **REQUIRED**, get it from https://core.telegram.org/
* APP_HASH: **REQUIRED** * APP_HASH: **REQUIRED**
* TOKEN: **REQUIRED** * TOKEN: **REQUIRED**

View File

@ -6,6 +6,12 @@ services:
env_file: env_file:
- env/ytdl.env - env/ytdl.env
restart: always 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: volumes:
- ./data/instagram.com_cookies.txt:/ytdlbot/ytdlbot/instagram.com_cookies.txt - ./data/instagram.com_cookies.txt:/ytdlbot/ytdlbot/instagram.com_cookies.txt
# network_mode: "host"
# deploy:
# resources:
# limits:
# cpus: '0.3'
# memory: 1500M

View File

@ -197,27 +197,31 @@ def ytdl_download(url, tempdir, bm) -> dict:
formats = [ formats = [
"bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio",
"bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best", "bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best",
"" None
] ]
adjust_formats(chat_id, url, formats) adjust_formats(chat_id, url, formats)
add_instagram_cookies(url, ydl_opts) 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: address = ["::", "0.0.0.0"] if os.getenv("ipv6") else [None]
logging.error("Download failed for %s ", url)
response["status"] = False for format_ in formats:
response["error"] = str(e) ydl_opts["format"] = format_
except Exception as e: for addr in address:
logging.error("UNKNOWN EXCEPTION: %s", e) # 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) logging.info("%s - %s", url, response)
if response["status"] is False: if response["status"] is False: