bay.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. import requests
  3. import aiohttp
  4. import aiofiles
  5. from wbb import app, OWNER_ID
  6. from pyrogram import Client, filters
  7. __MODULE__ = "BAYFILES"
  8. __HELP__ = """/url [URL] To upload a url to bayfiles. [SUDO ONLY]
  9. /tg To Upload a telegram file to bayfiles. [SUDO ONLY]"""
  10. @app.on_message(filters.command("url") & filters.user(OWNER_ID))
  11. async def url(_, message):
  12. if len(message.command) != 2:
  13. await message.reply_text("/url [url]")
  14. return
  15. m = await message.reply_text("Downloading")
  16. lenk = message.text.split(None, 1)[1]
  17. try:
  18. filename = await download(lenk)
  19. files = { 'file': open(filename, 'rb')}
  20. await m.edit("Uploading....")
  21. r = requests.post("https://api.bayfiles.com/upload", files=files)
  22. text = r.json()
  23. output = f"""
  24. **status:** `{text['status']}`
  25. **link:** {text['data']['file']['url']['full']}
  26. **id:** `{text['data']['file']['metadata']['id']}`
  27. **name:** `{text['data']['file']['metadata']['name']}`
  28. **size:** `{text['data']['file']['metadata']['size']['readable']}`"""
  29. await m.edit(output)
  30. os.remove(filename)
  31. except Exception as e:
  32. print(str(e))
  33. await m.edit(str(e))
  34. return
  35. @app.on_message(filters.command("tg") & filters.user(OWNER_ID))
  36. async def tg(_, message):
  37. if not message.reply_to_message:
  38. await message.reply_text("Reply To A File With /tg To Upload")
  39. return
  40. if not message.reply_to_message.media:
  41. await message.reply_text("Reply To A File With /tg To Upload")
  42. return
  43. m = await message.reply_text("Downloading Document.")
  44. fn = await message.reply_to_message.download()
  45. try:
  46. files = { 'file': open(fn, 'rb')}
  47. await m.edit("Uploading....")
  48. r = requests.post("https://api.bayfiles.com/upload", files=files)
  49. text = r.json()
  50. output = f"""
  51. **Status:** `{text['status']}`
  52. **Link:** {text['data']['file']['url']['full']}
  53. **ID:** `{text['data']['file']['metadata']['id']}`
  54. **Name:** `{text['data']['file']['metadata']['name']}`
  55. **Size:** `{text['data']['file']['metadata']['size']['readable']}`"""
  56. await m.edit(output)
  57. os.remove(fn)
  58. except Exception as e:
  59. print(str(e))
  60. await m.edit(str(e))
  61. return
  62. async def download(filename, url):
  63. async with aiohttp.ClientSession() as session:
  64. async with session.get(url) as resp:
  65. if resp.status == 200:
  66. f = await aiofiles.open(filename, mode='wb')
  67. await f.write(await resp.read())
  68. await f.close()
  69. return filename