bgneal@402
|
1 """
|
bgneal@402
|
2 Views for the podcast application.
|
bgneal@402
|
3
|
bgneal@402
|
4 """
|
bgneal@143
|
5 import os.path
|
bgneal@143
|
6 from urlparse import urlparse
|
gremmie@1
|
7
|
gremmie@1
|
8 from django.shortcuts import render_to_response
|
gremmie@1
|
9 from django.template import RequestContext
|
gremmie@1
|
10 from django.shortcuts import get_object_or_404
|
bgneal@402
|
11 import django.utils.simplejson as json
|
gremmie@1
|
12
|
gremmie@1
|
13 from podcast.models import Channel
|
gremmie@1
|
14 from podcast.models import Item
|
gremmie@1
|
15
|
gremmie@1
|
16
|
bgneal@143
|
17 def get_ext_from_url(url):
|
bgneal@143
|
18 """
|
bgneal@143
|
19 This function returns the extension part of the path from the given
|
bgneal@143
|
20 url.
|
bgneal@143
|
21 """
|
bgneal@402
|
22 ext = os.path.splitext(urlparse(url).path)[1]
|
bgneal@402
|
23 if ext.startswith('.'):
|
bgneal@402
|
24 ext = ext[1:]
|
bgneal@402
|
25 return ext
|
bgneal@402
|
26
|
bgneal@402
|
27
|
bgneal@402
|
28 def jplayer_params(ext, url, alt_ext, alt_url):
|
bgneal@402
|
29 """
|
bgneal@402
|
30 Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied)
|
bgneal@402
|
31 where
|
bgneal@402
|
32 jplayer_media: a string representation of the JSON for the
|
bgneal@402
|
33 jplayer setMedia parameter
|
bgneal@402
|
34 jplayer_supplied: the string for the jplayer supplied parameter
|
bgneal@402
|
35
|
bgneal@402
|
36 media_list is an input list or tuple of 2-tuples of the form
|
bgneal@402
|
37 (media_type, url)
|
bgneal@402
|
38 where media_type is e.g. mp3, m4a, ogg, etc.
|
bgneal@402
|
39
|
bgneal@402
|
40 """
|
bgneal@402
|
41 media = dict([(ext, url)])
|
bgneal@402
|
42 if alt_ext and alt_url:
|
bgneal@402
|
43 media[alt_ext] = alt_url
|
bgneal@402
|
44
|
bgneal@402
|
45 # prefer mp4 to mp3
|
bgneal@402
|
46 if alt_ext is None:
|
bgneal@402
|
47 supplied = [ext]
|
bgneal@402
|
48 elif ext == "m4a":
|
bgneal@402
|
49 supplied = (ext, alt_ext)
|
bgneal@402
|
50 else:
|
bgneal@402
|
51 supplied = (alt_ext, ext)
|
bgneal@402
|
52
|
bgneal@402
|
53 supplied = ", ".join(supplied)
|
bgneal@402
|
54
|
bgneal@402
|
55 return json.dumps(media), supplied
|
bgneal@143
|
56
|
bgneal@143
|
57
|
gremmie@1
|
58 def index(request):
|
gremmie@1
|
59 try:
|
gremmie@1
|
60 channel = Channel.objects.get(pk=1)
|
gremmie@1
|
61 except Channel.DoesNotExist:
|
gremmie@1
|
62 channel = None
|
gremmie@1
|
63
|
gremmie@1
|
64 return render_to_response('podcast/index.html', {
|
bgneal@402
|
65 'channel': channel,
|
gremmie@1
|
66 },
|
gremmie@1
|
67 context_instance = RequestContext(request))
|
gremmie@1
|
68
|
gremmie@1
|
69
|
gremmie@1
|
70 def detail(request, id):
|
bgneal@143
|
71 podcast = get_object_or_404(Item.objects.select_related(), pk = id)
|
bgneal@143
|
72
|
bgneal@143
|
73 ext = get_ext_from_url(podcast.enclosure_url)
|
bgneal@143
|
74 alt_ext = None
|
bgneal@143
|
75 if podcast.alt_enclosure_url:
|
bgneal@143
|
76 alt_ext = get_ext_from_url(podcast.alt_enclosure_url)
|
bgneal@143
|
77
|
bgneal@402
|
78 jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url,
|
bgneal@402
|
79 alt_ext, podcast.alt_enclosure_url)
|
bgneal@402
|
80
|
gremmie@1
|
81 return render_to_response('podcast/detail.html', {
|
gremmie@1
|
82 'channel': podcast.channel,
|
bgneal@402
|
83 'podcast': podcast,
|
bgneal@143
|
84 'ext': ext,
|
bgneal@143
|
85 'alt_ext': alt_ext,
|
bgneal@402
|
86 'jplayer_media': jplayer_media,
|
bgneal@402
|
87 'jplayer_supplied': jplayer_supplied,
|
gremmie@1
|
88 },
|
gremmie@1
|
89 context_instance = RequestContext(request))
|
bgneal@402
|
90
|
gremmie@1
|
91
|
gremmie@1
|
92 def feed(request):
|
gremmie@1
|
93 try:
|
gremmie@1
|
94 channel = Channel.objects.get(pk=1)
|
gremmie@1
|
95 except Channel.DoesNotExist:
|
gremmie@1
|
96 channel = None
|
gremmie@1
|
97 return render_to_response('podcast/feed.xml', {
|
bgneal@402
|
98 'channel': channel,
|
gremmie@1
|
99 },
|
gremmie@1
|
100 context_instance = RequestContext(request))
|