-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
76 lines (62 loc) · 2.5 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
import os
import foursquare
import flask
import json
import pymongo
from functools import wraps
from flask import Flask, request, current_app
from pymongo import Connection
app = Flask(__name__)
fb_client_id = "0VKLLPOXT2ZHPSMMO3XNJPVW43G2J1W33HR5SFW3GH0O10S1"
fb_client_secret = "TX5J4PEABTFNUPYB5TS0ED4F2DFRZSLJUGI0NWKBBDUSO3X0"
instagram_client_id = "0790cbf24ff84eb2ab0f57660dacc016"
instagram_client_secret = "055b5b7661564c2197c0b92d1105bbb1"
mongo_config = "mongodb://heroku_app8040801:ng7kdd6lg3jr9bos8fjrabu91f@ds037987.mongolab.com:37987/heroku_app8040801"
# TODO(madhav): get the mongo config from heroku:config
client = foursquare.Foursquare(client_id=fb_client_id,
client_secret=fb_client_secret,
redirect_uri='http://hidden-inlet-2627.herokuapp.com/callback')
db = Connection(host=mongo_config)[u'heroku_app8040801']
@app.route("/place/<location>")
def get_place(location):
func = jsonp(place_handler)
return func(location)
def place_handler(location):
app.logger.debug("GOT REQUEST FOR " + location)
output = json.dumps(get_4sq_place(location))
return {'data' : output}
@app.route("/instagram/callback")
def instagram_callback():
return "asd"
def jsonp(func):
"""Wraps JSONified output for JSONP requests."""
@wraps(func)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
data = str(func(*args, **kwargs)['data'])
content = str(callback) + '(' + data + ')'
mimetype = 'application/javascript'
return current_app.response_class(content, mimetype=mimetype)
else:
return func(*args, **kwargs)['data']
return decorated_function
def get_4sq_place(location):
places = client.venues.search(params={'near':location})
venues = []
for venue in places['venues']:
venue['_id'] = venue['id'] #maybe create a copy?
venues.append(venue)
#TODO(mbhagat): check if the id already exists and update it
places_collection = db['foursquare_places']
places_collection.insert(venues)
return places;
if __name__ == "__main__":
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
from werkzeug import SharedDataMiddleware
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/': os.path.join(os.path.dirname(__file__), 'client/app')
})
app.debug = True
app.run(host='0.0.0.0', port=port)