forked from malevolentstrix/RealTime-TwitterAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (37 loc) · 1.28 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import ast
from collections import OrderedDict
from flask import Flask, jsonify, request
from flask import render_template
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__)
dataValues = []
categoryValues = []
tags = {}
def get_top_players(data, n=20):
"""Get top n players by score.
Returns a dictionary or an `OrderedDict` if `order` is true.
"""
top = sorted(data.items(), key=lambda x: x[1], reverse=True)[:n]
return OrderedDict(top)
@app.route("/")
def home():
return render_template('index.html', dataValues=dataValues, categoryValues=categoryValues)
@app.route('/refreshData')
def refresh_data():
global dataValues, categoryValues
return jsonify(dataValues=dataValues, categoryValues=categoryValues)
@app.route('/updateData', methods=['POST'])
def update_data():
global tags, dataValues, categoryValues
data = ast.literal_eval(request.data.decode("utf-8"))
tags[data['hashtag']] = data['count']
sorted_tags = get_top_players(tags)
categoryValues.clear()
dataValues.clear()
categoryValues = [x for x in sorted_tags]
dataValues = [tags[x] for x in sorted_tags]
return "success", 201
if __name__ == "__main__":
app.run(host='localhost', port=5001, debug=True)