querier.py 1.66 KB
Newer Older
Sacha's avatar
Sacha committed
1
# -*- coding: utf-8 -*-
2
3
from config import REDIS_HOST, REDIS_PORT
from scoring import controversy
4
from content import article_search, twitter_search
5
6
7
import db
import redis
import json
Sacha's avatar
Sacha committed
8
import datetime
Sacha's avatar
Sacha committed
9
10
from functools import partial
from operator import is_not
11
12
13

sr = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)

Sacha's avatar
Sacha committed
14
def perform(keyword, training=False):
Sacha's avatar
Sacha committed
15
    """Provide ``keyword`` for content retrieval, scoring.
16
    """
Sacha's avatar
Sacha committed
17
    articles = article_search(keyword, training=training)
18
    if len(articles) == 0:
19
20
        raise UsageError('no-articles', status_code=200)

21
22
    if training:
        res = {
Sacha's avatar
Sacha committed
23
24
25
            'articles': filter(partial(is_not, None),
                               map(lambda x: x.to_dict(),
                                   articles)),
26
27
28
29
30
31
32
            'kw_tweets': map(lambda x: x.to_dict(), 
                             twitter_search(keyword, training=training))
        }
    else:
        res = controversy(articles,
                          twitter_search(keyword, training=training),
                          _filter=~training)
Sacha's avatar
Sacha committed
33
    return {
34
35
        'training': training,
        'result': res,
Sacha's avatar
Sacha committed
36
37
        'ts': datetime.datetime.utcnow(),
        'keyword': keyword,
Sacha's avatar
Sacha committed
38
39
40
        'ok': 1
    }

Sacha's avatar
Sacha committed
41
42
43
44
45
46
47

def new_query(keyword):
    """Provide ``keyword`` for content retrieval,
    scoring, cache, and history entry.
    """
    ranked = perform(keyword)
    ranked['ts'] = ranked['ts'].isoformat()
48
49
    ranked_dump = json.dumps(ranked)
    sr.set(keyword, ranked_dump)
50
    # expire cache in 60 * 60 * 24 = 86400 seconds = 24 hours
51
    sr.expire(keyword, 86400)
Sacha's avatar
Sacha committed
52
    keyword_score = sum(a['score'] for a in ranked['result'])
53
    db.append_queries(keyword, keyword_score)
54
    return ranked_dump