music.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from __future__ import unicode_literals
  2. from urllib.parse import urlparse
  3. import youtube_dl
  4. import aiohttp
  5. import aiofiles
  6. import os
  7. from random import randint
  8. from hurry.filesize import size as format_size
  9. from pyrogram import filters
  10. from wbb import app, SUDOERS, arq, MAIN_CHATS
  11. from wbb.core.decorators.errors import capture_err
  12. from wbb.utils.pastebin import paste
  13. from wbb.utils.functions import file_size_from_url
  14. __MODULE__ = "Music"
  15. __HELP__ = """/ytmusic [link] To Download Music From Various Websites Including Youtube.
  16. /saavn [query] To Download Music From Saavn.
  17. /deezer [query] To Download Music From Deezer.
  18. /lyrics [query] To Get Lyrics Of A Song."""
  19. ydl_opts = {
  20. 'format': 'bestaudio/best',
  21. 'writethumbnail': True,
  22. 'postprocessors': [{
  23. 'key': 'FFmpegExtractAudio',
  24. 'preferredcodec': 'mp3',
  25. 'preferredquality': '192'
  26. }]
  27. }
  28. # Ytmusic
  29. @app.on_message(filters.command("ytmusic") & ~filters.edited & filters.user(SUDOERS))
  30. @capture_err
  31. async def music(_, message):
  32. if len(message.command) != 2:
  33. await message.reply_text("/ytmusic needs a link as argument")
  34. return
  35. link = message.text.split(None, 1)[1]
  36. m = await message.reply_text(f"Downloading {link}",
  37. disable_web_page_preview=True)
  38. try:
  39. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  40. info_dict = ydl.extract_info(link, download=False)
  41. audio_file = ydl.prepare_filename(info_dict)
  42. ydl.process_info(info_dict)
  43. # .webm -> .weba
  44. basename = audio_file.rsplit(".", 1)[-2]
  45. thumbnail_url = info_dict['thumbnail']
  46. thumbnail_file = basename + "." + \
  47. get_file_extension_from_url(thumbnail_url)
  48. audio_file = basename + ".mp3"
  49. except Exception as e:
  50. await m.edit(str(e))
  51. return
  52. # info
  53. title = info_dict['title']
  54. performer = info_dict['uploader']
  55. duration = int(float(info_dict['duration']))
  56. await m.delete()
  57. await message.reply_chat_action("upload_document")
  58. await message.reply_audio(audio_file, duration=duration, performer=performer,
  59. title=title, thumb=thumbnail_file)
  60. os.remove(audio_file)
  61. os.remove(thumbnail_file)
  62. def get_file_extension_from_url(url):
  63. url_path = urlparse(url).path
  64. basename = os.path.basename(url_path)
  65. return basename.split(".")[-1]
  66. # Funtion To Download Song
  67. async def download_song(url):
  68. song_name = f"{randint(6969, 6999)}.mp3"
  69. async with aiohttp.ClientSession() as session:
  70. async with session.get(url) as resp:
  71. if resp.status == 200:
  72. f = await aiofiles.open(song_name, mode='wb')
  73. await f.write(await resp.read())
  74. await f.close()
  75. return song_name
  76. is_downloading = False
  77. # Jiosaavn Music
  78. @app.on_message(filters.command("saavn") & ~filters.edited)
  79. @capture_err
  80. async def jssong(_, message):
  81. global is_downloading
  82. if len(message.command) < 2:
  83. await message.reply_text("/saavn requires an argument.")
  84. return
  85. if is_downloading:
  86. await message.reply_text("Another download is in progress, try again after sometime.")
  87. return
  88. is_downloading = True
  89. text = message.text.split(None, 1)[1]
  90. query = text.replace(" ", "%20")
  91. m = await message.reply_text("Searching...")
  92. try:
  93. songs = await arq.saavn(query)
  94. sname = songs[0].song
  95. slink = songs[0].media_url
  96. ssingers = songs[0].singers
  97. await m.edit("Downloading")
  98. song = await download_song(slink)
  99. await m.edit("Uploading")
  100. await message.reply_audio(
  101. audio=song,
  102. title=sname,
  103. caption=f"「 `{format_size(await file_size_from_url(slink))}` 」",
  104. performer=ssingers,
  105. duration=int(songs[0].duration)
  106. )
  107. os.remove(song)
  108. await m.delete()
  109. except Exception as e:
  110. is_downloading = False
  111. await m.edit(str(e))
  112. return
  113. is_downloading = False
  114. # Deezer Music
  115. @app.on_message(filters.command("deezer") & ~filters.edited)
  116. @capture_err
  117. async def deezsong(_, message):
  118. global is_downloading
  119. if len(message.command) < 2:
  120. await message.reply_text("/deezer requires an argument.")
  121. return
  122. if is_downloading:
  123. await message.reply_text("Another download is in progress, try again after sometime.")
  124. return
  125. is_downloading = True
  126. text = message.text.split(None, 1)[1]
  127. query = text.replace(" ", "%20")
  128. m = await message.reply_text("Searching...")
  129. try:
  130. songs = await arq.deezer(query, 1)
  131. title = songs[0].title
  132. url = songs[0].url
  133. artist = songs[0].artist
  134. await m.edit("Downloading")
  135. song = await download_song(url)
  136. await m.edit("Uploading")
  137. await message.reply_audio(
  138. audio=song,
  139. title=title,
  140. performer=artist,
  141. caption=f"「 `{format_size(await file_size_from_url(url))}` 」")
  142. os.remove(song)
  143. await m.delete()
  144. except Exception as e:
  145. is_downloading = False
  146. await m.edit(str(e))
  147. return
  148. is_downloading = False
  149. # Lyrics
  150. @app.on_message(filters.command("lyrics"))
  151. async def lyrics_func(_, message):
  152. if len(message.command) < 2:
  153. await message.reply_text("**Usage:**\n/lyrics [QUERY]")
  154. return
  155. m = await message.reply_text("**Searching**")
  156. query = message.text.strip().split(None, 1)[1]
  157. song = await arq.lyrics(query)
  158. lyrics = song.lyrics
  159. if len(lyrics) < 4095:
  160. await m.edit(f"__{lyrics}__")
  161. return
  162. lyrics = await paste(lyrics)
  163. await m.edit(f"**LYRICS_TOO_LONG:** [URL]({lyrics})")