| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- """
- MIT License
- Copyright (c) 2021 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- from PIL import Image, ImageDraw, ImageFont, ImageFilter
- from random import randint
- from wbb.utils import aiodownloader
- from carbonnow import Carbon
- import speedtest
- import aiohttp
- """
- Just import 'downloader' anywhere and do downloader.download() to
- download file from a given url
- """
- downloader = aiodownloader.Handler()
- def generate_captcha():
- # Generate one letter
- def gen_letter():
- return chr(randint(65, 90))
- def rndColor():
- return (randint(64, 255), randint(64, 255), randint(64, 255))
- def rndColor2():
- return (randint(32, 127), randint(32, 127), randint(32, 127))
- # Generate a 4 letter word
- def gen_wrong_answer():
- word = ""
- for _ in range(4):
- word += gen_letter()
- return word
- # Generate 8 wrong captcha answers
- wrong_answers = []
- for _ in range(8):
- wrong_answers.append(gen_wrong_answer())
- width = 80 * 4
- height = 100
- correct_answer = ""
- font = ImageFont.truetype("assets/arial.ttf", 55)
- file = f"assets/{randint(1000, 9999)}.jpg"
- image = Image.new('RGB', (width, height), (255, 255, 255))
- draw = ImageDraw.Draw(image)
- # Draw random points on image
- for x in range(width):
- for y in range(height):
- draw.point((x, y), fill=rndColor())
- for t in range(4):
- letter = gen_letter()
- correct_answer += letter
- draw.text(
- (60 * t + 50, 15),
- letter,
- font=font,
- fill=rndColor2()
- )
- image = image.filter(ImageFilter.BLUR)
- image.save(file, 'jpeg')
- return [file, correct_answer, wrong_answers]
- async def test_speedtest():
- def speed_convert(size):
- power = 2 ** 10
- zero = 0
- units = {0: "", 1: "Kb/s", 2: "Mb/s", 3: "Gb/s", 4: "Tb/s"}
- while size > power:
- size /= power
- zero += 1
- return f"{round(size, 2)} {units[zero]}"
- speed = speedtest.Speedtest()
- info = speed.get_best_server()
- download = speed.download()
- upload = speed.upload()
- return [speed_convert(download), speed_convert(upload), info]
- async def file_size_from_url(url: str) -> int:
- async with aiohttp.ClientSession() as session:
- async with session.head(url) as resp:
- size = int(resp.headers['content-length'])
- return size
- async def get_http_status_code(url: str) -> int:
- async with aiohttp.ClientSession() as session:
- async with session.head(url) as resp:
- return resp.status
- async def make_carbon(code):
- carbon = Carbon(code=code)
- image = await carbon.save(str(randint(1000, 10000)))
- return image
|