ytdl.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import traceback
  2. from asyncio import gather
  3. from os import remove
  4. from random import randint
  5. from pykeyboard import InlineKeyboard
  6. from pyrogram import filters
  7. from pyrogram.types import InlineKeyboardButton
  8. from wbb import app, arq
  9. from wbb.core.decorators.errors import capture_err
  10. from wbb.utils.formatter import convert_seconds_to_minutes as timeFormat
  11. from wbb.utils.functions import downloader
  12. __MODULE__ = "YoutubeDL"
  13. __HELP__ = "/ytdl [VIDEO_LINK] - Download a youtube video."
  14. VIDEO_DATA = {}
  15. @app.on_message(filters.command("ytdl"))
  16. async def ytdl_func(_, message):
  17. if len(message.command) != 2:
  18. await message.reply_text("**Usage:**/ytdl [VIDEO_LINK]")
  19. return
  20. m = await message.reply_text("Processing")
  21. url = message.text.split(None, 1)[1]
  22. results = await arq.ytdl(url)
  23. if not results.ok:
  24. await message.reply_text(results.result)
  25. return
  26. result = results.result
  27. title = result.title
  28. thumbnail = result.thumbnail
  29. duration = result.duration
  30. video = result.video
  31. buttons = InlineKeyboard(row_width=3)
  32. keyboard = []
  33. for media in video:
  34. quality = media.quality
  35. size = media.size
  36. url = media.url
  37. format = media.format
  38. data = str(randint(999, 9999999))
  39. VIDEO_DATA[data] = {
  40. "url": url,
  41. "title": title,
  42. "size": size,
  43. "quality": quality,
  44. "duration": duration,
  45. "format": format,
  46. "thumbnail": thumbnail,
  47. }
  48. keyboard.append(
  49. InlineKeyboardButton(
  50. text=f"{quality} | {size}", callback_data=f"ytdl {data}"
  51. )
  52. )
  53. buttons.add(*keyboard)
  54. caption = f"""
  55. **Title:** {title}
  56. **Duration:** {await timeFormat(duration)}
  57. **Size:** {size}
  58. """
  59. await message.reply_photo(thumbnail, caption=caption, reply_markup=buttons)
  60. await m.delete()
  61. @app.on_callback_query(filters.regex(r"^ytdl"))
  62. async def ytdlCallback(_, cq):
  63. await cq.message.edit("Downloading")
  64. data_ = cq.data.split()[1]
  65. try:
  66. data = VIDEO_DATA[data_]
  67. url = data["url"]
  68. title = data["title"]
  69. duration = data["duration"]
  70. format = data["format"]
  71. size = data["size"]
  72. thumbnail = data["thumbnail"]
  73. caption = f"""
  74. **Title:** {title}
  75. **Size:** {size}
  76. **Format:** {format}
  77. **Duration:** {await timeFormat(duration)}
  78. """
  79. media, thumb = await gather(
  80. downloader.download(url), downloader.download(thumbnail)
  81. )
  82. await cq.message.edit("Uploading")
  83. if format == "mp3":
  84. await cq.message.reply_audio(
  85. media,
  86. quote=False,
  87. caption=caption,
  88. duration=duration,
  89. thumb=thumb,
  90. title=title,
  91. )
  92. else:
  93. await cq.message.reply_video(
  94. media,
  95. caption=caption,
  96. quote=False,
  97. duration=duration,
  98. supports_streaming=True,
  99. )
  100. del VIDEO_DATA[data_]
  101. remove(thumb)
  102. remove(media)
  103. await cq.message.delete()
  104. except Exception as e:
  105. e = traceback.format_exc()
  106. print(e)
  107. del VIDEO_DATA[data_]
  108. await cq.message.delete()