136 lines
2.9 KiB
Python
136 lines
2.9 KiB
Python
import aiohttp
|
|
import re
|
|
import json
|
|
import logging
|
|
|
|
from http import client as http
|
|
|
|
from colour import Color
|
|
from aiohttp_jinja2 import render_template as render
|
|
|
|
|
|
error_codes = {
|
|
400: 'BadRequest',
|
|
404: 'NotFound',
|
|
401: 'Unauthorized',
|
|
403: 'Forbidden',
|
|
404: 'NotFound',
|
|
500: 'InternalServerError',
|
|
504: 'GatewayTimeout'
|
|
}
|
|
|
|
|
|
def bool_check(value):
|
|
if value == True or str(value).lower() in ['yes', 'true', 'enable']:
|
|
return True
|
|
|
|
elif value in [None, False] or str(value).lower() in ['no', 'false', 'disable', '']:
|
|
return False
|
|
|
|
else:
|
|
return value
|
|
|
|
|
|
def error(code, error):
|
|
error_body = json.dumps({'error': error})
|
|
cont_type = 'application/json'
|
|
|
|
if code == 418:
|
|
raise HTTPTeapot(body=error_body, content_type=cont_type)
|
|
|
|
elif code not in error_codes.keys():
|
|
logging.error(f'Hey! You specified a wrong error code: {code} {error}')
|
|
|
|
error_body = json.dumps({'error': 'DevError'})
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(body=error_body, content_type=cont_type)
|
|
|
|
raise eval('aiohttp.web.HTTP'+error_codes[code]+'(body=error_body, content_type=cont_type)')
|
|
|
|
|
|
def fed_domain(user, domain):
|
|
headers = {'Accept': 'application/json'}
|
|
conn = http.HTTPSConnection(domain)
|
|
conn.request('GET', f'/.well-known/webfinger?resource=acct:{user}@{domain}', headers=headers)
|
|
response = conn.getresponse()
|
|
|
|
if response.status != 200:
|
|
return
|
|
|
|
try:
|
|
data = json.loads(response.read())
|
|
|
|
except:
|
|
logging.debug(f'Invalid user: {user}@{domain}')
|
|
|
|
wf_data = data.get('subject')
|
|
user_data = wf_data.replace('acct:', '').split('@')
|
|
|
|
return user_data[1]
|
|
|
|
|
|
def user_check(path):
|
|
parsed = path.replace('.json', '').split('/')
|
|
|
|
if (parsed[1] == 'users' and len(parsed) < 4) or (parsed[1].startswith('@') and len(parsed) < 3):
|
|
try:
|
|
int(parsed[-1])
|
|
return False
|
|
|
|
except ValueError:
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
class color:
|
|
def __init__(self):
|
|
self.check = lambda color: Color(f'#{str(color)}' if re.search(r'^(?:[0-9a-fA-F]{3}){1,2}$', color) else color)
|
|
|
|
def multi(self, multiplier):
|
|
if multiplier >= 1:
|
|
return 1
|
|
|
|
elif multiplier <= 0:
|
|
return 0
|
|
|
|
return multiplier
|
|
|
|
def lighten(self, color, multiplier):
|
|
col = self.check(color)
|
|
col.luminance += ((1 - col.luminance) * self.multi(multiplier))
|
|
|
|
return col.hex_l
|
|
|
|
def darken(self, color, multiplier):
|
|
col = self.check(color)
|
|
col.luminance -= (col.luminance * self.multi(multiplier))
|
|
|
|
return col.hex_l
|
|
|
|
|
|
def saturate(self, color, multiplier):
|
|
col = self.check(color)
|
|
col.saturation += ((1 - col.saturation) * self.multi(multiplier))
|
|
|
|
return col.hex_l
|
|
|
|
|
|
def desaturate(self, color, multiplier):
|
|
col = self.check(color)
|
|
col.saturation -= (col.saturation * self.multi(multiplier))
|
|
|
|
return col.hex_l
|
|
|
|
|
|
def rgba(self, color, transparency):
|
|
col = self.check(color)
|
|
|
|
red = col.red*255
|
|
green = col.green*255
|
|
blue = col.blue*255
|
|
trans = self.multi(transparency)
|
|
|
|
return f'rgba({red:0.2f}, {green:0.2f}, {blue:0.2f}, {trans:0.2f})'
|
|
|