Ufoptg's picture
Upload 93 files
78b07ad verified
raw
history blame
6.74 kB
import os
import time
import requests
from lyricsgenius import Genius
from pyrogram import Client
from pyrogram.errors import MessageTooLong
from pyrogram.types import Message
from yt_dlp import YoutubeDL
from Hellbot.core import ENV
from Hellbot.functions.driver import YoutubeDriver
from Hellbot.functions.paste import post_to_telegraph
from Hellbot.functions.tools import progress
from . import HelpMenu, Symbols, db, hellbot, on_message
@on_message("song", allow_stan=True)
async def dwlSong(_, message: Message):
if len(message.command) < 2:
return await hellbot.delete(message, "Provide a song name to download.")
query = await hellbot.input(message)
hell = await hellbot.edit(message, f"πŸ”Ž __π–£π—ˆπ—π—‡π—…π—ˆπ–Ίπ–½π—‚π—‡π—€ π–²π—ˆπ—‡π—€__ `{query}`...")
ytSearch = YoutubeDriver(query, 1).to_dict()[0]
upload_text = f"**⬆️ π–΄π—‰π—…π—ˆπ–Ίπ–½π—‚π—‡π—€ π–²π—ˆπ—‡π—€ ...** \n\n**{Symbols.anchor} 𝖳𝗂𝗍𝗅𝖾:** `{ytSearch['title'][:50]}`\n**{Symbols.anchor} 𝖒𝗁𝖺𝗇𝗇𝖾𝗅:** `{ytSearch['channel']}`"
try:
url = f"https://www.youtube.com{ytSearch['url_suffix']}"
with YoutubeDL(YoutubeDriver.song_options()) as ytdl:
yt_data = ytdl.extract_info(url, False)
yt_file = ytdl.prepare_filename(yt_data)
ytdl.process_info(yt_data)
await hell.edit(upload_text)
resp = requests.get(ytSearch["thumbnail"])
with open(f"{yt_file}.jpg", "wb") as thumbnail:
thumbnail.write(resp.content)
start_time = time.time()
await message.reply_audio(
f"{yt_file}.mp3",
caption=f"**🎧 𝖳𝗂𝗍𝗅𝖾:** {ytSearch['title']} \n\n**πŸ‘€ π–΅π—‚π–Ύπ—π—Œ:** `{ytSearch['views']}` \n**βŒ› π–£π—Žπ—‹π–Ίπ—π—‚π—ˆπ—‡:** `{ytSearch['duration']}`",
duration=int(yt_data["duration"]),
performer="[Ρ‚Π½Ρ” Π½Ρ”β„“β„“Π²ΟƒΡ‚]",
title=ytSearch["title"],
thumb=f"{yt_file}.jpg",
progress=progress,
progress_args=(
hell,
start_time,
upload_text,
),
)
await hell.delete()
except Exception as e:
return await hellbot.delete(hell, f"**πŸ€ π–²π—ˆπ—‡π—€ π–­π—ˆπ— π–£π—ˆπ—π—‡π—…π—ˆπ–Ίπ–½π–Ύπ–½:** `{e}`")
try:
os.remove(f"{yt_file}.mp3")
os.remove(f"{yt_file}.jpg")
except:
pass
@on_message("video", allow_stan=True)
async def dwlSong(_, message: Message):
if len(message.command) < 2:
return await hellbot.delete(message, "Provide a song name to download.")
query = await hellbot.input(message)
hell = await hellbot.edit(message, f"πŸ”Ž __π–£π—ˆπ—π—‡π—…π—ˆπ–Ίπ–½π—‚π—‡π—€ π–΅π—‚π–½π–Ύπ—ˆ π–²π—ˆπ—‡π—€__ `{query}`...")
ytSearch = YoutubeDriver(query, 1).to_dict()[0]
upload_text = f"**⬆️ π–΄π—‰π—…π—ˆπ–Ίπ–½π—‚π—‡π—€ π–΅π—‚π–½π–Ύπ—ˆ π–²π—ˆπ—‡π—€ ...** \n\n**{Symbols.anchor} 𝖳𝗂𝗍𝗅𝖾:** `{ytSearch['title'][:50]}`\n**{Symbols.anchor} 𝖒𝗁𝖺𝗇𝗇𝖾𝗅:** `{ytSearch['channel']}`"
try:
url = f"https://www.youtube.com{ytSearch['url_suffix']}"
with YoutubeDL(YoutubeDriver.video_options()) as ytdl:
yt_data = ytdl.extract_info(url, True)
yt_file = yt_data["id"]
await hell.edit(upload_text)
resp = requests.get(ytSearch["thumbnail"])
with open(f"{yt_file}.jpg", "wb") as thumbnail:
thumbnail.write(resp.content)
start_time = time.time()
await message.reply_video(
f"{yt_file}.mp4",
caption=f"**🎧 𝖳𝗂𝗍𝗅𝖾:** {ytSearch['title']} \n\n**πŸ‘€ π–΅π—‚π–Ύπ—π—Œ:** `{ytSearch['views']}` \n**βŒ› π–£π—Žπ—‹π–Ίπ—π—‚π—ˆπ—‡:** `{ytSearch['duration']}`",
duration=int(yt_data["duration"]),
thumb=f"{yt_file}.jpg",
progress=progress,
progress_args=(
hell,
start_time,
upload_text,
),
)
await hell.delete()
except Exception as e:
return await hellbot.delete(hell, f"**πŸ€ π–΅π—‚π–½π–Ύπ—ˆ π–²π—ˆπ—‡π—€ π–­π—ˆπ— π–£π—ˆπ—π—‡π—…π—ˆπ–Ίπ–½π–Ύπ–½:** `{e}`")
try:
os.remove(f"{yt_file}.mp4")
os.remove(f"{yt_file}.jpg")
except:
pass
@on_message("lyrics", allow_stan=True)
async def getlyrics(_, message: Message):
if len(message.command) < 2:
return await hellbot.delete(message, "Provide a song name to fetch lyrics.")
api = await db.get_env(ENV.lyrics_api)
if not api:
return await hellbot.delete(message, "Lyrics API not found.")
query = await hellbot.input(message)
if "-" in query:
artist, song = query.split("-")
else:
artist, song = "", query
hell = await hellbot.edit(message, f"πŸ”Ž __π–«π—’π—‹π—‚π–Όπ—Œ π–²π—ˆπ—‡π—€__ `{query}`...")
genius = Genius(
api,
verbose=False,
remove_section_headers=True,
skip_non_songs=True,
excluded_terms=["(Remix)", "(Live)"],
)
song = genius.search_song(song, artist)
if not song:
return await hellbot.delete(hell, "No results found.")
title = song.full_title
image = song.song_art_image_url
artist = song.artist
lyrics = song.lyrics
outStr = f"<b>{Symbols.anchor} Title:</b> <code>{title}</code>\n<b>{Symbols.anchor} Artist:</b> <code>{artist}</code>\n\n<code>{lyrics}</code>"
try:
await hell.edit(outStr, disable_web_page_preview=True)
except MessageTooLong:
content = f"<img src='{image}'/>\n\n{outStr}"
url = post_to_telegraph(title, content)
await hell.edit(
f"**{Symbols.anchor} Title:** `{title}`\n**{Symbols.anchor} Artist:** `{artist}`\n\n**{Symbols.anchor} Lyrics:** [Click Here]({url})",
disable_web_page_preview=True,
)
HelpMenu("songs").add(
"song",
"<song name>",
"Download the given audio song from Youtube!",
"song believer",
).add(
"video",
"<song name>",
"Download the given video song from Youtube!",
"song believer",
).add(
"lyrics",
"<song name>",
"Get the lyrics of the given song! Give artist name after - to get accurate results.",
"lyrics believer - imagine dragons",
"Need to setup Lyrics Api key from https://genius.com/developers",
).info(
"Song and Lyrics"
).done()