2022-02-14 22:19:02 +03:00
|
|
|
from bs4 import BeautifulSoup as bsoup
|
2021-10-29 06:31:24 +03:00
|
|
|
from flask import Request
|
2021-07-01 02:00:01 +03:00
|
|
|
import hashlib
|
|
|
|
import os
|
2022-02-14 22:19:02 +03:00
|
|
|
from requests import exceptions, get
|
2022-04-13 20:29:07 +03:00
|
|
|
from urllib.parse import urlparse
|
2021-07-01 02:00:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def gen_file_hash(path: str, static_file: str) -> str:
|
|
|
|
file_contents = open(os.path.join(path, static_file), 'rb').read()
|
|
|
|
file_hash = hashlib.md5(file_contents).hexdigest()[:8]
|
|
|
|
filename_split = os.path.splitext(static_file)
|
|
|
|
|
|
|
|
return filename_split[0] + '.' + file_hash + filename_split[-1]
|
2021-10-15 03:58:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def read_config_bool(var: str) -> bool:
|
|
|
|
val = os.getenv(var, '0')
|
|
|
|
if val.isdigit():
|
|
|
|
return bool(int(val))
|
|
|
|
return False
|
2021-10-29 06:31:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_client_ip(r: Request) -> str:
|
|
|
|
if r.environ.get('HTTP_X_FORWARDED_FOR') is None:
|
|
|
|
return r.environ['REMOTE_ADDR']
|
|
|
|
else:
|
|
|
|
return r.environ['HTTP_X_FORWARDED_FOR']
|
2021-11-22 09:21:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_request_url(url: str) -> str:
|
|
|
|
if os.getenv('HTTPS_ONLY', False):
|
|
|
|
return url.replace('http://', 'https://', 1)
|
|
|
|
|
|
|
|
return url
|
2022-02-14 22:19:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def check_for_update(version_url: str, current: str) -> int:
|
|
|
|
# Check for the latest version of Whoogle
|
|
|
|
try:
|
|
|
|
update = bsoup(get(version_url).text, 'html.parser')
|
|
|
|
latest = update.select_one('[class="Link--primary"]').string[1:]
|
|
|
|
current = int(''.join(filter(str.isdigit, current)))
|
|
|
|
latest = int(''.join(filter(str.isdigit, latest)))
|
|
|
|
has_update = '' if current >= latest else latest
|
|
|
|
except (exceptions.ConnectionError, AttributeError):
|
|
|
|
# Ignore failures, assume current version is up to date
|
|
|
|
has_update = ''
|
|
|
|
|
|
|
|
return has_update
|
2022-04-13 20:29:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_abs_url(url, page_url):
|
|
|
|
# Creates a valid absolute URL using a partial or relative URL
|
|
|
|
if url.startswith('//'):
|
|
|
|
return f'https:{url}'
|
|
|
|
elif url.startswith('/'):
|
|
|
|
return f'{urlparse(page_url).netloc}{url}'
|
|
|
|
elif url.startswith('./'):
|
|
|
|
return f'{page_url}{url[2:]}'
|
|
|
|
return url
|