forked from jessepollak/squeak-day-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (51 loc) · 1.6 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
from flask import (
Flask,
render_template,
session,
redirect,
request,
url_for
)
import urllib
import requests
import json
from oauth import sign_url
app = Flask(__name__)
YELP_SEARCH_URL = 'http://api.yelp.com/v2/search'
@app.route('/')
def index():
# url args:
# http://localhost:5000/?data1=5&data2=7
# print request.args['data1']
return render_template('index.html')
@app.route('/results', methods=['POST'])
def results():
search_term = request.form['term']
location = request.form['location']
# will print search term in terminal
# print "search: %s" % search_term
# term and location are keys for the yelp api
# they just happen to be the same as what we had
# in the html file
data = {
'term': search_term,
'location': location
}
query_string = urllib.urlencode(data) # encode this data
# put the search terms in the url
api_url = YELP_SEARCH_URL + "?" + query_string
# authenticate the url with the yelp api
signed_url = sign_url(api_url)
# get the desired results from yelp
# requests is a library
# returns in the JSON format
response = requests.get(signed_url)
# get the fields from the json document:
json_response = json.loads(response.text)
return render_template('results.html',
search_term=search_term,
location=location,
businesses=json_response['businesses'])
# the businesses array in the json document
if __name__ == '__main__':
app.run(debug=True)