music.py 4.7 KB

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