Created
September 4, 2012 15:52
-
-
Save Apreche/3622666 to your computer and use it in GitHub Desktop.
github hosted shownotes
This file contains hidden or 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 urllib2 | |
import base64 | |
from django.utils import simplejson | |
def get_file_contents(user, repo, path): | |
url = "https://api.github.com/repos/%s/%s/contents/%s" % (user, repo, path) | |
try: | |
response = urllib2.urlopen(url) | |
except urllib2.HTTPError: | |
return None | |
json = simplejson.loads(response.read()) | |
return unicode(base64.decodestring(json['content'])) | |
# example view | |
def episode_detail(request, showid, slug=None): | |
# ... | |
path = "podcasts/%s/%s.html" % (episode.show.slug, episode.slug) | |
cache_key = "shownotes_%s" % path | |
shownotes = cache.get(cache_key) | |
if shownotes is None: | |
user = getattr(settings, 'GITHUB_USER') # Apreche | |
repo = getattr(settings, 'GITHUB_REPO') # FRC-Shownotes | |
shownotes = get_file_contents(user, repo, path) | |
if shownotes is not None: | |
cache.set(cache_key, shownotes, 600) | |
context.update({'shownotes': shownotes or '' }) | |
# ... | |
# template example | |
# <div id="shownotes">{{ shownotes }}</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment