Created
September 15, 2014 13:55
-
-
Save informationsea/b2af8682385cc4a0718f to your computer and use it in GitHub Desktop.
Gist List view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.path.insert(0, 'flask.zip') | |
sys.path.insert(0, 'werkzeug.zip') | |
from flask import Flask, render_template, request, abort, redirect, url_for | |
import json | |
import urllib2 | |
import re | |
app = Flask(__name__) | |
githubuser_re = re.compile(r'\w[\w\-]*') | |
links_re = re.compile(r'<https://api.github.com/user/\d+/gists\?page=(\d+)>; rel="(\w+)"') | |
@app.route('/') | |
def index(): | |
username = request.args.get('username', None) | |
if username: | |
return redirect('/{0}'.format(username)) | |
return render_template('index.html', title='Hello') | |
@app.route('/<username>') | |
def show_gist(username): | |
if not githubuser_re.match(username): | |
abort(404) | |
page = request.args.get('page', '1') | |
if not page.isdigit(): | |
abort(404) | |
url = 'https://api.github.com/users/{0}/gists?page={1}'.format(username, page) | |
try: | |
f = urllib2.urlopen(url) | |
except urllib2.HTTPError as e: | |
abort(404) | |
gists = json.load(f) | |
links = {'next': None, 'last': None, 'prev': None, 'first': None} | |
for oneheader in f.info().headers: | |
if oneheader.startswith('Link: '): | |
for p, k in links_re.findall(oneheader): | |
links[k] = p | |
return render_template('listview.html', username=username, gists=gists, links=links) | |
if __name__ == '__main__': | |
app.debug = True | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment