music.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import datetime
  21. import os
  22. from asyncio import get_running_loop
  23. from functools import partial
  24. from io import BytesIO
  25. from pyrogram import filters
  26. from pytube import YouTube
  27. from requests import get
  28. from wbb import aiohttpsession as session
  29. from wbb import app, arq
  30. from wbb.core.decorators.errors import capture_err
  31. from wbb.utils.pastebin import paste
  32. __MODULE__ = "Music"
  33. __HELP__ = """
  34. /ytmusic [link] To Download Music From Various Websites Including Youtube. [SUDOERS]
  35. /saavn [query] To Download Music From Saavn.
  36. /lyrics [query] To Get Lyrics Of A Song.
  37. """
  38. is_downloading = False
  39. def download_youtube_audio(arq_resp):
  40. r = arq_resp.result[0]
  41. title = r.title
  42. performer = r.channel
  43. m, s = r.duration.split(":")
  44. duration = int(
  45. datetime.timedelta(minutes=int(m), seconds=int(s)).total_seconds()
  46. )
  47. if duration > 1800:
  48. return
  49. thumb = get(r.thumbnails[0]).content
  50. with open("thumbnail.png", "wb") as f:
  51. f.write(thumb)
  52. thumbnail_file = "thumbnail.png"
  53. url = f"https://youtube.com{r.url_suffix}"
  54. yt = YouTube(url)
  55. audio = yt.streams.filter(only_audio=True).get_audio_only()
  56. out_file = audio.download()
  57. base, _ = os.path.splitext(out_file)
  58. audio_file = base + ".mp3"
  59. os.rename(out_file, audio_file)
  60. return [title, performer, duration, audio_file, thumbnail_file]
  61. @app.on_message(filters.command("ytmusic"))
  62. @capture_err
  63. async def music(_, message):
  64. global is_downloading
  65. if len(message.command) < 2:
  66. return await message.reply_text("/ytmusic needs a query as argument")
  67. url = message.text.split(None, 1)[1]
  68. if is_downloading:
  69. return await message.reply_text(
  70. "Another download is in progress, try again after sometime."
  71. )
  72. is_downloading = True
  73. m = await message.reply_text(
  74. f"Downloading {url}", disable_web_page_preview=True
  75. )
  76. try:
  77. loop = get_running_loop()
  78. arq_resp = await arq.youtube(url)
  79. music = await loop.run_in_executor(
  80. None, partial(download_youtube_audio, arq_resp)
  81. )
  82. if not music:
  83. return await message.reply_text("[ERROR]: MUSIC TOO LONG")
  84. (
  85. title,
  86. performer,
  87. duration,
  88. audio_file,
  89. thumbnail_file,
  90. ) = music
  91. except Exception as e:
  92. is_downloading = False
  93. return await m.edit(str(e))
  94. await message.reply_audio(
  95. audio_file,
  96. duration=duration,
  97. performer=performer,
  98. title=title,
  99. thumb=thumbnail_file,
  100. )
  101. await m.delete()
  102. os.remove(audio_file)
  103. os.remove(thumbnail_file)
  104. is_downloading = False
  105. # Funtion To Download Song
  106. async def download_song(url):
  107. async with session.get(url) as resp:
  108. song = await resp.read()
  109. song = BytesIO(song)
  110. song.name = "a.mp3"
  111. return song
  112. # Jiosaavn Music
  113. @app.on_message(filters.command("saavn"))
  114. @capture_err
  115. async def jssong(_, message):
  116. global is_downloading
  117. if len(message.command) < 2:
  118. return await message.reply_text("/saavn requires an argument.")
  119. if is_downloading:
  120. return await message.reply_text(
  121. "Another download is in progress, try again after sometime."
  122. )
  123. is_downloading = True
  124. text = message.text.split(None, 1)[1]
  125. m = await message.reply_text("Searching...")
  126. try:
  127. songs = await arq.saavn(text)
  128. if not songs.ok:
  129. await m.edit(songs.result)
  130. is_downloading = False
  131. return
  132. sname = songs.result[0].song
  133. slink = songs.result[0].media_url
  134. ssingers = songs.result[0].singers
  135. sduration = songs.result[0].duration
  136. await m.edit("Downloading")
  137. song = await download_song(slink)
  138. await m.edit("Uploading")
  139. await message.reply_audio(
  140. audio=song,
  141. title=sname,
  142. performer=ssingers,
  143. duration=sduration,
  144. )
  145. await m.delete()
  146. except Exception as e:
  147. is_downloading = False
  148. return await m.edit(str(e))
  149. is_downloading = False
  150. song.close()
  151. # Lyrics
  152. @app.on_message(filters.command("lyrics"))
  153. async def lyrics_func(_, message):
  154. if len(message.command) < 2:
  155. return await message.reply_text("**Usage:**\n/lyrics [QUERY]")
  156. m = await message.reply_text("**Searching**")
  157. query = message.text.strip().split(None, 1)[1]
  158. resp = await arq.lyrics(query)
  159. if not (resp.ok and resp.result):
  160. return await m.edit("No lyrics found.")
  161. song = resp.result[0]
  162. song_name = song["song"]
  163. artist = song["artist"]
  164. lyrics = song["lyrics"]
  165. msg = f"**{song_name}** | **{artist}**\n\n__{lyrics}__"
  166. if len(msg) > 4095:
  167. msg = await paste(msg)
  168. msg = f"**LYRICS_TOO_LONG:** [URL]({msg})"
  169. return await m.edit(msg)