-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
124 lines (102 loc) · 3.57 KB
/
server.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
from flask import Flask, render_template, request, redirect
from app.measure import create_process, read_proc_results, measure
# from measure.py file, import these functions
from app.bingSearch import search # from bingSearch.py import search() functions
from twilio import twiml
from app.model import Result, connect_to_db, db #database stuff
from datetime import datetime
from sqlalchemy import update
import sqlite3
conn = sqlite3.connect('datauseresult.db') #connects to database called 'websizeresult'
app = Flask(__name__)
@app.route('/')
def bingSearch():
return render_template('bingsearchhome.html')
@app.route('/result')
def bingResult():
bing_input = request.args.get("bing_input")
# bing_input is unicode
search_input = str(bing_input)
# if user doesn't type anything in search box
if search_input == "":
return redirect('/')
search_list = search(search_input, 5)
result_objects = []
page_data_structure_2 = []
for s in search_list:
result = Result.query.filter_by(url=s.url).first()
# if result is not True (meaning it has a query)
if result:
result_objects.append(result)
else:
page_data_structure_2.append({
'title': s.title,
'url': s.url,
'description': s.description,
})
url_list = [page_data['url'] for page_data in page_data_structure_2]
# url_list = []
# for page_data in page_data_structure_2:
# url_list.append(page_data['url'])
print "measuring", url_list
# measure the list of URLs after 20 seconds
measured_url_list = measure(url_list, "20000")
for i in range(len(page_data_structure_2)):
page_data_structure_2[i]['dsize'] = measured_url_list[i]
# adds results to database
for page_data in page_data_structure_2:
bing_result_to_db = Result(url=page_data['url'], title=page_data['title'],
size=page_data['dsize'], datetime=datetime.utcnow(),
description=page_data['description'])
db.session.add(bing_result_to_db)
# **appends results to db**
result_objects.append(bing_result_to_db)
# then commits to database
db.session.commit()
return render_template('bingresult.html', results=result_objects)
@app.route('/twilioTest', methods=['POST'])
def twilioTest():
text_body = request.values.get('Body')
result_of_search_text_input = search(text_body, 3)
print result_of_search_text_input
result_objects = []
twilio_foo = []
for each in result_of_search_text_input:
result = Result.query.filter_by(url=each.url).first()
if result:
result_objects.append(result)
else:
twilio_foo.append({
'title': each.title,
'url': each.url,
'description': each.description
})
url_list = [i['url'] for i in twilio_foo]
# url_list = []
# for i in twilio_foo:
# url_list.append(i['url'])
print "measuring", url_list
# measure the list of URLs after 9 seconds
measured_url_list = measure(url_list, "9000")
for i in range(len(twilio_foo)):
twilio_foo[i]['dsize'] = measured_url_list[i]
for each in twilio_foo:
bing_result_to_db = Result(url=each['url'], size=each['dsize'],
datetime=datetime.utcnow(), description=each['description'])
db.session.add(bing_result_to_db)
result_objects.append(bing_result_to_db)
db.session.commit()
result_message_to_user = ""
for each in result_objects:
result_message_to_user += each.url + " "
result_message_to_user += each.size + "\n"
# See message before sending SMS
print result_message_to_user
resp = twiml.Response()
resp.message(result_message_to_user)
return str(resp)
if __name__ == '__main__':
# debug=True gives us error messages in the browser and also "reloads" our web app
# if we change the code.
connect_to_db(app)
app.run(host='0.0.0.0', port=80)