Initialization of the app now includes generation of a ddg-bang json file, which is used for all bang style searches afterwards. Also added search suggestion handling for bang json lookup. Queries beginning with "!" now reference the bang json file to pull all keys that match. Updated test suite to include basic tests for bang functionality. Updated gitignore to exclude bang subdir.
27 lines
686 B
Python
27 lines
686 B
Python
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'))
|