Include option to disable search

This commit is contained in:
Tanner Collin 2022-03-05 21:58:35 +00:00
parent 55c282ee69
commit f8e8597e3a
2 changed files with 15 additions and 6 deletions

View File

@ -4,12 +4,13 @@ logging.basicConfig(
level=logging.DEBUG) level=logging.DEBUG)
import requests import requests
import settings
MEILI_URL = 'http://127.0.0.1:7700/' SEARCH_ENABLED = bool(settings.MEILI_URL)
def meili_api(method, route, json=None, params=None): def meili_api(method, route, json=None, params=None):
try: try:
r = method(MEILI_URL + route, json=json, params=params, timeout=4) r = method(settings.MEILI_URL + route, json=json, params=params, timeout=4)
if r.status_code > 299: if r.status_code > 299:
raise Exception('Bad response code ' + str(r.status_code)) raise Exception('Bad response code ' + str(r.status_code))
return r.json() return r.json()
@ -35,15 +36,20 @@ def update_attributes():
return r return r
def init(): def init():
if not SEARCH_ENABLED:
logging.info('Search is not enabled, skipping init.')
return
print(create_index()) print(create_index())
update_rankings() update_rankings()
update_attributes() update_attributes()
def put_story(story): def put_story(story):
if not SEARCH_ENABLED: return
to_add = dict(title=story['title'], id=story['id'], date=story['date']) to_add = dict(title=story['title'], id=story['id'], date=story['date'])
return meili_api(requests.post, 'indexes/qotnews/documents', [to_add]) return meili_api(requests.post, 'indexes/qotnews/documents', [to_add])
def search(q): def search(q):
if not SEARCH_ENABLED: return []
params = dict(q=q, limit=250) params = dict(q=q, limit=250)
r = meili_api(requests.get, 'indexes/qotnews/search', params=params) r = meili_api(requests.get, 'indexes/qotnews/search', params=params)
return r['hits'] return r['hits']

View File

@ -6,9 +6,14 @@
# set to 0 to disable that site # set to 0 to disable that site
NUM_HACKERNEWS = 15 NUM_HACKERNEWS = 15
NUM_LOBSTERS = 10 NUM_LOBSTERS = 10
NUM_REDDIT = 10 NUM_REDDIT = 15
NUM_TILDES = 5 NUM_TILDES = 5
# Meilisearch server URL
# Leave blank if not using search
#MEILI_URL = 'http://127.0.0.1:7700/'
MEILI_URL = ''
# Reddit account info # Reddit account info
# leave blank if not using Reddit # leave blank if not using Reddit
REDDIT_CLIENT_ID = '' REDDIT_CLIENT_ID = ''
@ -25,9 +30,7 @@ SUBREDDITS = [
'PhilosophyofScience', 'PhilosophyofScience',
'StateOfTheUnion', 'StateOfTheUnion',
'TheAgora', 'TheAgora',
'TrueFilm',
'TrueReddit', 'TrueReddit',
'UniversityofReddit',
'culturalstudies', 'culturalstudies',
'hardscience', 'hardscience',
'indepthsports', 'indepthsports',
@ -37,6 +40,6 @@ SUBREDDITS = [
'resilientcommunities', 'resilientcommunities',
'worldevents', 'worldevents',
'StallmanWasRight', 'StallmanWasRight',
'DarkFuturology',
'EverythingScience', 'EverythingScience',
'longevity',
] ]