bgneal@402: """ bgneal@402: Views for the podcast application. bgneal@402: bgneal@402: """ bgneal@143: import os.path bgneal@143: from urlparse import urlparse gremmie@1: gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext gremmie@1: from django.shortcuts import get_object_or_404 bgneal@402: import django.utils.simplejson as json gremmie@1: gremmie@1: from podcast.models import Channel gremmie@1: from podcast.models import Item gremmie@1: gremmie@1: bgneal@143: def get_ext_from_url(url): bgneal@143: """ bgneal@143: This function returns the extension part of the path from the given bgneal@143: url. bgneal@143: """ bgneal@402: ext = os.path.splitext(urlparse(url).path)[1] bgneal@402: if ext.startswith('.'): bgneal@402: ext = ext[1:] bgneal@402: return ext bgneal@402: bgneal@402: bgneal@402: def jplayer_params(ext, url, alt_ext, alt_url): bgneal@402: """ bgneal@402: Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied) bgneal@402: where bgneal@402: jplayer_media: a string representation of the JSON for the bgneal@402: jplayer setMedia parameter bgneal@402: jplayer_supplied: the string for the jplayer supplied parameter bgneal@402: bgneal@402: media_list is an input list or tuple of 2-tuples of the form bgneal@402: (media_type, url) bgneal@402: where media_type is e.g. mp3, m4a, ogg, etc. bgneal@402: bgneal@402: """ bgneal@402: media = dict([(ext, url)]) bgneal@402: if alt_ext and alt_url: bgneal@402: media[alt_ext] = alt_url bgneal@402: bgneal@402: # prefer mp4 to mp3 bgneal@402: if alt_ext is None: bgneal@402: supplied = [ext] bgneal@402: elif ext == "m4a": bgneal@402: supplied = (ext, alt_ext) bgneal@402: else: bgneal@402: supplied = (alt_ext, ext) bgneal@402: bgneal@402: supplied = ", ".join(supplied) bgneal@402: bgneal@402: return json.dumps(media), supplied bgneal@143: bgneal@143: gremmie@1: def index(request): gremmie@1: try: gremmie@1: channel = Channel.objects.get(pk=1) gremmie@1: except Channel.DoesNotExist: gremmie@1: channel = None gremmie@1: gremmie@1: return render_to_response('podcast/index.html', { bgneal@402: 'channel': channel, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: def detail(request, id): bgneal@143: podcast = get_object_or_404(Item.objects.select_related(), pk = id) bgneal@143: bgneal@143: ext = get_ext_from_url(podcast.enclosure_url) bgneal@143: alt_ext = None bgneal@143: if podcast.alt_enclosure_url: bgneal@143: alt_ext = get_ext_from_url(podcast.alt_enclosure_url) bgneal@143: bgneal@402: jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url, bgneal@402: alt_ext, podcast.alt_enclosure_url) bgneal@402: gremmie@1: return render_to_response('podcast/detail.html', { gremmie@1: 'channel': podcast.channel, bgneal@402: 'podcast': podcast, bgneal@143: 'ext': ext, bgneal@143: 'alt_ext': alt_ext, bgneal@402: 'jplayer_media': jplayer_media, bgneal@402: 'jplayer_supplied': jplayer_supplied, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) bgneal@402: gremmie@1: gremmie@1: def feed(request): gremmie@1: try: gremmie@1: channel = Channel.objects.get(pk=1) gremmie@1: except Channel.DoesNotExist: gremmie@1: channel = None gremmie@1: return render_to_response('podcast/feed.xml', { bgneal@402: 'channel': channel, gremmie@1: }, gremmie@1: context_instance = RequestContext(request))