functions.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. from PIL import Image, ImageDraw, ImageFont, ImageFilter
  21. from random import randint
  22. from wbb.utils import aiodownloader
  23. from carbonnow import Carbon
  24. import speedtest
  25. import aiohttp
  26. import aiofiles
  27. import pickle
  28. import codecs
  29. """
  30. Just import 'downloader' anywhere and do downloader.download() to
  31. download file from a given url
  32. """
  33. downloader = aiodownloader.Handler()
  34. def generate_captcha():
  35. # Generate one letter
  36. def gen_letter():
  37. return chr(randint(65, 90))
  38. def rndColor():
  39. return (randint(64, 255), randint(64, 255), randint(64, 255))
  40. def rndColor2():
  41. return (randint(32, 127), randint(32, 127), randint(32, 127))
  42. # Generate a 4 letter word
  43. def gen_wrong_answer():
  44. word = ""
  45. for _ in range(4):
  46. word += gen_letter()
  47. return word
  48. # Generate 8 wrong captcha answers
  49. wrong_answers = []
  50. for _ in range(8):
  51. wrong_answers.append(gen_wrong_answer())
  52. width = 80 * 4
  53. height = 100
  54. correct_answer = ""
  55. font = ImageFont.truetype("assets/arial.ttf", 55)
  56. file = f"assets/{randint(1000, 9999)}.jpg"
  57. image = Image.new('RGB', (width, height), (255, 255, 255))
  58. draw = ImageDraw.Draw(image)
  59. # Draw random points on image
  60. for x in range(width):
  61. for y in range(height):
  62. draw.point((x, y), fill=rndColor())
  63. for t in range(4):
  64. letter = gen_letter()
  65. correct_answer += letter
  66. draw.text(
  67. (60 * t + 50, 15),
  68. letter,
  69. font=font,
  70. fill=rndColor2()
  71. )
  72. image = image.filter(ImageFilter.BLUR)
  73. image.save(file, 'jpeg')
  74. return [file, correct_answer, wrong_answers]
  75. async def test_speedtest():
  76. def speed_convert(size):
  77. power = 2 ** 10
  78. zero = 0
  79. units = {0: "", 1: "Kb/s", 2: "Mb/s", 3: "Gb/s", 4: "Tb/s"}
  80. while size > power:
  81. size /= power
  82. zero += 1
  83. return f"{round(size, 2)} {units[zero]}"
  84. speed = speedtest.Speedtest()
  85. info = speed.get_best_server()
  86. download = speed.download()
  87. upload = speed.upload()
  88. return [speed_convert(download), speed_convert(upload), info]
  89. async def file_size_from_url(url: str) -> int:
  90. async with aiohttp.ClientSession() as session:
  91. async with session.head(url) as resp:
  92. size = int(resp.headers['content-length'])
  93. return size
  94. async def get_http_status_code(url: str) -> int:
  95. async with aiohttp.ClientSession() as session:
  96. async with session.head(url) as resp:
  97. return resp.status
  98. async def make_carbon(code):
  99. carbon = Carbon(code=code)
  100. image = await carbon.save(str(randint(1000, 10000)))
  101. return image
  102. async def transfer_sh(file):
  103. async with aiofiles.open(file, 'rb') as f:
  104. params = {file: await f.read()}
  105. async with aiohttp.ClientSession() as session:
  106. async with session.post('https://transfer.sh/', data=params) as resp:
  107. download_link = str(await resp.text()).strip()
  108. return download_link
  109. def obj_to_str(object):
  110. if not object:
  111. return False
  112. string = codecs.encode(pickle.dumps(object), "base64").decode()
  113. return string
  114. def str_to_obj(string: str):
  115. object = pickle.loads(codecs.decode(string.encode(), "base64"))
  116. return object