ytdl.py 3.4 KB

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