music.py 5.0 KB

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