download_upload.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from os import remove
  2. from os.path import isfile
  3. from time import ctime, time
  4. from pyrogram import filters
  5. from pyrogram.types import Message
  6. from wbb import SUDOERS, USERBOT_PREFIX, app2
  7. from wbb.core.sections import section
  8. from wbb.modules.userbot import add_task, eor, rm_task
  9. from wbb.utils.downloader import download
  10. @app2.on_message(
  11. filters.user(SUDOERS)
  12. & filters.command("download", prefixes=USERBOT_PREFIX)
  13. )
  14. async def download_func(_, message: Message):
  15. reply = message.reply_to_message
  16. start = time()
  17. task_id = int(start)
  18. body = {
  19. "Started": ctime(start),
  20. "Task ID": task_id,
  21. }
  22. m = await eor(
  23. message,
  24. text=section("Downloading", body),
  25. )
  26. if reply:
  27. task = await add_task(
  28. reply.download,
  29. task_id=task_id,
  30. task_name="Downloader",
  31. )
  32. await task
  33. await rm_task(task_id)
  34. elapsed = int(time() - start)
  35. body["Took"] = f"{elapsed}s"
  36. return await eor(m, text=section("Downloaded", body))
  37. text = message.text
  38. if len(text.split()) < 2:
  39. return await eor(message, text="Invalid Arguments")
  40. url = text.split(None, 1)[1]
  41. try:
  42. await download(
  43. url,
  44. task_id=task_id,
  45. )
  46. except Exception as e:
  47. return await eor(m, text=f"**Error:** `{str(e)}`")
  48. elapsed = int(time() - start)
  49. body["Took"] = f"{elapsed}s"
  50. await eor(m, text=section("Downloaded", body))
  51. @app2.on_message(
  52. filters.user(SUDOERS)
  53. & filters.command("upload", prefixes=USERBOT_PREFIX)
  54. )
  55. async def upload_func(_, message: Message):
  56. if len(message.text.split()) != 2:
  57. return await eor(message, text="Invalid Arguments")
  58. url_or_path = message.text.split(None, 1)[1]
  59. start = time()
  60. task_id = int(start)
  61. body = {
  62. "Started": ctime(start),
  63. "Task ID": task_id,
  64. }
  65. m = await eor(message, text=section("Uploading", body))
  66. async def upload_file(path: str):
  67. task = await add_task(
  68. message.reply_document,
  69. task_id,
  70. "Uploader",
  71. path,
  72. )
  73. await task
  74. await rm_task(task_id)
  75. elapsed = int(time() - start)
  76. body["Took"] = f"{elapsed}s"
  77. return await eor(m, text=section("Uploaded", body))
  78. try:
  79. if isfile(url_or_path):
  80. return await upload_file(url_or_path)
  81. path = await download(
  82. url_or_path,
  83. task_id=task_id,
  84. )
  85. return await upload_file(path)
  86. except Exception as e:
  87. return await eor(m, text=f"**Error:** `{str(e)}`")