-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
159 lines (127 loc) · 6.36 KB
/
app.py
File metadata and controls
159 lines (127 loc) · 6.36 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from flask import Flask, render_template, request, send_from_directory, flash
from internal.political_leaning.political_leaning_ml import ml_model
from analyse import analyse, analyse_account, compare_results
import os
import urllib
from markupsafe import Markup
resultlist = [None,None]
resultitem = None
compare=None
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Add a result to the list of results
# The current implementation only shows 2 results at a time for easy comparison
def add_to_resultlist(resultitem, resultlist):
if len(resultlist) >= 2:
resultlist.pop(0)
resultlist.append(resultitem)
# If nothing has been passed, display an empty html page
@app.route("/")
def hello():
return render_template('tabs/home.html', result=resultitem)
@app.route("/home")
def home():
return render_template('tabs/home.html', result=resultitem)
@app.route('/analyse_tweets')
def analyse_tweets():
resultlist[0] = None
resultlist[1] = None
return render_template('tabs/analyse_tweets.html', result=resultitem)
def analyse_err(msg):
flash(msg, 'error')
return render_template('tabs/analyse_tweets.html', result=None)
# If a request has been made, render the results on the page
@app.route('/analyse_tweets', methods=['POST'])
def analyseQuery():
term = request.form.get('twitter_query', '')
if len(term) == 0:
return analyse_err("You must add a search query")
country = request.form.get('countryDataset', 'global')
resultitem, err = analyse(term, country)
if resultitem == "noHashorAt":
return analyse_err("You must enter a #tag or @user, please try again")
elif resultitem == "noTweetsFound":
return analyse_err("No tweets found for this query, please try again")
if err != None:
flash("Analysing fewer than 20 tweets will lead to less accurate results. Only "+str(resultitem.tweetsetInfo.tweet_count)+" tweets analysed for "+str(resultitem.tweetsetInfo.term),'info')
return render_template('tabs/analyse_tweets.html', result=resultitem)
@app.route('/compare_tweets')
def compare():
resultitem = None
return render_template('tabs/compare_tweets.html', resultlist=resultlist, compare=None)
def compare_err(msg):
flash(msg, "error")
return render_template('tabs/compare_tweets.html', resultlist=resultlist, compare=None)
# If a request has been made, render the results on the page
@app.route('/compare_tweets', methods=['POST'])
def compareQuery():
term1 = request.form.get('twitter_query1', None)
term2 = request.form.get('twitter_query2', None)
country = request.form.get('countryDataset', 'global')
if len(term1) == 0:
if len(term2) == 0:
compare = None
return compare_err("You must add a search query in at least one of the input fields")
if len(term1) != 0:
result1, err = analyse(term1, country)
if err != None:
flash("Analysing fewer than 20 tweets will lead to less accurate results. Only "+str(result1.tweetsetInfo.tweet_count)+" tweets analysed for "+str(result1.tweetsetInfo.term))
if result1 == "invalidSearchQuery":
return compare_err(term1+" is not a valid Twitter hashtag or user handle, please try again")
elif result1 == "noHashorAt":
return compare_err("You must enter a #tag or @user in the first input field, please try again")
elif result1 == "noTweetsFound":
return compare_err("No tweets found for the query "+term1+", please try again")
resultlist[0] = result1
if len(term2) != 0:
result2, err = analyse(term2, country)
if err != None:
flash("Analysing fewer than 20 tweets will lead to less accurate results. Only "+str(result2.tweetsetInfo.tweet_count)+" tweets analysed for "+str(result2.tweetsetInfo.term))
if result2 == "invalidSearchQuery":
return compare_err(term2+" is not a valid Twitter hashtag or user handle, please try again")
elif result2 == "noHashorAt":
return compare_err("You must enter a #tag or @user in input second input field, please try again")
elif result2 == "noTweetsFound":
return compare_err("No tweets found for this query "+term2+", please try again")
resultlist[1] = result2
compare = compare_results(resultlist[0], resultlist[1], country)
return render_template('tabs/compare_tweets.html', resultlist=resultlist, compare=compare)
@app.route('/analyse_account')
def analyse_acc():
resultlist[0] = None
resultlist[1] = None
return render_template('tabs/analyse_account.html', result=resultitem)
def analyse_acc_err(msg):
flash(msg, 'error')
return render_template('tabs/analyse_account.html', result=None)
# If a request has been made, render the results on the page
@app.route('/analyse_account', methods=['POST'])
def analyse_acc_Query():
term = request.form.get('twitter_query', '')
if len(term) == 0:
return analyse_acc_err("You must add a search query")
country = request.form.get('countryDataset', 'global')
resultitem, err = analyse_account(term, country)
if err != None:
flash("Analysing fewer than 20 tweets will lead to less accurate results. Only "+str(resultitem.tweetsetInfo.tweet_count)+" tweets analysed for "+str(resultitem.tweetsetInfo.term),'info')
if resultitem == "noHashorAt":
return analyse_acc_err("You must enter a @user handle, please try again")
elif resultitem == "hashNotAt":
return analyse_acc_err("You must enter a @user handle, not a hashtag, please try again")
elif (resultitem == "noUserFound") | (resultitem == "invalidSearchQuery"):
return analyse_acc_err("No user found for the handle "+str(term)+" , please try again")
return render_template('tabs/analyse_account.html', result=resultitem)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),'favicon.ico', mimetype='image/vnd.microsoft.icon')
# This webapp runs on port 8081
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port=8081)
@app.template_filter('urlencode')
def urlencode_filter(s):
if type(s) == 'Markup':
s = s.unescape()
s = s.encode('utf8')
s = urllib.quote_plus(s)
return Markup(s)