Jelajahi Sumber

Inline ytmusic

TheHamkerCat 5 tahun lalu
induk
melakukan
fbe2c2fa21

TEMPAT SAMPAH
Video Countdown 27 Digital 10 seconds-4vQ8If7f374.m4a


+ 16 - 0
wbb/modules/inline.py

@@ -370,6 +370,22 @@ async def inline_query_handler(client, query):
                 cache_time=2
             )
 
+        elif text.split()[0] == "ytmusic":
+            if len(text.split()) < 2:
+                await client.answer_inline_query(
+                    query.id,
+                    results=answers,
+                    switch_pm_text='YT Music | ytmusic [url]',
+                    switch_pm_parameter='inline',
+                )
+                return
+            tex = query.query.split(None, 1)[1].strip()
+            answerss = await yt_music_func(answers, tex)
+            await client.answer_inline_query(
+                query.id,
+                results=answerss,
+                cache_time=2
+            )
     except Exception as e:
         e = traceback.format_exc()
         print(e, " InLine")

+ 40 - 34
wbb/modules/music.py

@@ -31,7 +31,7 @@ import ffmpeg
 from random import randint
 from hurry.filesize import size as format_size
 from pyrogram import filters
-from wbb import app, SUDOERS, arq
+from wbb import app, arq
 from wbb.core.decorators.errors import capture_err
 from wbb.utils.pastebin import paste
 from wbb.utils.functions import file_size_from_url
