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