2021-03-08 20:22:04 +03:00
|
|
|
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'))
|
2022-01-25 22:28:06 +03:00
|
|
|
print('* Finished creating ddg bangs json')
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
|
2022-03-01 22:06:59 +03:00
|
|
|
def resolve_bang(query: str, bangs_dict: dict, fallback: str) -> str:
|
2021-03-08 20:22:04 +03:00
|
|
|
"""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
|
|
|
|
|
|
|
|
"""
|
2021-09-28 04:39:58 +03:00
|
|
|
# Ensure bang search is case insensitive
|
|
|
|
query = query.lower()
|
2021-03-08 20:22:04 +03:00
|
|
|
split_query = query.split(' ')
|
|
|
|
for operator in bangs_dict.keys():
|
2021-10-28 21:39:33 +03:00
|
|
|
if operator not in split_query \
|
|
|
|
and operator[1:] + operator[0] not in split_query:
|
2021-03-08 20:22:04 +03:00
|
|
|
continue
|
2022-03-01 22:06:59 +03:00
|
|
|
|
|
|
|
bang_query = query.replace(
|
2022-03-01 22:13:40 +03:00
|
|
|
operator if operator in split_query else operator[1:] +
|
|
|
|
operator[0], ''
|
2022-03-01 22:06:59 +03:00
|
|
|
).strip()
|
|
|
|
|
|
|
|
if bang_query:
|
|
|
|
return bangs_dict[operator]['url'].replace('{}', bang_query, 1)
|
|
|
|
else:
|
|
|
|
return fallback
|
2021-03-08 20:22:04 +03:00
|
|
|
return ''
|