Exemplo n.º 1
0
def stream_event():
    from service.commons import json_response
    res = dict()
    try:
        keywords = request.args.get('keywords')
        if keywords:
            keywords = keywords.split(",")
        else:
            res = {
                "status": "error",
                "message": "Please provide a few keyword(s) (comma-separated)",
                "example": "/stream?keywords=kw1,kw2,abc,xyz"
            }
            return json_response(res)
        from tweety import Tweety
        Tweety().filter(keywords=keywords)
        res['status'] = "success"
        res['message'] = "Started streaming tweets with keywords {}".format(
            keywords)

    except Exception as exc:
        res['status'] = "error"
        res['message'] = exc.message
        res['args'] = exc.args
    return json_response(res)
Exemplo n.º 2
0
def search_handler():
    es_size = int(request.args.get('size', 100))
    es_from = int(request.args.get('from', 0))
    data = json.loads(request.data)
    criteria = data.get('criteria')
    sort = data.get('sort')
    s = QueryBuilder(criteria).search(index='tweets_index', doc_type='tweet')
    if sort:
        s = s.sort(*sort)
    s = s[es_from:es_size]
    print "[QUERY]", QueryBuilder.get_raw_query(s)

    try:
        es_res = QueryBuilder.execute(s)
    except Exception as ex:
        res = {"status": "error", "message": ex.message, "args": ex.args}
        return json_response(res)
    res = dict()
    if es_res is not None:
        hits = es_res.hits
        res["count"] = {"total": hits.total, "fetched": len(hits.hits)}
        res["results"] = hits.hits
    return json_response(res)