-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
54 lines (40 loc) · 1.56 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
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from flask import Flask
from flask import render_template, request, redirect, url_for, jsonify, Response
from test import *
import json
app = Flask(__name__)
#_demo = ['corgi']
_demo = []
@app.route("/")
def hello():
return render_template('FlyZoom.html')
@app.route("/hashtag", methods=['POST'])
def hashtag():
hashtag_text = request.form.get('hashtag', None)
# if user input is from demo,
if hashtag_text in _demo:
with open('data/' + hashtag_text + '.txt') as f:
scores = [[0, 0], 0, [0, 0]]
for line in f:
json_form = {}
_tweet = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])" +\
" |(\w+:\/\/\S+)", " ", line).split())
analysis = TextBlob(_tweet)
if analysis.polarity>= 0:
scores[0][0] += 1 # increment positive
scores[0][1] += analysis.polarity# add score (+)
else:
scores[2][0] += 1 # increment negative
scores[2][1] += analysis.polarity# add score (-)
return jsonify(tweets=scores)
else:
api = TwitterClient()
tweets_dict = api.get_tweets(query = hashtag_text, lang='en', count=1000)
#tweets = [json.dumps(tweet) for tweet in tweets_dict]
#print json.dumps(tweets_dict)
return jsonify(tweets=tweets_dict)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)