""" 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. """ import codecs import pickle from asyncio import gather, get_running_loop from math import atan2, cos, radians, sin, sqrt from random import randint import aiofiles import aiohttp import speedtest from PIL import Image, ImageDraw, ImageFilter, ImageFont from wget import download from wbb import aiohttpsession as aiosession from wbb.utils import aiodownloader from wbb.utils.fetch import fetch """ Just import 'downloader' anywhere and do downloader.download() to download file from a given url """ downloader = aiodownloader.Handler() # Another downloader, but with wget async def download_url(url: str): loop = get_running_loop() file = await loop.run_in_executor(None, download, url) return file 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] 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 aiosession.head(url) as resp: size = int(resp.headers["content-length"]) return size async def get_http_status_code(url: str) -> int: async with aiosession.head(url) as resp: return resp.status async def make_carbon(code): url = "https://carbonara.vercel.app/api/cook" async with aiosession.post( url, json={"code": code} ) as resp: data = await resp.read() image = f"{randint(100, 10000)}.png" async with aiofiles.open(image, mode="wb") as f: await f.write(data) return image async def transfer_sh(file): async with aiofiles.open(file, "rb") as f: params = {file: await f.read()} async with aiohttp.ClientSession() as session: async with session.post("https://transfer.sh/", data=params) as resp: download_link = str(await resp.text()).strip() return download_link def obj_to_str(object): if not object: return False string = codecs.encode(pickle.dumps(object), "base64").decode() return string def str_to_obj(string: str): object = pickle.loads(codecs.decode(string.encode(), "base64")) return object async def calc_distance_from_ip(ip1: str, ip2: str) -> float: Radius_Earth = 6371.0088 data1, data2 = await gather( fetch(f"http://ipinfo.io/{ip1}"), fetch(f"http://ipinfo.io/{ip2}") ) lat1, lon1 = data1["loc"].split(",") lat2, lon2 = data2["loc"].split(",") lat1, lon1 = radians(float(lat1)), radians(float(lon1)) lat2, lon2 = radians(float(lat2)), radians(float(lon2)) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2 c = 2 * atan2(sqrt(a), sqrt(1 - a)) distance = Radius_Earth * c return distance