Improve naming of *_utils files, update fn/class doc
The app/utils/*_utils weren't named very well, and all have been updated to have more accurate names. Function and class documention for the utils have been updated as well, as part of the effort to improve overall documentation for the project.pull/223/head
parent
855b4e8502
commit
d447e5009f
@ -0,0 +1,61 @@
|
|||||||
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
|
DDG_BANGS = 'https://duckduckgo.com/bang.v255.js'
|
||||||
|
|
||||||
|
|
||||||
|
def gen_bangs_json(bangs_file: str) -> None:
|
||||||
|
"""Generates a json file from the DDG bangs list
|
||||||
|
|
||||||
|
Args:
|
||||||
|
bangs_file: The str path to the new DDG bangs json file
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Request full list from DDG
|
||||||
|
r = requests.get(DDG_BANGS)
|
||||||
|
r.raise_for_status()
|
||||||
|
except requests.exceptions.HTTPError as err:
|
||||||
|
raise SystemExit(err)
|
||||||
|
|
||||||
|
# Convert to json
|
||||||
|
data = json.loads(r.text)
|
||||||
|
|
||||||
|
# Set up a json object (with better formatting) for all available bangs
|
||||||
|
bangs_data = {}
|
||||||
|
|
||||||
|
for row in data:
|
||||||
|
bang_command = '!' + row['t']
|
||||||
|
bangs_data[bang_command] = {
|
||||||
|
'url': row['u'].replace('{{{s}}}', '{}'),
|
||||||
|
'suggestion': bang_command + ' (' + row['s'] + ')'
|
||||||
|
}
|
||||||
|
|
||||||
|
json.dump(bangs_data, open(bangs_file, 'w'))
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_bang(query: str, bangs_dict: dict) -> str:
|
||||||
|
"""Transform's a user's query to a bang search, if an operator is found
|
||||||
|
|
||||||
|
Args:
|
||||||
|
query: The search query
|
||||||
|
bangs_dict: The dict of available bang operators, with corresponding
|
||||||
|
format string search URLs
|
||||||
|
(i.e. "!w": "https://en.wikipedia.org...?search={}")
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: A formatted redirect for a bang search, or an empty str if there
|
||||||
|
wasn't a match or didn't contain a bang operator
|
||||||
|
|
||||||
|
"""
|
||||||
|
split_query = query.split(' ')
|
||||||
|
for operator in bangs_dict.keys():
|
||||||
|
if operator not in split_query:
|
||||||
|
continue
|
||||||
|
|
||||||
|
return bangs_dict[operator]['url'].format(
|
||||||
|
query.replace(operator, '').strip())
|
||||||
|
return ''
|
@ -1,26 +0,0 @@
|
|||||||
import json
|
|
||||||
import requests
|
|
||||||
|
|
||||||
|
|
||||||
def gen_bangs_json(bangs_file):
|
|
||||||
# Request list
|
|
||||||
try:
|
|
||||||
r = requests.get('https://duckduckgo.com/bang.v255.js')
|
|
||||||
r.raise_for_status()
|
|
||||||
except requests.exceptions.HTTPError as err:
|
|
||||||
raise SystemExit(err)
|
|
||||||
|
|
||||||
# Convert to json
|
|
||||||
data = json.loads(r.text)
|
|
||||||
|
|
||||||
# Set up a json object (with better formatting) for all available bangs
|
|
||||||
bangs_data = {}
|
|
||||||
|
|
||||||
for row in data:
|
|
||||||
bang_command = '!' + row['t']
|
|
||||||
bangs_data[bang_command] = {
|
|
||||||
'url': row['u'].replace('{{{s}}}', '{}'),
|
|
||||||
'suggestion': bang_command + ' (' + row['s'] + ')'
|
|
||||||
}
|
|
||||||
|
|
||||||
json.dump(bangs_data, open(bangs_file, 'w'))
|
|
@ -0,0 +1,45 @@
|
|||||||
|
from cryptography.fernet import Fernet
|
||||||
|
from flask import current_app as app
|
||||||
|
|
||||||
|
REQUIRED_SESSION_VALUES = ['uuid', 'config', 'fernet_keys']
|
||||||
|
|
||||||
|
|
||||||
|
def generate_user_keys(cookies_disabled=False) -> dict:
|
||||||
|
"""Generates a set of user keys
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cookies_disabled: Flag for whether or not cookies are disabled by the
|
||||||
|
user. If so, the user can only use the default key
|
||||||
|
set generated on app init for queries.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: A new Fernet key set
|
||||||
|
|
||||||
|
"""
|
||||||
|
if cookies_disabled:
|
||||||
|
return app.default_key_set
|
||||||
|
|
||||||
|
# Generate/regenerate unique key per user
|
||||||
|
return {
|
||||||
|
'element_key': Fernet.generate_key(),
|
||||||
|
'text_key': Fernet.generate_key()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def valid_user_session(session: dict) -> bool:
|
||||||
|
"""Validates the current user session
|
||||||
|
|
||||||
|
Args:
|
||||||
|
session: The current Flask user session
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True/False indicating that all required session values are
|
||||||
|
available
|
||||||
|
|
||||||
|
"""
|
||||||
|
# Generate secret key for user if unavailable
|
||||||
|
for value in REQUIRED_SESSION_VALUES:
|
||||||
|
if value not in session:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
@ -1,24 +0,0 @@
|
|||||||
from cryptography.fernet import Fernet
|
|
||||||
from flask import current_app as app
|
|
||||||
|
|
||||||
REQUIRED_SESSION_VALUES = ['uuid', 'config', 'fernet_keys']
|
|
||||||
|
|
||||||
|
|
||||||
def generate_user_keys(cookies_disabled=False) -> dict:
|
|
||||||
if cookies_disabled:
|
|
||||||
return app.default_key_set
|
|
||||||
|
|
||||||
# Generate/regenerate unique key per user
|
|
||||||
return {
|
|
||||||
'element_key': Fernet.generate_key(),
|
|
||||||
'text_key': Fernet.generate_key()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def valid_user_session(session):
|
|
||||||
# Generate secret key for user if unavailable
|
|
||||||
for value in REQUIRED_SESSION_VALUES:
|
|
||||||
if value not in session:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
Loading…
Reference in New Issue