Skip to content

Instantly share code, notes, and snippets.

@Apreche
Created September 4, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apreche/3622666 to your computer and use it in GitHub Desktop.
Save Apreche/3622666 to your computer and use it in GitHub Desktop.
github hosted shownotes
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