2020-06-27 22:53:39 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.DEBUG)
|
|
|
|
|
|
|
|
import requests
|
2022-03-05 21:58:35 +00:00
|
|
|
import settings
|
2020-06-27 22:53:39 +00:00
|
|
|
|
2022-03-05 21:58:35 +00:00
|
|
|
SEARCH_ENABLED = bool(settings.MEILI_URL)
|
2020-06-27 22:53:39 +00:00
|
|
|
|
2022-07-04 04:32:27 +00:00
|
|
|
def meili_api(method, route, json=None, params=None, parse_json=True):
|
2020-06-27 22:53:39 +00:00
|
|
|
try:
|
2022-03-05 21:58:35 +00:00
|
|
|
r = method(settings.MEILI_URL + route, json=json, params=params, timeout=4)
|
2021-09-06 00:20:21 +00:00
|
|
|
if r.status_code > 299:
|
2020-06-27 22:53:39 +00:00
|
|
|
raise Exception('Bad response code ' + str(r.status_code))
|
2022-07-04 04:32:27 +00:00
|
|
|
if parse_json:
|
|
|
|
return r.json()
|
|
|
|
else:
|
2022-07-04 05:55:52 +00:00
|
|
|
r.encoding = 'utf-8'
|
2022-07-04 04:32:27 +00:00
|
|
|
return r.text
|
2020-06-27 22:53:39 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except BaseException as e:
|
2021-09-06 00:20:21 +00:00
|
|
|
logging.error('Problem with MeiliSearch api route: %s: %s', route, str(e))
|
2020-06-27 22:53:39 +00:00
|
|
|
return False
|
|
|
|
|
2021-09-06 00:20:21 +00:00
|
|
|
def create_index():
|
|
|
|
json = dict(uid='qotnews', primaryKey='id')
|
|
|
|
return meili_api(requests.post, 'indexes', json=json)
|
|
|
|
|
2020-07-06 21:43:57 +00:00
|
|
|
def update_rankings():
|
2022-03-05 21:33:07 +00:00
|
|
|
json = ['typo', 'words', 'proximity', 'date:desc', 'exactness']
|
2021-09-06 00:20:21 +00:00
|
|
|
return meili_api(requests.post, 'indexes/qotnews/settings/ranking-rules', json=json)
|
2020-07-06 21:43:57 +00:00
|
|
|
|
|
|
|
def update_attributes():
|
2022-07-04 04:32:27 +00:00
|
|
|
json = ['title', 'url', 'author']
|
2021-09-06 00:20:21 +00:00
|
|
|
r = meili_api(requests.post, 'indexes/qotnews/settings/searchable-attributes', json=json)
|
2022-07-04 04:32:27 +00:00
|
|
|
json = ['id', 'ref', 'source', 'author', 'author_link', 'score', 'date', 'title', 'link', 'url', 'num_comments']
|
2022-03-05 21:33:07 +00:00
|
|
|
r = meili_api(requests.post, 'indexes/qotnews/settings/displayed-attributes', json=json)
|
2021-09-06 00:20:21 +00:00
|
|
|
return r
|
2020-07-06 21:43:57 +00:00
|
|
|
|
2020-06-27 22:53:39 +00:00
|
|
|
def init():
|
2022-03-05 21:58:35 +00:00
|
|
|
if not SEARCH_ENABLED:
|
|
|
|
logging.info('Search is not enabled, skipping init.')
|
|
|
|
return
|
2021-09-06 00:20:21 +00:00
|
|
|
print(create_index())
|
2020-07-06 21:43:57 +00:00
|
|
|
update_rankings()
|
|
|
|
update_attributes()
|
2020-06-27 22:53:39 +00:00
|
|
|
|
|
|
|
def put_story(story):
|
2022-03-05 21:58:35 +00:00
|
|
|
if not SEARCH_ENABLED: return
|
2022-07-04 04:32:27 +00:00
|
|
|
return meili_api(requests.post, 'indexes/qotnews/documents', [story])
|
2020-06-27 22:53:39 +00:00
|
|
|
|
|
|
|
def search(q):
|
2022-03-05 21:58:35 +00:00
|
|
|
if not SEARCH_ENABLED: return []
|
2022-07-04 06:05:33 +00:00
|
|
|
params = dict(q=q, limit=settings.FEED_LENGTH)
|
2022-07-04 04:32:27 +00:00
|
|
|
r = meili_api(requests.get, 'indexes/qotnews/search', params=params, parse_json=False)
|
|
|
|
return r
|
2020-06-27 22:53:39 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-09-06 00:20:21 +00:00
|
|
|
init()
|
2020-06-27 22:53:39 +00:00
|
|
|
|
2022-03-05 21:33:07 +00:00
|
|
|
print(update_rankings())
|
|
|
|
|
2022-07-04 04:32:27 +00:00
|
|
|
print(search('facebook'))
|