music.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # Jiosaavn Music
  74. @app.on_message(filters.command("saavn") & ~filters.edited)
  75. @capture_err
  76. async def jssong(_, message):
  77. if len(message.command) < 2:
  78. await message.reply_text("/saavn requires an argument.")
  79. return
  80. text = message.text.split(None, 1)[1]
  81. query = text.replace(" ", "%20")
  82. m = await message.reply_text("Searching...")
  83. try:
  84. songs = await arq.saavn(query)
  85. sname = songs[0].song
  86. slink = songs[0].media_url
  87. ssingers = songs[0].singers
  88. duration = int(songs[0].duration)
  89. duration = convert_seconds_to_minutes(duration)
  90. await m.edit("Downloading")
  91. song = await download_song(slink)
  92. await m.edit("Uploading")
  93. await message.reply_audio(audio=song, title=sname,
  94. performer=ssingers, duration=duration)
  95. os.remove(song)
  96. await m.delete()
  97. except Exception as e:
  98. await m.edit(str(e))
  99. return
  100. # Deezer Music
  101. @app.on_message(filters.command("deezer") & ~filters.edited)
  102. @capture_err
  103. async def deezsong(_, message):
  104. if len(message.command) < 2:
  105. await message.reply_text("/deezer requires an argument.")
  106. return
  107. text = message.text.split(None, 1)[1]
  108. query = text.replace(" ", "%20")
  109. m = await message.reply_text("Searching...")
  110. try:
  111. songs = await arq.deezer(query, 1)
  112. title = songs[0].title
  113. url = songs[0].url
  114. artist = songs[0].artist
  115. duration = int(songs[0].duration)
  116. duration = convert_seconds_to_minutes(duration)
  117. await m.edit("Downloading")
  118. song = await download_song(url)
  119. await m.edit("Uploading")
  120. await message.reply_audio(audio=song, title=title,
  121. performer=artist, duration=duration)
  122. os.remove(song)
  123. await m.delete()
  124. except Exception as e:
  125. await m.edit(str(e))
  126. return