music.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from __future__ import unicode_literals
  2. from urllib.parse import urlparse
  3. import os
  4. import youtube_dl
  5. import aiohttp
  6. import aiofiles
  7. import os
  8. from random import randint
  9. from pyrogram import filters
  10. from wbb import app, SUDOERS, ARQ
  11. from wbb.utils.fetch import fetch
  12. from wbb.utils.errors import capture_err
  13. __MODULE__ = "Music"
  14. __HELP__ = """/ytmusic [link] To Download Music From Various Websites Including Youtube
  15. /saavn [query] To Download Music From Saavn."""
  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 + "." + 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. webpage_url = info_dict['webpage_url']
  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"))
  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. r = await fetch(f"{ARQ}saavn?query={query}")
  85. except Exception as e:
  86. await m.edit(str(e))
  87. return
  88. sname = r[0]['song']
  89. slink = r[0]['media_url']
  90. ssingers = r[0]['singers']
  91. await m.edit("Downloading")
  92. song = await download_song(slink)
  93. await m.edit("Uploading")
  94. await message.reply_audio(audio=song, title=sname,
  95. performer=ssingers)
  96. os.remove(song)
  97. await m.delete()