forked from landakram/squeak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (86 loc) · 2.99 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
from flask import (
Flask,
render_template,
session,
redirect,
request,
url_for
)
from mongokit import Connection, Document, ObjectId
import datetime
from oauth import sign_url
import json
import requests
import urllib
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DEBUG = True
SECRET_KEY = 'development key'
YELP_SEARCH_URL = 'http://api.yelp.com/v2/search'
app = Flask(__name__)
app.config.from_object(__name__)
connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])
class Entry(Document):
use_dot_notation = True
structure = {
'name': basestring,
'url': basestring,
'created_at': datetime.datetime,
'phone_number': basestring,
'address': basestring,
'categories': [basestring]
}
default_values = {'created_at': datetime.datetime.utcnow}
def id(self):
return self._id
def __repr__(self):
return '<Entry %s>' % self['name']
connection.register([Entry])
collection = connection['squeak'].entries
@app.route('/')
def index():
entries = list(collection.Entry.find())
return render_template('index.html', saved_entries=entries)
@app.route('/save', methods=['POST'])
def save_entry():
new_entry = collection.Entry()
new_entry.name = request.form['name']
new_entry.url = request.form['url']
new_entry.phone_number = request.form['phone_number']
new_entry.address = request.form['address']
new_entry.categories = request.form.getlist('categories')
new_entry.save()
return redirect(url_for('index'))
@app.route('/results', methods=['POST'])
def yelp_search():
search_term = request.form['term']
location = request.form['location']
data = {
'term': search_term,
'location': location
}
query_string = urllib.urlencode(data)
api_url = '%s?%s' % (app.config['YELP_SEARCH_URL'], query_string)
signed_url = sign_url(api_url)
response = requests.get(signed_url)
json_response = json.loads(response.text)
return render_template('results.html',
search_term=search_term,
location=location,
businesses=json_response['businesses'])
def create_oauth_url(url):
consumer = oauth.Consumer(app.config['OAUTH_CONSUMER_KEY'],
app.config['OAUTH_CONSUMER_SECRET'])
token = oauth.Token(app.config['OAUTH_TOKEN'],
app.config['OAUTH_TOKEN_SECRET'])
oauth_request = oauth.Request('GET', url, {})
oauth_request.update({'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': oauth.generate_timestamp(),
'oauth_token': token.key,
'oauth_consumer_key': app.config['OAUTH_CONSUMER_KEY']})
oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(),
consumer,
token)
return oauth_request.to_url()
if __name__ == '__main__':
app.run()