def index(): auth=FoursquareAuth(client_id, client_secret) foursq=Foursquare(auth) venues=None error=None lat='' lon='' if request.method == 'POST': lat=request.form.get('lat', '') lon=request.form.get('lon', '') radius=request.form.get('radius', 500) cid=request.form.get('cid', category_id) latlon='%s.%s,%s.%s' % (lat.split('.')[0], lat.split('.')[1], lon.split('.')[0], lon.split('.')[1],) try: res=foursq.request('venues', aspect='search', ll=latlon, radius=radius, categoryId=cid) venues=res['response']['venues'] except Exception, e: error='Connection error - %s ' % e
def get_checkins(): key = "checkins-%s" % session["user"] checkins = cache.get(key) if checkins: return checkins fsq = Foursquare(session["access_token"]) raw_checkins = fsq.checkins() items = raw_checkins["items"] checkins = {"items": {}} center = {"lon": 0, "lat": 0} for item in items: venue = item["venue"] if not venue["id"] in checkins["items"]: checkins["items"][venue["id"]] = { "name": venue["name"], "location": { "lon": venue["location"]["lng"], "lat": venue["location"]["lat"] }, "count": 1 } else: checkins["items"][venue["id"]]["count"] += 1 center["lon"] += venue["location"]["lng"] center["lat"] += venue["location"]["lat"] center["lon"] /= len(items) center["lat"] /= len(items) checkins["center"] = center cache.set(key, checkins, timeout=60*60) return checkins
def get_user(): fsq = Foursquare(session["access_token"]) user = fsq.users() if user.get("lastName"): name = user["lastName"] + " " + user["firstName"] else: name = user["firstName"] photo = user["photo"]["prefix"] + "20x20" + user["photo"]["suffix"] result = { "name": name, "photo": photo, "bio": user["bio"], "homeCity": user["homeCity"] } return result
def authenticate(): callback = url_for("authenticate", _external=True) code = request.args["code"] access_token = Foursquare.get_access_token( CLIENT_ID, CLIENT_SECRET, callback, code ) session["access_token"] = access_token session["user"] = get_user() session["is_login"] = True return redirect(url_for("index"))
def index(): if os.environ["FSQTOKEN"]: fsq = Foursquare(os.environ["FSQTOKEN"]) checkins = fsq.checkins() return render_template("index.html", token=os.environ["FSQTOKEN"], checkins=checkins)
def login(): callback = url_for("authenticate", _external=True) redirect_to = Foursquare.authenticate_url(CLIENT_ID, callback) return redirect(redirect_to)