music.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. MIT License
  3. Copyright (c) 2021 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. from __future__ import unicode_literals
  21. from urllib.parse import urlparse
  22. import youtube_dl
  23. import aiohttp
  24. import aiofiles
  25. import os
  26. import ffmpeg
  27. from random import randint
  28. from hurry.filesize import size as format_size
  29. from pyrogram import filters
  30. from wbb import app, arq
  31. from wbb.core.decorators.errors import capture_err
  32. from wbb.utils.pastebin import paste
  33. from wbb.utils.functions import file_size_from_url
  34. __MODULE__ = "Music"
  35. __HELP__ = """/ytmusic [link] To Download Music From Various Websites Including Youtube.
  36. /saavn [query] To Download Music From Saavn.
  37. /deezer [query] To Download Music From Deezer.
  38. /lyrics [query] To Get Lyrics Of A Song."""
  39. is_downloading = False
  40. async def download_youtube_audio(url: str, m = 0):
  41. global is_downloading
  42. with youtube_dl.YoutubeDL({'format': 'bestaudio'}) as ydl:
  43. info_dict = ydl.extract_info(url, download=False)
  44. if int(float(info_dict['duration'])) > 600:
  45. if m != 0:
  46. await m.edit("This music cannot be downloaded as it's too long.")
  47. is_downloading = False
  48. return
  49. ydl.process_info(info_dict)
  50. audio_file = ydl.prepare_filename(info_dict)
  51. basename = audio_file.rsplit(".", 1)[-2]
  52. if info_dict['ext'] == 'webm':
  53. audio_file_opus = basename + ".opus"
  54. ffmpeg.input(audio_file).output(audio_file_opus,
  55. codec="copy", loglevel='error').overwrite_output().run()
  56. os.remove(audio_file)
  57. audio_file = audio_file_opus
  58. title = info_dict['title']
  59. performer = info_dict['uploader']
  60. duration = int(float(info_dict['duration']))
  61. return [title, performer, duration, audio_file]
  62. @app.on_message(filters.command("ytmusic") & ~filters.edited)
  63. @capture_err
  64. async def music(_, message):
  65. global is_downloading
  66. if len(message.command) != 2:
  67. await message.reply_text("/ytmusic needs a link as argument")
  68. return
  69. url = message.text.split(None, 1)[1]
  70. if is_downloading:
  71. await message.reply_text("Another download is in progress, try again after sometime.")
  72. return
  73. is_downloading = True
  74. m = await message.reply_text(f"Downloading {url}",
  75. disable_web_page_preview=True)
  76. try:
  77. title, performer, duration, audio_file = await download_youtube_audio(url, message)
  78. except Exception as e:
  79. is_downloading = False
  80. await m.edit(str(e))
  81. return
  82. await message.reply_audio(audio_file, duration=duration, performer=performer, title=title)
  83. await m.delete()
  84. os.remove(audio_file)
  85. is_downloading = False
  86. # Funtion To Download Song
  87. async def download_song(url):
  88. song_name = f"{randint(6969, 6999)}.mp3"
  89. async with aiohttp.ClientSession() as session:
  90. async with session.get(url) as resp:
  91. if resp.status == 200:
  92. f = await aiofiles.open(song_name, mode='wb')
  93. await f.write(await resp.read())
  94. await f.close()
  95. return song_name
  96. # Jiosaavn Music
  97. @app.on_message(filters.command("saavn") & ~filters.edited)
  98. @capture_err
  99. async def jssong(_, message):
  100. global is_downloading
  101. if len(message.command) < 2:
  102. await message.reply_text("/saavn requires an argument.")
  103. return
  104. if is_downloading:
  105. await message.reply_text("Another download is in progress, try again after sometime.")
  106. return
  107. is_downloading = True
  108. text = message.text.split(None, 1)[1]
  109. query = text.replace(" ", "%20")
  110. m = await message.reply_text("Searching...")
  111. try:
  112. songs = await arq.saavn(query)
  113. sname = songs[0].song
  114. slink = songs[0].media_url
  115. ssingers = songs[0].singers
  116. await m.edit("Downloading")
  117. song = await download_song(slink)
  118. await m.edit("Uploading")
  119. await message.reply_audio(
  120. audio=song,
  121. title=sname,
  122. caption=f"「 `{format_size(await file_size_from_url(slink))}` 」",
  123. performer=ssingers,
  124. duration=int(songs[0].duration)
  125. )
  126. os.remove(song)
  127. await m.delete()
  128. except Exception as e:
  129. is_downloading = False
  130. await m.edit(str(e))
  131. return
  132. is_downloading = False
  133. # Deezer Music
  134. @app.on_message(filters.command("deezer") & ~filters.edited)
  135. @capture_err
  136. async def deezsong(_, message):
  137. global is_downloading
  138. if len(message.command) < 2:
  139. await message.reply_text("/deezer requires an argument.")
  140. return
  141. if is_downloading:
  142. await message.reply_text("Another download is in progress, try again after sometime.")
  143. return
  144. is_downloading = True
  145. text = message.text.split(None, 1)[1]
  146. query = text.replace(" ", "%20")
  147. m = await message.reply_text("Searching...")
  148. try:
  149. songs = await arq.deezer(query, 1)
  150. title = songs[0].title
  151. url = songs[0].url
  152. artist = songs[0].artist
  153. await m.edit("Downloading")
  154. song = await download_song(url)
  155. await m.edit("Uploading")
  156. await message.reply_audio(
  157. audio=song,
  158. title=title,
  159. performer=artist,
  160. duration=songs[0].duration,
  161. caption=f"「 `{format_size(await file_size_from_url(url))}` 」")
  162. os.remove(song)
  163. await m.delete()
  164. except Exception as e:
  165. is_downloading = False
  166. await m.edit(str(e))
  167. return
  168. is_downloading = False
  169. # Lyrics
  170. @app.on_message(filters.command("lyrics"))
  171. async def lyrics_func(_, message):
  172. if len(message.command) < 2:
  173. await message.reply_text("**Usage:**\n/lyrics [QUERY]")
  174. return
  175. m = await message.reply_text("**Searching**")
  176. query = message.text.strip().split(None, 1)[1]
  177. song = await arq.lyrics(query)
  178. lyrics = song.lyrics
  179. if len(lyrics) < 4095:
  180. await m.edit(f"__{lyrics}__")
  181. return
  182. lyrics = await paste(lyrics)
  183. await m.edit(f"**LYRICS_TOO_LONG:** [URL]({lyrics})")