ytdl.py 3.3 KB

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