Compare commits

..

No commits in common. "7bdbbf10b208364f54006ceeae42e9f7b0ad6202" and "bf3663bbecd71bce595ff67eff3f4132b55b1010" have entirely different histories.

6 changed files with 31 additions and 17 deletions

View File

@ -59,7 +59,8 @@ if __name__ == '__main__':
story = database.get_story(sid) story = database.get_story(sid)
print('Indexing {}/{} id: {} title: {}'.format(count, num_stories, sid[0], story.title)) print('Indexing {}/{} id: {} title: {}'.format(count, num_stories, sid[0], story.title))
story_obj = json.loads(story.meta_json) story_obj = json.loads(story.meta_json)
stories.append(story_obj) to_add = dict(title=story_obj['title'], id=story_obj['id'], date=story_obj['date'])
stories.append(to_add)
count += 1 count += 1
res = put_stories(stories) res = put_stories(stories)

View File

@ -8,15 +8,12 @@ import settings
SEARCH_ENABLED = bool(settings.MEILI_URL) SEARCH_ENABLED = bool(settings.MEILI_URL)
def meili_api(method, route, json=None, params=None, parse_json=True): def meili_api(method, route, json=None, params=None):
try: try:
r = method(settings.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))
if parse_json: return r.json()
return r.json()
else:
return r.text
except KeyboardInterrupt: except KeyboardInterrupt:
raise raise
except BaseException as e: except BaseException as e:
@ -32,9 +29,9 @@ def update_rankings():
return meili_api(requests.post, 'indexes/qotnews/settings/ranking-rules', json=json) return meili_api(requests.post, 'indexes/qotnews/settings/ranking-rules', json=json)
def update_attributes(): def update_attributes():
json = ['title', 'url', 'author'] json = ['title']
r = meili_api(requests.post, 'indexes/qotnews/settings/searchable-attributes', json=json) 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'] json = ['id']
r = meili_api(requests.post, 'indexes/qotnews/settings/displayed-attributes', json=json) r = meili_api(requests.post, 'indexes/qotnews/settings/displayed-attributes', json=json)
return r return r
@ -48,17 +45,18 @@ def init():
def put_story(story): def put_story(story):
if not SEARCH_ENABLED: return if not SEARCH_ENABLED: return
return meili_api(requests.post, 'indexes/qotnews/documents', [story]) to_add = dict(title=story['title'], id=story['id'], date=story['date'])
return meili_api(requests.post, 'indexes/qotnews/documents', [to_add])
def search(q): def search(q):
if not SEARCH_ENABLED: return [] 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, parse_json=False) r = meili_api(requests.get, 'indexes/qotnews/search', params=params)
return r return r['hits']
if __name__ == '__main__': if __name__ == '__main__':
init() init()
print(update_rankings()) print(update_rankings())
print(search('facebook')) print(search('qot'))

View File

@ -56,8 +56,10 @@ def apisearch():
if len(q) >= 3: if len(q) >= 3:
results = search.search(q) results = search.search(q)
else: else:
results = '[]' results = []
res = Response(results) story_metas = [database.get_story(x['id']).meta_json for x in results]
# hacky nested json
res = Response('{"results":[' + ','.join(story_metas) + ']}')
res.headers['content-type'] = 'application/json' res.headers['content-type'] = 'application/json'
return res return res

View File

@ -56,7 +56,20 @@ class App extends React.Component {
<Router> <Router>
<div className='container menu'> <div className='container menu'>
<p> <p>
<Link to='/'>QotNews</Link> <Switch>
<Route path='/' exact>
<Link to='/'>QotNews - Feed</Link>
</Route>
<Route path='/search'>
<Link to='/'>QotNews - Search</Link>
</Route>
<Route path='/:id' exact>
<Link to='/'>QotNews - Article</Link>
</Route>
<Route path='/:id/c'>
<Link to='/'>QotNews - Comments</Link>
</Route>
</Switch>
<span className='theme'>Theme: <a href='#' onClick={() => this.light()}>Light</a> - <a href='#' onClick={() => this.dark()}>Dark</a></span> <span className='theme'>Theme: <a href='#' onClick={() => this.light()}>Light</a> - <a href='#' onClick={() => this.dark()}>Dark</a></span>
<br /> <br />

View File

@ -29,7 +29,7 @@ class Results extends React.Component {
.then(res => res.json()) .then(res => res.json())
.then( .then(
(result) => { (result) => {
this.setState({ stories: result.hits }); this.setState({ stories: result.results });
}, },
(error) => { (error) => {
if (error.message !== 'The operation was aborted. ') { if (error.message !== 'The operation was aborted. ') {

View File

@ -37,7 +37,7 @@ class Search extends Component {
<span className='search'> <span className='search'>
<form onSubmit={this.searchAgain}> <form onSubmit={this.searchAgain}>
<input <input
placeholder='Search...' placeholder='Search... (fixed)'
value={search} value={search}
onChange={this.searchArticles} onChange={this.searchArticles}
ref={this.inputRef} ref={this.inputRef}