music.py 5.6 KB

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