Skip to content

Commit

Permalink
download done?
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Dec 3, 2024
1 parent 5346fb6 commit 38ed4c4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/engine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def get_cache_fileid(self):
return self._redis.get_send_cache(unique)

@abstractmethod
def _setup_formats(self):
def _setup_formats(self) -> list | None:
pass

@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion src/engine/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DirectDownloader(BaseDownloader):
def start(self):
pass

def _setup_formats(self):
def _setup_formats(self) -> list | None:
pass

def _requests_download(self):
Expand Down
51 changes: 43 additions & 8 deletions src/engine/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

# ytdlbot - generic.py

from pathlib import Path

import yt_dlp
from base import BaseDownloader
from pyrogram import types

Expand All @@ -11,7 +14,7 @@

class YoutubeDownload(BaseDownloader):

def _setup_formats(self):
def _setup_formats(self) -> list | None:
download = get_download_settings(self._user_id)
formats = []
# "high", "medium", "low", "audio", "custom"
Expand All @@ -33,13 +36,21 @@ def _setup_formats(self):
if temp_row:
markup.append(temp_row)
self._bot_msg.__edit_text("Choose the format", reply_markup=types.InlineKeyboardMarkup(markup))
return
return None
if download == "audio":
# download audio only
formats.append("bestaudio")
elif download == "high":
# download high quality video
formats.append("best")
# default config
formats.extend(
[
# webm , vp9 and av01 are not streamable on telegram, so we'll extract only mp4
"bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio",
"bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best",
None,
]
)

elif download == "medium":
# download medium quality video
formats.append("medium")
Expand All @@ -49,10 +60,34 @@ def _setup_formats(self):
return formats

def _download(self, formats):
pass
output = Path(self._tempdir, "%(title).70s.%(ext)s").as_posix()

ydl_opts = {
"progress_hooks": [lambda d: self.download_hook(d)],
"outtmpl": output,
"restrictfilenames": False,
"quiet": True,
}

if self._url.startswith("https://drive.google.com"):
# Always use the `source` format for Google Drive URLs.
formats = ["source"] + formats

files = None
for f in formats:
ydl_opts["format"] = f
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([self._url])
files = list(Path(self._tempdir).glob("*"))
break

return files

def start(self, formats=None):
if formats is None:
formats = self._setup_formats()
self._download(formats)
# user can choose format by clicking on the button(custom config)
default_formats = self._setup_formats()
if formats is not None:
# formats according to user choice
default_formats = formats + self._setup_formats()
self._download(default_formats)
self._upload()
41 changes: 0 additions & 41 deletions src/engine/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,6 @@
from utils import shorten_url, sizeof_fmt


def ytdl_download(url: str, tempdir, bm, formats: list = None) -> list:
output = pathlib.Path(tempdir, "%(title).70s.%(ext)s").as_posix()
default_formats = [
# webm , vp9 and av01 are not streamable on telegram, so we'll extract only mp4
"bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio",
"bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best",
None,
]
if formats:
default_formats = formats + default_formats

ydl_opts = {
"progress_hooks": [lambda d: download_hook(d, bm)],
"outtmpl": output,
"restrictfilenames": False,
"quiet": True,
}
if ENABLE_ARIA2:
ydl_opts["external_downloader"] = "aria2c"
ydl_opts["external_downloader_args"] = [
"--min-split-size=1M",
"--max-connection-per-server=16",
"--max-concurrent-downloads=16",
"--split=16",
]

if url.startswith("https://drive.google.com"):
# Always use the `source` format for Google Drive URLs.
default_formats = ["source"]

video_paths = None
for f in default_formats:
ydl_opts["format"] = f
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
video_paths = list(pathlib.Path(tempdir).glob("*"))
break

return video_paths


def debounce(wait_seconds):
"""
Thread-safe debounce decorator for functions that take a message with chat.id and msg.id attributes.
Expand Down
2 changes: 1 addition & 1 deletion src/engine/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def extract_code(self):

return None

def _setup_formats(self):
def _setup_formats(self) -> list | None:
pass

def _download(self, formats):
Expand Down

0 comments on commit 38ed4c4

Please sign in to comment.