bay.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. MIT License
  3. Copyright (c) 2021 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. import os
  21. import requests
  22. import aiohttp
  23. import aiofiles
  24. from wbb import app, SUDOERS
  25. from pyrogram import filters
  26. from random import randint
  27. from wbb.core.decorators.errors import capture_err
  28. __MODULE__ = "BAYFILES"
  29. __HELP__ = """/url [URL] To upload a url to bayfiles. [SUDO ONLY]
  30. /tg To Upload a telegram file to bayfiles. [SUDO ONLY]"""
  31. @app.on_message(filters.command("url") & filters.user(SUDOERS))
  32. @capture_err
  33. async def url(_, message):
  34. if len(message.command) != 2:
  35. await message.reply_text("/url [url]")
  36. return
  37. m = await message.reply_text("Downloading")
  38. lenk = message.text.split(None, 1)[1]
  39. try:
  40. filename = await download(lenk)
  41. files = {'file': open(filename, 'rb')}
  42. await m.edit("Uploading....")
  43. r = requests.post("https://api.bayfiles.com/upload", files=files)
  44. text = r.json()
  45. output = f"""
  46. **status:** `{text['status']}`
  47. **link:** {text['data']['file']['url']['full']}
  48. **id:** `{text['data']['file']['metadata']['id']}`
  49. **name:** `{text['data']['file']['metadata']['name']}`
  50. **size:** `{text['data']['file']['metadata']['size']['readable']}`"""
  51. await m.edit(output)
  52. os.remove(filename)
  53. except Exception as e:
  54. print(str(e))
  55. await m.edit(str(e))
  56. return
  57. @app.on_message(filters.command("tg") & filters.user(SUDOERS))
  58. @capture_err
  59. async def tg(_, message):
  60. if not message.reply_to_message:
  61. await message.reply_text("Reply To A File With /tg To Upload")
  62. return
  63. if not message.reply_to_message.media:
  64. await message.reply_text("Reply To A File With /tg To Upload")
  65. return
  66. m = await message.reply_text("Downloading Document.")
  67. fn = await message.reply_to_message.download()
  68. try:
  69. files = {'file': open(fn, 'rb')}
  70. await m.edit("Uploading....")
  71. r = requests.post("https://api.bayfiles.com/upload", files=files)
  72. text = r.json()
  73. output = f"""
  74. **Status:** `{text['status']}`
  75. **Link:** {text['data']['file']['url']['full']}
  76. **ID:** `{text['data']['file']['metadata']['id']}`
  77. **Name:** `{text['data']['file']['metadata']['name']}`
  78. **Size:** `{text['data']['file']['metadata']['size']['readable']}`"""
  79. await m.edit(output)
  80. os.remove(fn)
  81. except Exception as e:
  82. print(str(e))
  83. await m.edit(str(e))
  84. return
  85. async def download(url):
  86. ext = url.split(".")[-1]
  87. filename = str(randint(1000, 9999)) + "." + ext
  88. async with aiohttp.ClientSession() as session:
  89. async with session.get(url) as resp:
  90. if resp.status == 200:
  91. f = await aiofiles.open(filename, mode='wb')
  92. await f.write(await resp.read())
  93. await f.close()
  94. return filename