Skip to content

Commit

Permalink
Added YouTube search (UsergeTeam#309)
Browse files Browse the repository at this point in the history
* added YouTube search

* added YouTube search here

* typo

* little fix

* sync to async

* Done

* typo

* minor change

* forgot this

* https://t.me/UserGeSpam/268723

* Update voice_call.py
  • Loading branch information
Krishna-Singhal authored Apr 24, 2021
1 parent fb95c4c commit c75d4e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ urbandict==0.5
wget
wikipedia
youtube_dl>=2021.1.8
youtube-search-python
83 changes: 62 additions & 21 deletions userge/plugins/utils/voice_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import asyncio
import youtube_dl as ytdl

from typing import List, Optional
from typing import List, Optional, Tuple
from traceback import format_exc
from pytgcalls import GroupCall
from youtubesearchpython import VideosSearch

from pyrogram.raw import functions
from pyrogram.types import (
InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery, Message as RawMessage
Expand Down Expand Up @@ -92,10 +94,10 @@ async def reply_text(


async def cache_admins(chat_id: int) -> None:
k = []
async for member in userge.iter_chat_members(chat_id):
if member.status in ("creator", "administrator"):
k.append(member.user.id)
k = [
member.user.id async for member in userge.iter_chat_members(chat_id)
if member.status in ("creator", "administrator")
]
ADMINS[chat_id] = k


Expand Down Expand Up @@ -172,26 +174,35 @@ async def play_music(msg: Message):
f"[Song]({msg.input_str}) "
f"Scheduled to QUEUE on #{len(QUEUE)} position."
)
await reply_text(
msg,
text
)
await reply_text(msg, text)
else:
await reply_text(msg, "Only youtube links are supported")
mesg = await reply_text(msg, f"Searching `{msg.input_str}` on YouTube")
title, link = await _get_song(msg.input_str)
if link:
await mesg.delete()
mesg = await reply_text(msg, f"Found [{title}]({link})")
QUEUE.append(mesg)
text = (
"`Playing` "
f"[{title}]({mesg.link})"
) if not PLAYING else (
f"[{title}]({mesg.link}) "
f"Scheduled to QUEUE on #{len(QUEUE)} position."
)
await reply_text(msg, text)
else:
await mesg.edit("No results found.")
elif msg.reply_to_message and msg.reply_to_message.audio:
QUEUE.append(msg)
replied = msg.reply_to_message
text = (
"`Playing` "
f"[{replied.audio.title}]({replied.link})"
) if not PLAYING else (
f"[Song]({msg.reply_to_message.link}) "
f"[{replied.audio.title}]({replied.link}) "
f"Scheduled to QUEUE on #{len(QUEUE)} position."
)
await reply_text(
msg,
text
)
await reply_text(msg, text)
else:
return await reply_text(msg, "Input not found")

Expand All @@ -216,8 +227,14 @@ async def view_queue(msg: Message):
out = f"**{len(QUEUE)} Songs in Queue:**\n"
for i in QUEUE:
replied = i.reply_to_message
out += f" - [{replied.audio.title if replied else i.input_str}]"
out += f"({replied.link if replied else i.link})"
if replied and replied.audio:
out += f"\n - [{replied.audio.title}]"
out += f"({replied.link})"
else:
link = i.input_str
if "Found" in i.text:
link = i.entities[0].url
out += f"\n{link}"

await reply_text(msg, out)

Expand Down Expand Up @@ -351,7 +368,8 @@ async def yt_down(msg: Message):
shutil.rmtree("temp_music_dir", ignore_errors=True)
message = await reply_text(msg, "`Downloading this Song...`")

url = msg.input_str
url = msg.entities[0].url if "Found" in msg.text else msg.input_str

del QUEUE[0]
title, duration = await mp3_down(url.strip())

Expand All @@ -369,10 +387,16 @@ async def yt_down(msg: Message):
call.input_filename = await _transcode(audio_path)
await message.delete()

def requester():
replied = msg.reply_to_message
if msg.client.id == msg.from_user.id and replied:
return replied.from_user.mention
return msg.from_user.mnetion

BACK_BUTTON_TEXT = (
f"🎶 **Now playing:** [{title}]({url})\n"
f"⏳ **Duration:** `{duration}`\n"
f"🎧 **Requested By:** {msg.from_user.mention}"
f"🎧 **Requested By:** {requester()}"
)

CQ_MSG = await reply_text(
Expand All @@ -383,6 +407,9 @@ async def yt_down(msg: Message):
)
shutil.rmtree("temp_music_dir", ignore_errors=True)

if msg.client.id == msg.from_user.id:
await msg.delete()


async def tg_down(msg: Message):
global BACK_BUTTON_TEXT, CQ_MSG # pylint: disable=global-statement
Expand Down Expand Up @@ -413,6 +440,14 @@ async def tg_down(msg: Message):
shutil.rmtree("temp_music_dir", ignore_errors=True)


@pool.run_in_thread
def _get_song(name: str) -> Tuple[str, str]:
results: List[dict] = VideosSearch(name, limit=1).result()['result']
if results:
return results[0].get('title', name), results[0].get('link')
return name, ""


@pool.run_in_thread
def mp3_down(url: str):
ydl_opts = {
Expand Down Expand Up @@ -483,8 +518,14 @@ async def vc_callback(_, cq: CallbackQuery):
out += f"{'s' if len(QUEUE) > 1 else ''} in Queue:**\n"
for i in QUEUE:
replied = i.reply_to_message
out += f"\n - [{replied.audio.title if replied else i.input_str}]"
out += f"({replied.link if replied else i.link})"
if replied and replied.audio:
out += f"\n - [{replied.audio.title}]"
out += f"({replied.link})"
else:
link = i.input_str
if "Found" in i.text:
link = i.entities[0].url
out += f"\n{link}"

out += f"\n\n**Clicked by:** {cq.from_user.mention}"
button = InlineKeyboardMarkup(
Expand Down

0 comments on commit c75d4e4

Please sign in to comment.