- Author:
- Kyle_Dickerson
- Posted:
- July 16, 2010
- Language:
- Python
- Version:
- 1.2
- Score:
- 2 (after 2 ratings)
The syndication documentation gives a very basic beginning to creating an iTunes Podcast Feed, but leaves a lot of work left to be figured out. This is a completed example. Because I needed to obtain the duration of the podcast file programmatically (not part of this code) I limit the podcast to mp3 files. If you want to obtain durations some other way you can set the mime_type in the same manner as the other properties.
This is what I'm using and should be plenty for you to customize from.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | from django.utils.feedgenerator import Rss201rev2Feed
from django.contrib.syndication.views import Feed
import datetime
class iTunesPodcastsFeedGenerator(Rss201rev2Feed):
def rss_attributes(self):
return {u"version": self._version, u"xmlns:atom": u"http://www.w3.org/2005/Atom", u'xmlns:itunes': u'http://www.itunes.com/dtds/podcast-1.0.dtd'}
def add_root_elements(self, handler):
super(iTunesPodcastsFeedGenerator, self).add_root_elements(handler)
handler.addQuickElement(u'itunes:subtitle', self.feed['subtitle'])
handler.addQuickElement(u'itunes:author', self.feed['author_name'])
handler.addQuickElement(u'itunes:summary', self.feed['description'])
handler.addQuickElement(u'itunes:explicit', self.feed['iTunes_explicit'])
handler.startElement(u"itunes:owner", {})
handler.addQuickElement(u'itunes:name', self.feed['iTunes_name'])
handler.addQuickElement(u'itunes:email', self.feed['iTunes_email'])
handler.endElement(u"itunes:owner")
handler.addQuickElement(u'itunes:image', self.feed['iTunes_image_url'])
def add_item_elements(self, handler, item):
super(iTunesPodcastsFeedGenerator, self).add_item_elements(handler, item)
handler.addQuickElement(u'iTunes:summary',item['summary'])
handler.addQuickElement(u'iTunes:duration',item['duration'])
handler.addQuickElement(u'iTunes:explicit',item['explicit'])
class iTunesPodcastPost():
def __init__(self, podcast):
self.id = podcast.id
self.approval_date_time = podcast.approval_date_time
self.title = podcast.title
self.summary = podcast.description
self.enclosure_url = insecure_url(podcast.podcast_file.url)
self.enclosure_length = podcast.podcast_file.size
self.enclosure_mime_type = u'audio/mpeg'
self.duration = podcast.duration
self.explicit = u'no'
def __unicode__(self):
return "Podcast: %s" % self.title
@permalink
def get_absolute_url(self):
return ('podcast_view', [str(self.id)])
class iTunesPodcastsFeed(Feed):
"""
A feed of podcasts for iTunes and other compatible podcatchers.
"""
title = "iTunes Podcast"
link = "/podcasts/iTunes/"
author_name = 'The Great Author'
description = "A Podcast of great things."
subtitle = "From now until forever"
summary = "People get around and chat about stuff. You listen."
iTunes_name = u'author Name'
iTunes_email = u'[email protected]'
iTunes_image_url = u'http://example.com/url/of/image'
iTunes_explicit = u'no'
feed_type = iTunesPodcastsFeedGenerator
feed_copyright = "Copyright %s by the The Author." % datetime.date.today().year
def items(self):
"""
Returns a list of items to publish in this feed.
"""
stop = datetime.datetime.now()
start = stop - datetime.timedelta(hours=72)
posts = Podcast.objects.filter(approval_date_time__range=(start,stop)).order_by('-approval_date_time','id')
posts = [iTunesPodcastPost(item) for item in posts]
return posts
def feed_extra_kwargs(self, obj):
extra = {}
extra['iTunes_name'] = self.iTunes_name
extra['iTunes_email'] = self.iTunes_email
extra['iTunes_image_url'] = self.iTunes_image_url
extra['iTunes_explicit'] = self.iTunes_explicit
return extra
def item_extra_kwargs(self, item):
return {'summary':item.summary, 'duration':item.duration, 'explicit':item.explicit}
def item_pubdate(self, item):
return item.approval_date_time
def item_enclosure_url(self, item):
return item.enclosure_url
def item_enclosure_length(self, item):
return item.enclosure_length
def item_enclosure_mime_type(self, item):
return item.enclosure_mime_type
def item_description(self, item):
return item.summary
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
You need a
from django.db.models import permalink
at the top there. Otherwise, very handy - cheers.#
You have one bug in add_item_elements method - name space iTunes with capital "T" - it doesn't work in iTunes app. Need to use 'itunes'.
#
Super useful snippet, thank you very much.
If others are interested in an example of this that'll soon be deployed, you can see our code at:
https://github.com/freelawproject/courtlistener/blob/multi-doc/alert/audio/feeds.py
You may notice that we didn't end up using iTunesPodcastPost from above. Seemed unnecessary and added unneeded complexity. I also ran into the issue with the capital iTunes breaking things.
#
Please login first before commenting.