Skip to content

Instantly share code, notes, and snippets.

@eads
Created February 18, 2014 04:41
Show Gist options
  • Save eads/9064737 to your computer and use it in GitHub Desktop.
Save eads/9064737 to your computer and use it in GitHub Desktop.
import requests
from app import app
from flask import Response, request, render_template
from urlparse import urlparse, parse_qsl
import json
from cStringIO import StringIO
import unicodecsv
from time import time
from slughifi import slugify
API_KEY = 'XXXX'
EMAIL = '[email protected]'
BASE_URL = 'http://apiurl.com/v1/'
@app.route('/ical/')
def ical():
timestamp = int(time())
context = dict(request.args.items())
slug = slugify(context['SUMMARY'])
context['UID'] = "{0}-{1}@events.chicagotribune.com".format(slug, timestamp)
event = render_template('event.ics', **context)
ret = Response(response=event, mimetype='text/calendar')
ret.headers.add('Content-Disposition', 'attachment;filename=event-{0}-{1}.ics'.format(slug, timestamp))
return ret
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
"""Proxy connections to the API"""
url = "%s/%s" % (BASE_URL, path)
params = {"api_key": API_KEY}
args = dict(request.args.items())
format = args.pop('format', 'json')
callback = args.pop("callback", "processJSONP")
params.update(args)
response = requests.get(url, params=params)
if response.status_code != 200:
return Response(response="{'error':'There was an error in the API.'}",
status=response.status_code, mimetype='application/javascript')
data = response.json()
if format == 'jsonp':
ret = Response(response="%s(%s)" % (callback, json.dumps(data)),
status=200, mimetype='application/javascript')
else:
ret = Response(response=json.dumps(data),
status=200, mimetype='application/javascript')
ret.headers.add('Access-Control-Allow-Origin', 'whitelistedorigin.com')
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment