From b3da0b04122695a33fbff47c3792e089cd995b3a Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 8 Jul 2024 22:40:59 +0300 Subject: [PATCH 1/2] Refactoring utils/misc.py --- app/utils/misc.py | 53 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/app/utils/misc.py b/app/utils/misc.py index 20705bc47c..b92cb7fcc8 100644 --- a/app/utils/misc.py +++ b/app/utils/misc.py @@ -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' @@ -34,18 +36,15 @@ 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}/{urlparse(url).netloc}.ico') - response = get(f'{ddg_favicon_site}/{domain}.ico') - - if response.status_code == 200 and len(response.content) > 0: + if response.status_code 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: @@ -53,7 +52,7 @@ def gen_file_hash(path: str, static_file: str) -> str: 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: @@ -61,15 +60,14 @@ def read_config_bool(var: str, default: bool=False) -> bool: # 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: @@ -97,28 +95,29 @@ 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: + 'Check for the latest version of Whoogle' + 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:]}' + 'Creates a valid absolute URL using a partial or relative URL' + 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 @@ -136,4 +135,4 @@ def encrypt_string(key: bytes, string: str) -> str: def decrypt_string(key: bytes, string: str) -> str: cipher_suite = Fernet(g.session_key) - return cipher_suite.decrypt(string.encode()).decode() + return cipher_suite.decrypt(string.encode()).decode() \ No newline at end of file From 137cfe63c3f28934069fe92980a6fb5a8ae244a6 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Mon, 30 Sep 2024 10:39:44 -0600 Subject: [PATCH 2/2] Minor changes to comment style and favicon response code --- app/utils/misc.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/utils/misc.py b/app/utils/misc.py index b92cb7fcc8..b0c74c24b5 100644 --- a/app/utils/misc.py +++ b/app/utils/misc.py @@ -38,7 +38,7 @@ def fetch_favicon(url: str) -> bytes: """ response = get(f'{ddg_favicon_site}/{urlparse(url).netloc}.ico') - if response.status_code and len(response.content) > 0: + if response.status_code == 200 and len(response.content) > 0: tmp_mem = io.BytesIO() tmp_mem.write(response.content) tmp_mem.seek(0) @@ -66,7 +66,7 @@ def read_config_bool(var: str, default: bool=False) -> bool: def get_client_ip(r: Request) -> str: if r.environ.get('HTTP_X_FORWARDED_FOR') is None: return r.environ['REMOTE_ADDR'] - + return r.environ['HTTP_X_FORWARDED_FOR'] @@ -95,7 +95,7 @@ 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' + # Check for the latest version of Whoogle has_update = '' with contextlib.suppress(exceptions.ConnectionError, AttributeError): update = bsoup(get(version_url).text, 'html.parser') @@ -108,7 +108,7 @@ def check_for_update(version_url: str, current: str) -> int: def get_abs_url(url, page_url): - 'Creates a valid absolute URL using a partial or relative URL' + # Creates a valid absolute URL using a partial or relative URL urls = { "//": f"https:{url}", "/": f"{urlparse(page_url).netloc}{url}", @@ -135,4 +135,4 @@ def encrypt_string(key: bytes, string: str) -> str: def decrypt_string(key: bytes, string: str) -> str: cipher_suite = Fernet(g.session_key) - return cipher_suite.decrypt(string.encode()).decode() \ No newline at end of file + return cipher_suite.decrypt(string.encode()).decode()