ytdl.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 convert_seconds_to_minutes as timeFormat
  30. from wbb.utils.functions import downloader
  31. __MODULE__ = "YoutubeDL"
  32. __HELP__ = "/ytdl [VIDEO_LINK] - Download a youtube video."
  33. VIDEO_DATA = {}
  34. @app.on_message(filters.command("ytdl"))
  35. @capture_err
  36. async def ytdl_func(_, message):
  37. if len(message.command) != 2:
  38. return await message.reply_text("**Usage:**/ytdl [VIDEO_LINK]")
  39. m = await message.reply_text("Processing")
  40. url = message.text.split(None, 1)[1]
  41. results = await arq.ytdl(url)
  42. if not results.ok:
  43. return await m.edit(results.result)
  44. result = results.result
  45. title = result.title
  46. thumbnail = result.thumbnail
  47. duration = result.duration
  48. video = result.video
  49. buttons = InlineKeyboard(row_width=3)
  50. keyboard = []
  51. for media in video:
  52. quality = media.quality
  53. size = media.size
  54. url = media.url
  55. format = media.format
  56. data = str(randint(999, 9999999))
  57. VIDEO_DATA[data] = {
  58. "url": url,
  59. "title": title,
  60. "size": size,
  61. "quality": quality,
  62. "duration": duration,
  63. "format": format,
  64. "thumbnail": thumbnail,
  65. "cc": message.from_user.mention if message.from_user else "Anon",
  66. }
  67. keyboard.append(
  68. InlineKeyboardButton(
  69. text=f"{quality} | {size}", callback_data=f"ytdl {data}"
  70. )
  71. )
  72. buttons.add(*keyboard)
  73. caption = f"""
  74. **Title:** {title}
  75. **Duration:** {await timeFormat(duration)}
  76. """
  77. await message.reply_photo(thumbnail, caption=caption, reply_markup=buttons)
  78. await m.delete()
  79. @app.on_callback_query(filters.regex(r"^ytdl"))
  80. async def ytdlCallback(_, cq):
  81. await cq.message.edit("Downloading")
  82. data_ = cq.data.split()[1]
  83. try:
  84. data = VIDEO_DATA[data_]
  85. url = data["url"]
  86. title = data["title"]
  87. duration = data["duration"]
  88. format = data["format"]
  89. size = data["size"]
  90. thumbnail = data["thumbnail"]
  91. cc = data["cc"]
  92. caption = f"""
  93. **Title:** {title}
  94. **Size:** {size}
  95. **Format:** {format}
  96. **Duration:** {await timeFormat(duration)}
  97. **CC:** {cc}
  98. """
  99. media, thumb = await gather(
  100. downloader.download(url), downloader.download(thumbnail)
  101. )
  102. await cq.message.edit("Uploading")
  103. if format == "mp3":
  104. await cq.message.reply_audio(
  105. media,
  106. quote=False,
  107. caption=caption,
  108. duration=duration,
  109. thumb=thumb,
  110. title=title,
  111. )
  112. else:
  113. await cq.message.reply_video(
  114. media,
  115. caption=caption,
  116. quote=False,
  117. duration=duration,
  118. supports_streaming=True,
  119. )
  120. del VIDEO_DATA[data_]
  121. remove(thumb)
  122. remove(media)
  123. await cq.message.delete()
  124. except Exception as e:
  125. e = traceback.format_exc()
  126. print(e)
  127. del VIDEO_DATA[data_]
  128. await cq.message.delete()