annotate podcast/views.py @ 1201:fe10aea76cbd tip

Add 2023 MP3 compilation links
author Brian Neal <bgneal@gmail.com>
date Sun, 24 Mar 2024 14:50:23 -0500
parents aa43db10c565
children
rev   line source
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@1086 65 'V3_DESIGN': True,
bgneal@1032 66 })
gremmie@1 67
gremmie@1 68
gremmie@1 69 def detail(request, id):
bgneal@1086 70 podcast = get_object_or_404(Item.objects.select_related(), pk=id)
bgneal@143 71
bgneal@403 72 ext = get_ext_from_url(podcast.enclosure_url)
bgneal@403 73 alt_ext = None
bgneal@403 74 if podcast.alt_enclosure_url:
bgneal@403 75 alt_ext = get_ext_from_url(podcast.alt_enclosure_url)
bgneal@143 76
bgneal@403 77 jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url,
bgneal@403 78 alt_ext, podcast.alt_enclosure_url)
bgneal@402 79
bgneal@1032 80 return render(request, 'podcast/detail.html', {
bgneal@403 81 'channel': podcast.channel,
bgneal@403 82 'podcast': podcast,
bgneal@403 83 'ext': ext,
bgneal@403 84 'alt_ext': alt_ext,
bgneal@403 85 'jplayer_media': jplayer_media,
bgneal@403 86 'jplayer_supplied': jplayer_supplied,
bgneal@1086 87 'V3_DESIGN': True,
bgneal@1032 88 })
bgneal@402 89
gremmie@1 90
gremmie@1 91 def feed(request):
bgneal@403 92 try:
bgneal@403 93 channel = Channel.objects.get(pk=1)
bgneal@403 94 except Channel.DoesNotExist:
bgneal@403 95 channel = None
bgneal@403 96
bgneal@403 97 if channel:
bgneal@403 98 channel.items = Item.objects.filter(channel=channel)
bgneal@403 99
bgneal@1032 100 return render(request, 'podcast/feed.xml', {
bgneal@403 101 'channel': channel,
bgneal@1032 102 })