|
1
|
1 |
#!/usr/bin/env python |
|
|
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
|
|
|
4 |
from datetime import datetime, timedelta |
|
|
5 |
from flask import Response, request #@UnresolvedImport |
|
|
6 |
from flaskext.sqlalchemy import get_debug_queries |
|
|
7 |
from iri_tweet.models import TweetSource, Tweet |
|
|
8 |
from pyrfc3339.generator import generate |
|
|
9 |
from sqlalchemy import func, desc #@UnresolvedImport |
|
|
10 |
from sqlalchemy.orm import joinedload |
|
|
11 |
from tweetgserver import app, db, cache |
|
|
12 |
import pytz |
|
|
13 |
import simplejson as json #@UnresolvedImport |
|
|
14 |
import re |
|
|
15 |
import math |
|
|
16 |
|
|
|
17 |
def jsonpwrap(resobj): |
|
|
18 |
resp_str = json.dumps(resobj) |
|
|
19 |
callback_param = request.args.get("callback") |
|
|
20 |
if callback_param: |
|
|
21 |
resp_str = "%s( %s );" % (callback_param, resp_str) |
|
|
22 |
mime_type = 'text/javascript' |
|
|
23 |
else: |
|
|
24 |
mime_type = 'application/json' |
|
|
25 |
return Response(resp_str, mimetype=mime_type) |
|
|
26 |
|
|
|
27 |
@app.route('/', methods=['GET']) |
|
|
28 |
def index(): |
|
|
29 |
query = db.session.query(func.count(TweetSource.id)) #@UndefinedVariable |
|
|
30 |
ts_list_count = query.scalar() |
|
|
31 |
return 'Nb of tweets ' + str(ts_list_count) |
|
|
32 |
|
|
|
33 |
@app.route('/total', methods=['GET']) |
|
|
34 |
def total(): |
|
|
35 |
query = db.session.query(func.count(TweetSource.id)) #@UndefinedVariable |
|
|
36 |
ts_list_count = query.scalar() |
|
|
37 |
return jsonpwrap({"total":ts_list_count}) |
|
|
38 |
|
|
|
39 |
@app.route('/podium/<tokens>', methods=['GET']) |
|
|
40 |
def podium(tokens): |
|
|
41 |
token_list = tokens.split(",") |
|
|
42 |
query_base = db.session.query(func.count(Tweet.id)) #@UndefinedVariable |
|
|
43 |
podium_res = {} |
|
|
44 |
for token in token_list: |
|
|
45 |
query = query_base.filter(Tweet.text.op('~*')(token)) #@UndefinedVariable |
|
|
46 |
podium_res[token] = query.scalar() |
|
|
47 |
res = { |
|
|
48 |
"podium" : podium_res, |
|
|
49 |
"total" : query_base.scalar() |
|
|
50 |
} |
|
|
51 |
return jsonpwrap(res) |
|
|
52 |
|