@@ -46,6 +46,30 @@ __HELP__ = """/ytmusic [link] To Download Music From Various Websites Including
 is_downloading = False
 
 
+async def download_youtube_audio(url: str, m = 0):
+    global is_downloading
+    with youtube_dl.YoutubeDL({'format': 'bestaudio'}) as ydl:
+        info_dict = ydl.extract_info(url, download=False)
+        if int(float(info_dict['duration'])) > 600:
+            if m != 0:
+                await m.edit("This music cannot be downloaded as it's too long.")
+            is_downloading = False
+            return
+        ydl.process_info(info_dict)
+        audio_file = ydl.prepare_filename(info_dict)
+        basename = audio_file.rsplit(".", 1)[-2]
+        if info_dict['ext'] == 'webm':
+            audio_file_opus = basename + ".opus"
+            ffmpeg.input(audio_file).output(audio_file_opus,
+                                            codec="copy", loglevel='error').run()
+            os.remove(audio_file)
+            audio_file = audio_file_opus
+        title = info_dict['title']
+        performer = info_dict['uploader']
+        duration = int(float(info_dict['duration']))
+    return [title, performer, duration, audio_file]
+
+
 @app.on_message(filters.command("ytmusic") & ~filters.edited)
 @capture_err
 async def music(_, message):
@@ -53,37 +77,20 @@ async def music(_, message):
     if len(message.command) != 2:
         await message.reply_text("/ytmusic needs a link as argument")
         return
-    link = message.text.split(None, 1)[1]
+    url = message.text.split(None, 1)[1]
     if is_downloading:
         await message.reply_text("Another download is in progress, try again after sometime.")
         return
     is_downloading = True
-    m = await message.reply_text(f"Downloading {link}",
+    m = await message.reply_text(f"Downloading {url}",
                                  disable_web_page_preview=True)
     try:
-        with youtube_dl.YoutubeDL({'format': 'bestaudio'}) as ydl:
-            info_dict = ydl.extract_info(link, download=False)
-            if int(float(info_dict['duration'])) > 600:
-                await m.edit("This music cannot be downloaded as it's too long.")
-                is_downloading = False
-                return
-            ydl.process_info(info_dict)
-            audio_file = ydl.prepare_filename(info_dict)
-            basename = audio_file.rsplit(".", 1)[-2]
-            if info_dict['ext'] == 'webm':
-                audio_file_opus = basename + ".opus"
-                ffmpeg.input(audio_file).output(audio_file_opus, codec="copy").run()
-                os.remove(audio_file)
-                audio_file = audio_file_opus
-            title = info_dict['title']
-            performer = info_dict['uploader']
-            duration = int(float(info_dict['duration']))
+        title, performer, duration, audio_file = await download_youtube_audio(url, message)
     except Exception as e:
         is_downloading = False
         await m.edit(str(e))
         return
-    await message.reply_audio(audio_file, duration=duration, performer=performer,
-                              title=title)
+    await message.reply_audio(audio_file, duration=duration, performer=performer, title=title)
     await m.delete()
     os.remove(audio_file)
     is_downloading = False
@@ -100,7 +107,6 @@ async def download_song(url):
                 await f.close()
     return song_name
 
-
 # Jiosaavn Music
 
 
@@ -127,12 +133,12 @@ async def jssong(_, message):
         song = await download_song(slink)
         await m.edit("Uploading")
         await message.reply_audio(
-                audio=song,
-                title=sname,
-                caption=f"「 `{format_size(await file_size_from_url(slink))}` 」",
-                performer=ssingers,
-                duration=int(songs[0].duration)
-                )
+            audio=song,
+            title=sname,
+            caption=f"「 `{format_size(await file_size_from_url(slink))}` 」",
+            performer=ssingers,
+            duration=int(songs[0].duration)
+        )
         os.remove(song)
         await m.delete()
     except Exception as e:
@@ -167,11 +173,11 @@ async def deezsong(_, message):
         song = await download_song(url)
         await m.edit("Uploading")
         await message.reply_audio(
-                audio=song,
-                title=title,
-                performer=artist,
-                duration=songs[0].duration,
-                caption=f"「 `{format_size(await file_size_from_url(url))}` 」")
+            audio=song,
+            title=title,
+            performer=artist,
+            duration=songs[0].duration,
+            caption=f"「 `{format_size(await file_size_from_url(url))}` 」")
         os.remove(song)
         await m.delete()
     except Exception as e:

+ 17 - 3
wbb/utils/inlinefuncs.py

@@ -44,6 +44,7 @@ from time import time, ctime
 from random import randint
 from wbb.utils.fetch import fetch
 from wbb.modules.userbot import eval_executer_func
+from wbb.modules.music import download_youtube_audio
 from wbb.utils.functions import test_speedtest, get_http_status_code
 from wbb.utils.formatter import convert_seconds_to_minutes as time_convert
 from wbb.utils.pastebin import paste
@@ -51,7 +52,7 @@ from wbb.core.types import InlineQueryResultCachedDocument
 from wbb import (
     arq, app, app2, USERBOT_USERNAME,
     BOT_USERNAME, USERBOT_ID,
-    SUDOERS, USERBOT_NAME
+    SUDOERS, USERBOT_NAME, MESSAGE_DUMP_CHAT
 )
 
 
@@ -61,7 +62,8 @@ async def inline_help_func(__HELP__):
         "alive", "ping", "tr", "ud", "google", "bitly",
         "wall", "yt", "torrent", "lyrics", "wiki",
         "speedtest", "eval", "music", "saavn", "deezer",
-        "gh_repo", "gh_user", "search", "pastebin", "nsfw_scan"
+        "gh_repo", "gh_user", "search", "pastebin",
+        "nsfw_scan", "ytmusic"
     ]
     buttons.add(*[(InlineKeyboardButton(
         text=i, switch_inline_query_current_chat=i)) for i in keywords_list])
@@ -742,7 +744,7 @@ async def pastebin_func(answers, link):
         status_code = await get_http_status_code(preview)
         await asyncio.sleep(0.2)
         i += 1
-    await app.send_photo(USERBOT_ID, preview)  # To Pre-cache the media
+    await app.send_photo(MESSAGE_DUMP_CHAT, preview)  # To Pre-cache the media
     buttons = InlineKeyboard(row_width=1)
     buttons.add(InlineKeyboardButton(text="Paste Link", url=link))
     answers.append(
@@ -825,3 +827,15 @@ async def nsfw_scan_func(answers, url: str):
         )
     )
     return answers
+
+
+async def yt_music_func(answers, url):
+    title, performer, duration, audio_file = await download_youtube_audio(url)
+    m = await app.send_audio(MESSAGE_DUMP_CHAT, audio_file)
+    answers.append(
+        InlineQueryResultCachedDocument(
+            title=title,
+            file_id=m.audio.file_id
+        )
+    )
+    return answers