music.py 4.6 KB

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