Compare commits
3 Commits
09ea2535e0
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f4ec74e63d | |||
| a958ca3614 | |||
| 1caf4248d2 |
@@ -111,3 +111,4 @@ data.db.bak
|
||||
data/archive/*
|
||||
data/backup/*
|
||||
qotnews.sqlite
|
||||
praw.ini
|
||||
|
||||
+11
-16
@@ -10,6 +10,7 @@ SEARCH_ENABLED = bool(settings.MEILI_URL)
|
||||
|
||||
def meili_api(method, route, json=None, params=None, parse_json=True):
|
||||
try:
|
||||
headers = {'Authorization': 'Bearer ' + settings.MEILI_API_KEY}
|
||||
r = method(settings.MEILI_URL + route, json=json, params=params, timeout=4)
|
||||
if r.status_code > 299:
|
||||
raise Exception('Bad response code ' + str(r.status_code))
|
||||
@@ -28,24 +29,20 @@ def create_index():
|
||||
json = dict(uid='qotnews', primaryKey='id')
|
||||
return meili_api(requests.post, 'indexes', json=json)
|
||||
|
||||
def update_rankings():
|
||||
json = ['typo', 'words', 'proximity', 'date:desc', 'exactness']
|
||||
return meili_api(requests.post, 'indexes/qotnews/settings/ranking-rules', json=json)
|
||||
|
||||
def update_attributes():
|
||||
json = ['title', 'url', 'author']
|
||||
r = meili_api(requests.post, 'indexes/qotnews/settings/searchable-attributes', json=json)
|
||||
json = ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments']
|
||||
r = meili_api(requests.post, 'indexes/qotnews/settings/displayed-attributes', json=json)
|
||||
return r
|
||||
def update_settings():
|
||||
json = {
|
||||
'rankingRules': ['typo', 'words', 'proximity', 'date:desc', 'exactness'],
|
||||
'searchableAttributes': ['title', 'url', 'author'],
|
||||
'displayedAttributes': ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments'],
|
||||
}
|
||||
return meili_api(requests.post, 'indexes/qotnews/settings', json=json)
|
||||
|
||||
def init():
|
||||
if not SEARCH_ENABLED:
|
||||
logging.info('Search is not enabled, skipping init.')
|
||||
return
|
||||
print(create_index())
|
||||
update_rankings()
|
||||
update_attributes()
|
||||
update_settings()
|
||||
|
||||
def put_story(story):
|
||||
if not SEARCH_ENABLED: return
|
||||
@@ -53,13 +50,11 @@ def put_story(story):
|
||||
|
||||
def search(q):
|
||||
if not SEARCH_ENABLED: return []
|
||||
params = dict(q=q, limit=settings.FEED_LENGTH)
|
||||
r = meili_api(requests.get, 'indexes/qotnews/search', params=params, parse_json=False)
|
||||
json = dict(q=q, limit=settings.FEED_LENGTH)
|
||||
r = meili_api(requests.post, 'indexes/qotnews/search', json=json, parse_json=False)
|
||||
return r
|
||||
|
||||
if __name__ == '__main__':
|
||||
init()
|
||||
|
||||
print(update_rankings())
|
||||
|
||||
print(search('facebook'))
|
||||
|
||||
Reference in New Issue
Block a user