download_upload.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. MIT License
  3. Copyright (c) 2024 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. from os import remove
  21. from os.path import isfile
  22. from time import ctime, time
  23. from traceback import format_exc
  24. from pyrogram import filters
  25. from pyrogram.types import Message
  26. from wbb import SUDOERS, USERBOT_PREFIX, app2, eor
  27. from wbb.core.sections import section
  28. from wbb.core.tasks import add_task, rm_task
  29. from wbb.utils.downloader import download
  30. @app2.on_message(
  31. SUDOERS
  32. & filters.command("download", prefixes=USERBOT_PREFIX)
  33. & ~filters.forwarded
  34. & ~filters.via_bot
  35. )
  36. async def download_func(_, message: Message):
  37. reply = message.reply_to_message
  38. start = time()
  39. body = {
  40. "Started": ctime(start),
  41. }
  42. m = await eor(
  43. message,
  44. text=section("Downloading", body),
  45. )
  46. if reply:
  47. task, task_id = await add_task(
  48. reply.download,
  49. task_name="Downloader",
  50. )
  51. body["Task ID"] = task_id
  52. await eor(
  53. m,
  54. text=section("Downloading", body),
  55. )
  56. await task
  57. await rm_task(task_id)
  58. file = task.result()
  59. elapsed = int(time() - start)
  60. body["File"] = file.split("/")[-1]
  61. body["Took"] = f"{elapsed}s"
  62. return await eor(m, text=section("Downloaded", body))
  63. text = message.text
  64. if len(text.split()) < 2:
  65. return await eor(m, text="Invalid Arguments")
  66. url = text.split(None, 1)[1]
  67. try:
  68. task, task_id = await download(url)
  69. body["Task ID"] = task_id
  70. await eor(
  71. m,
  72. text=section("Downloading", body),
  73. )
  74. await task
  75. file = task.result()
  76. await rm_task(task_id)
  77. except Exception as e:
  78. e = format_exc()
  79. e = e.splitlines()[-1]
  80. return await eor(m, text=f"**Error:** `{str(e)}`")
  81. elapsed = int(time() - start)
  82. body["File"] = file.split("/")[-1]
  83. body["Took"] = f"{elapsed}s"
  84. await eor(m, text=section("Downloaded", body))
  85. @app2.on_message(
  86. SUDOERS
  87. & filters.command("upload", prefixes=USERBOT_PREFIX)
  88. & ~filters.forwarded
  89. & ~filters.via_bot
  90. )
  91. async def upload_func(_, message: Message):
  92. if len(message.text.split()) != 2:
  93. return await eor(message, text="Invalid Arguments")
  94. url_or_path = message.text.split(None, 1)[1]
  95. start = time()
  96. body = {
  97. "Started": ctime(start),
  98. }
  99. m = await eor(message, text=section("Uploading", body))
  100. async def upload_file(path: str):
  101. task, task_id = await add_task(
  102. message.reply_document,
  103. "Uploader",
  104. path,
  105. )
  106. body["Task ID"] = task_id
  107. await eor(
  108. m,
  109. text=section("Uploading", body),
  110. )
  111. await task
  112. await rm_task(task_id)
  113. elapsed = int(time() - start)
  114. body["Took"] = f"{elapsed}s"
  115. return await eor(m, text=section("Uploaded", body))
  116. try:
  117. if isfile(url_or_path):
  118. return await upload_file(url_or_path)
  119. task, task_id = await download(url_or_path)
  120. body["Task ID"] = task_id
  121. await eor(
  122. m,
  123. text=section("Downloading", body),
  124. )
  125. await task
  126. file = task.result()
  127. await rm_task(task_id)
  128. await upload_file(file)
  129. remove(file)
  130. except Exception as e:
  131. e = format_exc()
  132. e = e.splitlines()[-1]
  133. return await eor(m, text=f"**Error:** `{str(e)}`")