Skip to content

Commit

Permalink
Merge branch 'ashwinstr:alpha' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
notashwin authored Dec 11, 2022
2 parents 58128a3 + 7641625 commit aaf3ed7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
File renamed without changes.
63 changes: 28 additions & 35 deletions userge/plugins/misc/utube.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from time import time

import wget
import youtube_dl as ytdl
import yt_dlp as ytdl

from userge import Config, Message, pool, userge
from userge.plugins.misc.uploads import upload
from userge.utils import humanbytes, time_formatter

from .uploads import upload

LOGGER = userge.getLogger(__name__)


Expand Down Expand Up @@ -53,7 +52,7 @@ async def ytinfo(message: Message):
_exracted
)
if _exracted["thumb"]:
_tmp = wget.download(
_tmp = await pool.run_in_thread(wget.download)(
_exracted["thumb"], os.path.join(Config.DOWN_PATH, f"{time()}.jpg")
)
await message.reply_photo(_tmp, caption=out)
Expand All @@ -66,18 +65,18 @@ async def ytinfo(message: Message):
@userge.on_cmd(
"ytdl",
about={
"header": "Download from youtube",
"header": "Download from youtube and upload to Telegram.",
"options": {
"-a": "select the audio u-id",
"-v": "select the video u-id",
"-m": "extract the mp3 in 320kbps",
"-t": "upload to telegram",
"-s": "Download to Bot",
},
"examples": [
"{tr}ytdl link",
"{tr}ytdl -a12 -v120 link",
"{tr}ytdl -m -t link will upload the mp3",
"{tr}ytdl -m -t -d link will upload ",
"{tr}ytdl -m -s link will upload the mp3",
"{tr}ytdl -m -s -d link will upload ",
"the mp3 as a document",
],
},
Expand Down Expand Up @@ -126,50 +125,44 @@ def __progress(data: dict):
userge.loop.create_task(message.edit(out))

await message.edit("Hold on \u23f3 ..")
reply = message.reply_to_message
input_ = reply.text if reply else message.filtered_input_str
for i in input_.split():
if "http" in i:
url = i
if bool(message.flags):
desiredFormat1 = str(message.flags.get("a", ""))
desiredFormat2 = str(message.flags.get("v", ""))
if "m" in message.flags:
retcode = await _mp3Dl([message.filtered_input_str], __progress, startTime)
retcode = await _mp3Dl([url], __progress, startTime)
elif all(k in message.flags for k in ("a", "v")):
# 1st format must contain the video
desiredFormat = "+".join([desiredFormat2, desiredFormat1])
retcode = await _tubeDl(
[message.filtered_input_str], __progress, startTime, desiredFormat
)
retcode = await _tubeDl([url], __progress, startTime, desiredFormat)
elif "a" in message.flags:
desiredFormat = desiredFormat1
retcode = await _tubeDl(
[message.filtered_input_str], __progress, startTime, desiredFormat
)
retcode = await _tubeDl([url], __progress, startTime, desiredFormat)
elif "v" in message.flags:
desiredFormat = desiredFormat2 + "+bestaudio"
retcode = await _tubeDl(
[message.filtered_input_str], __progress, startTime, desiredFormat
)
retcode = await _tubeDl([url], __progress, startTime, desiredFormat)
else:
retcode = await _tubeDl(
[message.filtered_input_str], __progress, startTime, None
)
retcode = await _tubeDl([url], __progress, startTime, None)
else:
retcode = await _tubeDl(
[message.filtered_input_str], __progress, startTime, None
)
retcode = await _tubeDl([url], __progress, startTime, None)
_fpath = ""
if retcode == 0:
_fpath = ""
for _path in glob.glob(os.path.join(Config.DOWN_PATH, str(startTime), "*")):
if not _path.lower().endswith((".jpg", ".png", ".webp")):
_fpath = _path
if not _fpath:
await message.err("nothing found !")
return
await message.edit(
f"**YTDL completed in {round(time() - startTime)} seconds**\n`{_fpath}`"
)
if "t" in message.flags:
await upload(message, Path(_fpath))
else:
if not _fpath:
return await message.err("nothing found !")
await message.edit(
f"**YTDL completed in {round(time() - startTime)} seconds**\n`{_fpath}`"
)
if "s" in message.flags:
await message.edit(str(retcode))
else:
await upload(message, Path(_fpath))


@userge.on_cmd(
Expand Down Expand Up @@ -243,7 +236,7 @@ def _supported(url):


@pool.run_in_thread
def _tubeDl(url: list, starttime, prog, uid=None):
def _tubeDl(url: list, prog, starttime, uid=None):
_opts = {
"outtmpl": os.path.join(
Config.DOWN_PATH, str(starttime), "%(title)s-%(format)s.%(ext)s"
Expand Down

0 comments on commit aaf3ed7

Please sign in to comment.