functions.py 3.3 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. from PIL import Image, ImageDraw, ImageFont, ImageFilter
  21. from random import randint
  22. import speedtest
  23. import aiohttp
  24. def generate_captcha():
  25. # Generate one letter
  26. def gen_letter():
  27. return chr(randint(65, 90))
  28. def rndColor():
  29. return (randint(64, 255), randint(64, 255), randint(64, 255))
  30. def rndColor2():
  31. return (randint(32, 127), randint(32, 127), randint(32, 127))
  32. # Generate a 4 letter word
  33. def gen_wrong_answer():
  34. word = ""
  35. for _ in range(4):
  36. word += gen_letter()
  37. return word
  38. # Generate 8 wrong captcha answers
  39. wrong_answers = []
  40. for _ in range(8):
  41. wrong_answers.append(gen_wrong_answer())
  42. width = 80 * 4
  43. height = 100
  44. correct_answer = ""
  45. font = ImageFont.truetype("assets/arial.ttf", 55)
  46. file = f"assets/{randint(1000, 9999)}.jpg"
  47. image = Image.new('RGB', (width, height), (255, 255, 255))
  48. draw = ImageDraw.Draw(image)
  49. # Draw random points on image
  50. for x in range(width):
  51. for y in range(height):
  52. draw.point((x, y), fill=rndColor())
  53. for t in range(4):
  54. letter = gen_letter()
  55. correct_answer += letter
  56. draw.text(
  57. (60 * t + 50, 15),
  58. letter,
  59. font=font,
  60. fill=rndColor2()
  61. )
  62. image = image.filter(ImageFilter.BLUR)
  63. image.save(file, 'jpeg')
  64. return [file, correct_answer, wrong_answers]
  65. async def test_speedtest():
  66. def speed_convert(size):
  67. power = 2 ** 10
  68. zero = 0
  69. units = {0: "", 1: "Kb/s", 2: "Mb/s", 3: "Gb/s", 4: "Tb/s"}
  70. while size > power:
  71. size /= power
  72. zero += 1
  73. return f"{round(size, 2)} {units[zero]}"
  74. speed = speedtest.Speedtest()
  75. info = speed.get_best_server()
  76. download = speed.download()
  77. upload = speed.upload()
  78. return [speed_convert(download), speed_convert(upload), info]
  79. async def file_size_from_url(url: str) -> int:
  80. async with aiohttp.ClientSession() as session:
  81. async with session.head(url) as resp:
  82. size = int(resp.headers['content-length'])
  83. return size
  84. async def get_http_status_code(url: str) -> int:
  85. async with aiohttp.ClientSession() as session:
  86. async with session.head(url) as resp:
  87. return resp.status