/
server.py
180 lines (143 loc) · 4.98 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
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
'''
foowoo application
J. Thomas
'''
import os
from flask import Flask,jsonify
from flask import render_template
from pymongo import MongoClient
from yelp import Yelp
import logging
import logging.handlers
app = Flask(__name__)
#
#setup logger
#
foo_logger = logging.getLogger('MyLogger')
foo_logger.setLevel(logging.INFO)
log_handler = logging.handlers.SysLogHandler(address = '/dev/log') # we are on Linux
foo_logger.addHandler(log_handler)
#
# configure and instantiate Yelp Object
#
app.config.from_pyfile('config.py', silent=True)
app.config.from_envvar('foowoo_config', silent=True)
#check local environment for config data
if os.getenv('CONSUMER_KEY'):
app.config['CONSUMER_KEY']= os.getenv('CONSUMER_KEY')
if os.getenv('CONSUMER_SECRET'):
app.config['CONSUMER_SECRET'] = os.getenv('CONSUMER_SECRET')
if os.getenv('TOKEN'):
app.config['TOKEN'] = os.getenv('TOKEN')
if os.getenv('TOKEN_SECRET'):
app.config['TOKEN_SECRET'] = os.getenv('TOKEN_SECRET')
if os.getenv('MONGO_URI'):
app.config['MONGO_URI'] = os.getenv('MONGO_URI')
if os.getenv('MONGO_PORT'):
app.config['MONGO_PORT'] = int(os.getenv('MONGO_PORT'))
if os.getenv('MONGO_USER'):
app.config['MONGO_USER'] = os.getenv('MONGO_USER')
if os.getenv('MONGO_PASSWORD'):
app.config['MONGO_PASSWORD'] = os.getenv('MONGO_PASSWORD')
yelp = Yelp(app.config['CONSUMER_KEY'],app.config['CONSUMER_SECRET'],app.config['TOKEN'],app.config['TOKEN_SECRET'])
#
# connect to MongoDB
#
connection = MongoClient(app.config['MONGO_URI'],app.config['MONGO_PORT'])
if app.config['MONGO_USER']!='':
connection.nyc.authenticate(app.config['MONGO_USER'], app.config['MONGO_PASSWORD'])
db = connection.nyc # attach to db
collection = db.restaurants # specify the colllection
foo_logger.info('Connected to MongoDB at %s on PORT %s',
app.config['MONGO_URI'],app.config['MONGO_PORT'])
#
# use this to page through result sets
#
def pager(someCursor,page):
# process cursor
json_docs = []
pagination = []
# get the entire result set
for doc in someCursor:
json_docs.append(doc)
# return the requested page (10 items per page)
for i in range((page-1)*10,page*10):
try:
# don't let building number be a integer
json_docs[i]['building'] = str(json_docs[i]['building'])
# the city uses 'Jewish/Kosher', but Yelp uses 'Kosher'
if json_docs[i]['cusine_description'] == 'Jewish/Kosher' :
json_docs[i]['cusine_description'] = 'Kosher'
pagination.append(json_docs[i])
except:
break
return pagination
@app.route('/')
def index():
return render_template('index.html')
'''
GET restaurant listing by restaurant name
'''
@app.route('/name/<name>/<int:page>')
def getByName(name,page):
restaurantName = name.upper()
if (restaurantName == 'undefined'):
return ""
print "show name listing for " + restaurantName
# search for restaurant name in MongoDB
cursor = collection.find({'dba':{'$regex':'^' + restaurantName}},projection={'_id': 0},sort=[('dba',1)])
# get a "page" full of results
pageData = pager(cursor,page)
return jsonify({'total_docs': cursor.count(), 'results' : pageData})
'''
GET restaurant listing by borough
'''
@app.route('/boro/<boroName>/<int:page>')
def getByBoro(boroName,page):
if (boroName == 'undefined'):
return ""
print "show boro listing for " + boroName
# search for boro in MongoDB
cursor = collection.find({'boro_name':boroName },projection={'_id': 0},sort=[('dba',1)])
# get a "page" full of results
pageData = pager(cursor,page)
return jsonify({'total_docs': cursor.count(), 'results' : pageData})
'''
GET restaurant listing by cusine/borough
'''
@app.route('/cusine/<cusine>/<boroName>/<int:page>')
def getByCusineBoro(cusine,boroName,page):
if (boroName == 'undefined'):
return ""
if (cusine == 'undefined'):
return ""
print "show cusine listing for " + cusine + " in " + boroName
# search for cusine/boro in MongoDB
cursor = collection.find({'boro_name':boroName, 'cusine_description': cusine },\
projection={'_id': False},sort=[('dba',1)])
# get a "page" full of results
pageData = pager(cursor,page)
return jsonify({'total_docs': cursor.count(), 'results' : pageData})
'''
call Yelp object to fetch restaurant data
'''
@app.route('/yelp/<address>/<phone>/<cusine>')
def getYelpData(address,phone,cusine):
arg = 'location=' + address + '&telephone=' + phone + '&term=' + cusine
result = yelp.getData(arg)
yelpData=""
places = result['businesses'];
for place in places:
if (place.get('phone') == phone):
yelpData = place
break;
return jsonify(yelpData)
'''
show Yelp data in a modal window
'''
@app.route('/yelpmodal.html')
def showModal():
return render_template('yelpmodal.html')
if __name__ == '__main__':
#app.run(debug=True)
app.run(host='0.0.0.0')