ytdl.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import traceback
  21. from asyncio import gather
  22. from os import remove
  23. from random import randint
  24. from pykeyboard import InlineKeyboard
  25. from pyrogram import filters
  26. from pyrogram.types import InlineKeyboardButton
  27. from wbb import app, arq
  28. from wbb.core.decorators.errors import capture_err
  29. from wbb.utils.formatter import \
  30. convert_seconds_to_minutes as timeFormat
  31. from wbb.utils.functions import downloader
  32. __MODULE__ = "YoutubeDL"
  33. __HELP__ = "/ytdl [VIDEO_LINK] - Download a youtube video."
  34. VIDEO_DATA = {}
  35. @app.on_message(filters.command("ytdl"))
  36. @capture_err
  37. async def ytdl_func(_, message):
  38. if len(message.command) != 2:
  39. return await message.reply_text(
  40. "**Usage:**/ytdl [VIDEO_LINK]"
  41. )
  42. m = await message.reply_text("Processing")
  43. url = message.text.split(None, 1)[1]
  44. results = await arq.ytdl(url)
  45. if not results.ok:
  46. return await m.edit(results.result)
  47. result = results.result
  48. title = result.title
  49. thumbnail = result.thumbnail
  50. duration = result.duration
  51. video = result.video
  52. buttons = InlineKeyboard(row_width=3)
  53. keyboard = []
  54. for media in video:
  55. quality = media.quality
  56. size = media.size
  57. url = media.url
  58. format = media.format
  59. data = str(randint(999, 9999999))
  60. VIDEO_DATA[data] = {
  61. "url": url,
  62. "title": title,
  63. "quality": quality,
  64. "duration": duration,
  65. "format": format,
  66. "thumbnail": thumbnail,
  67. "cc": message.from_user.mention
  68. if message.from_user
  69. else "Anon",
  70. }
  71. keyboard.append(
  72. InlineKeyboardButton(
  73. text=f"{quality} | {size}",
  74. callback_data=f"ytdl {data}",
  75. )
  76. )
  77. buttons.add(*keyboard)
  78. caption = f"""
  79. **Title:** {title}
  80. **Duration:** {await timeFormat(duration)}
  81. """
  82. await message.reply_photo(
  83. thumbnail, caption=caption, reply_markup=buttons
  84. )
  85. await m.delete()
  86. @app.on_callback_query(filters.regex(r"^ytdl"))
  87. async def ytdlCallback(_, cq):
  88. await cq.message.edit("Downloading")
  89. data_ = cq.data.split()[1]
  90. try:
  91. data = VIDEO_DATA[data_]
  92. url = data["url"]
  93. title = data["title"]
  94. duration = data["duration"]
  95. format = data["format"]
  96. thumbnail = data["thumbnail"]
  97. cc = data["cc"]
  98. caption = f"""
  99. **Title:** {title}
  100. **Format:** {format}
  101. **Duration:** {await timeFormat(duration)}
  102. **CC:** {cc}
  103. """
  104. media, thumb = await gather(
  105. downloader.download(url), downloader.download(thumbnail)
  106. )
  107. await cq.message.edit("Uploading")
  108. if format == "mp3":
  109. await cq.message.reply_audio(
  110. media,
  111. quote=False,
  112. caption=caption,
  113. duration=duration,
  114. thumb=thumb,
  115. title=title,
  116. )
  117. else:
  118. await cq.message.reply_video(
  119. media,
  120. caption=caption,
  121. quote=False,
  122. duration=duration,
  123. thumb=thumb,
  124. supports_streaming=True,
  125. )
  126. del VIDEO_DATA[data_]
  127. remove(thumb)
  128. remove(media)
  129. await cq.message.delete()
  130. except Exception as e:
  131. e = traceback.format_exc()
  132. print(e)
  133. del VIDEO_DATA[data_]
  134. await cq.message.delete()