-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
312 lines (287 loc) · 16.9 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from flask import Flask, render_template, request, Response
import pandas as pd
import requests
from io import StringIO
from geopy.geocoders import Nominatim
from geotext import GeoText
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Hola'
geolocator = Nominatim(user_agent="example app")
tweets = []
locations = []
csv_contents = []
applied = []
source_applied = []
count = 0
number_images= 100
confidence= 0.9
alert = ""
source_applied = {'ID': "", 'source': "", 'keywords': ""}
@app.route('/', methods=['GET','POST'])
def index():
global count, applied, source_applied, number_images, tweets, csv_contents, confidence, alert, locations
if request.method == "POST":
if 'source_button' in request.form:
if count == 0:
option = request.form['source']
keywords = request.form['keywords']
number_images = request.form['number_pic']
r = requests.post('http://131.175.120.2:7777/Crawler/API/CrawlCSV',
json={'query': keywords,
'count': number_images})
if len(r.text) != 1:
source_applied = {'ID': count, 'source': option, 'keywords': keywords}
print(source_applied)
f = {'ID': "", 'Filter': "", 'Attribute': "", 'Confidence': 0.9}
applied.append(f)
tmp= StringIO(r.text)
df= pd.read_csv(tmp)
df['user_country'] = ""
u = []
for x in range(len(df)):
user_loc = df['user_loc'].iloc[x]
if pd.isna(user_loc) == False:
geolocated_text = geolocator.geocode(user_loc, timeout= 10)
if geolocated_text == None:
full_location_list = GeoText(user_loc).cities + GeoText(user_loc).countries
full_location = ''.join(str(e) for e in full_location_list)
geolocated_full_location = geolocator.geocode(full_location, timeout= 10)
if geolocated_full_location == None:
df['user_country'].iloc[x] = 'Not defined'
else:
df['user_country'].iloc[x] = geolocated_full_location.address.split(', ')[-1]
else:
df['user_country'].iloc[x] = geolocated_text.address.split(', ')[-1]
else:
df['user_country'].iloc[x] = 'Not defined'
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets.append(u)
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations.append(df_sorted['user_country'].astype(str).unique())
csv_string = df.to_csv(encoding= "utf-8")
csv_contents.append(csv_string)
count+=1
alert = ""
else:
alert = "Your search query did not return any images. Please try to either shorten the query or make use of the OR keyword to make some of the terms optional"
else:
option = request.form['source']
keywords = request.form['keywords']
number_images = request.form['number_pic']
r = requests.post('http://131.175.120.2:7777/Crawler/API/CrawlCSV',
json={'query': keywords,
'count': number_images})
if len(r.text) != 1:
source_applied = {'ID': 0, 'source': option, 'keywords': keywords}
print(source_applied)
tmp= StringIO(r.text)
df= pd.read_csv(tmp)
df['user_country'] = ""
u = []
for x in range(len(df)):
user_loc = df['user_loc'].iloc[x]
if pd.isna(user_loc) == False:
geolocated_text = geolocator.geocode(user_loc, timeout= 10)
if geolocated_text == None:
full_location_list = GeoText(user_loc).cities + GeoText(user_loc).countries
full_location = ''.join(str(e) for e in full_location_list)
geolocated_full_location = geolocator.geocode(full_location, timeout= 10)
if geolocated_full_location == None:
df['user_country'].iloc[x] = 'Not defined'
else:
df['user_country'].iloc[x] = geolocated_full_location.address.split(', ')[-1]
else:
df['user_country'].iloc[x] = geolocated_text.address.split(', ')[-1]
else:
df['user_country'].iloc[x] = 'Not defined'
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets[0]= u
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations[0] = df_sorted['user_country'].astype(str).unique()
csv_string = df.to_csv(encoding= "utf-8")
csv_contents[0]= csv_string
alert = ""
else:
alert = "Your search query did not return any images. Please try to either shorten the query or make use of the OR keyword to make some of the terms optional"
elif 'apply_button' in request.form:
if int(request.form['apply_button']) == count:
if request.form['Filter_select'] != "User location":
Filter = request.form['Filter_select']
if request.form['Filter_select'] == "Remove memes":
attribute = "MemeDetector"
elif request.form['Filter_select'] == "Scene detector":
attribute = request.form['option1_select']
elif request.form['Filter_select'] == "Contains object":
attribute = request.form['option2_select']
confidence = request.form['confidence']
params = {'filter_name_list': [attribute],
'confidence_threshold_list': [request.form['confidence']],
'column_name': 'media_url',
'csv_file': csv_contents[count-1]
}
r = requests.post(url='http://131.175.120.2:7777/Filter/API/FilterCSV', json=params)
if len(r.text) > 160:
f = {'ID': count, 'Filter': Filter, 'Attribute': attribute, 'Confidence': confidence}
k = {'ID': "", 'Filter': "", 'Attribute': "", 'Confidence': 0.9}
applied[count-1] = f
applied.append(k)
print(applied)
csv_contents.append(r.text)
tmp= StringIO(r.text)
df= pd.read_csv(tmp)
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations.append(df_sorted['user_country'].astype(str).unique())
u = []
for x in range(len(df)):
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets.append(u)
count+=1
alert = ""
else:
alert = "After running the above filter, no images remain. Either increase the number of images or change the filter."
elif request.form['Filter_select'] == "User location":
Filter = "User location"
attribute = request.form['option3_select']
f = {'ID': count, 'Filter': Filter, 'Attribute': attribute, 'Confidence': confidence}
k = {'ID': "", 'Filter': "", 'Attribute': "", 'Confidence': 0.9}
applied[count-1] = f
applied.append(k)
print(applied)
tmp = StringIO(csv_contents[count-1])
df0 = pd.read_csv(tmp)
df = df0.loc[df0['user_country'] == attribute]
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations.append(df_sorted['user_country'].astype(str).unique())
u = []
for x in range(len(df)):
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets.append(u)
csv_string = df.to_csv(encoding= "utf-8")
csv_contents.append(csv_string)
count+=1
alert = ""
else:
sel_count = int(request.form['apply_button'])
if request.form['Filter_select'] != "User location":
Filter = request.form['Filter_select']
if request.form['Filter_select'] == "Remove memes":
attribute = "MemeDetector"
elif request.form['Filter_select'] == "Scene detector":
attribute = request.form['option1_select']
elif request.form['Filter_select'] == "Contains object":
attribute = request.form['option2_select']
confidence = request.form['confidence']
params = {'filter_name_list': [attribute],
'confidence_threshold_list': [request.form['confidence']],
'column_name': 'media_url',
'csv_file': csv_contents[sel_count-1]
}
r = requests.post(url='http://131.175.120.2:7777/Filter/API/FilterCSV', json=params)
if len(r.text) > 160:
f = {'ID': sel_count, 'Filter': Filter, 'Attribute': attribute, 'Confidence': confidence}
applied[sel_count-1] = f
print(applied)
csv_contents[sel_count] = r.text
tmp= StringIO(r.text)
df= pd.read_csv(tmp)
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations[sel_count]= df_sorted['user_country'].astype(str).unique()
u = []
for x in range(len(df)):
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets[sel_count]= u
alert = ""
else:
alert = "After running the above filter, no images remain. Either increase the number of images or change the filter."
elif request.form['Filter_select'] == "User location":
Filter = "User location"
attribute = request.form['option3_select']
f = {'ID': sel_count, 'Filter': Filter, 'Attribute': attribute, 'Confidence': confidence}
applied[sel_count-1] = f
print(applied)
tmp = StringIO(csv_contents[sel_count-1])
df0 = pd.read_csv(tmp)
df = df0.loc[df0['user_country'] == attribute]
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations[sel_count]= df_sorted['user_country'].astype(str).unique()
u = []
for x in range(len(df)):
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets[sel_count]= u
csv_string = df.to_csv(encoding= "utf-8")
csv_contents[sel_count]= csv_string
alert = ""
elif 'reset_button' in request.form:
count = 0
source_applied = {'ID': "", 'source': "", 'keywords': ""}
applied = []
tweets = []
csv_contents = []
locations = []
alert = ""
elif 'up_button' in request.form:
sel_count = int(request.form['up_button'])
a = applied[sel_count-2]
applied[sel_count-2] = applied[sel_count-1]
applied[sel_count-1] = a
for a in range(count-sel_count+1):
if applied[sel_count-2+a]['Filter'] != "User location":
params = {'filter_name_list': [applied[sel_count-2+a]['Attribute']],
'confidence_threshold_list': [applied[sel_count-2+a]['Confidence']],
'column_name': 'media_url',
'csv_file': csv_contents[sel_count-2+a]
}
r = requests.post(url='http://131.175.120.2:7777/Filter/API/FilterCSV', json=params)
if len(r.text) > 160:
print(applied)
csv_contents[sel_count-1+a] = r.text
tmp= StringIO(r.text)
df= pd.read_csv(tmp)
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations[sel_count-1+a]= df_sorted['user_country'].astype(str).unique()
u = []
for x in range(len(df)):
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets[sel_count-1+a]= u
alert = ""
else:
alert = "After running the above filter, no images remain. Either increase the number of images or change the filter."
break
else:
tmp = StringIO(csv_contents[sel_count-2+a])
df0 = pd.read_csv(tmp)
df = df0.loc[df0['user_country'] == applied[sel_count-2+a]['Attribute']]
csv_string = df.to_csv(encoding= "utf-8")
if len(csv_string) > 160:
print(applied)
df_sorted = df.sort_values(by=['user_country'], ascending = True)
locations[sel_count-1+a]= df_sorted['user_country'].astype(str).unique()
u = []
for x in range(len(df)):
p = {"url": df['media_url'].iloc[x], "text": df['full_text'].iloc[x], "user_country": df['user_country'].iloc[x]}
u.append(p)
tweets[sel_count-1+a]= u
csv_contents[sel_count-1+a]= csv_string
alert = ""
else:
alert = "After running the above filter, no images remain. Either increase the number of images or change the filter."
break
return render_template('index.html', count=count, source_applied=source_applied, tweets=tweets,
applied=applied, alert=alert, locations=locations,
number_images=number_images, confidence=confidence)
@app.route("/downloadCSV")
def downloadCSV():
return Response(
csv_contents[int(request.args.get('id'))],
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=download.csv"})
if __name__ == '__main__':
app.run(debug=True)