Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring utils/misc.py #1165

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions app/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import base64
from bs4 import BeautifulSoup as bsoup
from cryptography.fernet import Fernet
from flask import Request
import hashlib
import contextlib
import io
import os
import re

from requests import exceptions, get
from urllib.parse import urlparse
from bs4 import BeautifulSoup as bsoup
from cryptography.fernet import Fernet
from flask import Request

ddg_favicon_site = 'http://icons.duckduckgo.com/ip2'

Expand All @@ -34,42 +36,38 @@ def fetch_favicon(url: str) -> bytes:
bytes - the favicon bytes, or a placeholder image if one
was not returned
"""
domain = urlparse(url).netloc

response = get(f'{ddg_favicon_site}/{domain}.ico')
response = get(f'{ddg_favicon_site}/{urlparse(url).netloc}.ico')

if response.status_code == 200 and len(response.content) > 0:
tmp_mem = io.BytesIO()
tmp_mem.write(response.content)
tmp_mem.seek(0)

return tmp_mem.read()
else:
return placeholder_img
return placeholder_img


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]
return f'{filename_split[0]}.{file_hash}{filename_split[-1]}'


def read_config_bool(var: str, default: bool=False) -> bool:
val = os.getenv(var, '1' if default else '0')
# user can specify one of the following values as 'true' inputs (all
# variants with upper case letters will also work):
# ('true', 't', '1', 'yes', 'y')
val = val.lower() in ('true', 't', '1', 'yes', 'y')
return val
return val.lower() in ('true', 't', '1', 'yes', 'y')


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']

return r.environ['HTTP_X_FORWARDED_FOR']


def get_request_url(url: str) -> str:
Expand Down Expand Up @@ -98,27 +96,28 @@ def get_proxy_host_url(r: Request, default: str, root=False) -> str:

def check_for_update(version_url: str, current: str) -> int:
# Check for the latest version of Whoogle
try:
has_update = ''
with contextlib.suppress(exceptions.ConnectionError, AttributeError):
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


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:]}'
urls = {
"//": f"https:{url}",
"/": f"{urlparse(page_url).netloc}{url}",
"./": f"{page_url}{url[2:]}"
}
for start in urls:
if url.startswith(start):
return urls[start]

return url


Expand Down
Loading