Home
Empowerment through collective innovation

Tutorial: Making an iTunes-compatible podcast with Views 2 or 3

A podcast, as any RSS geek will tell you, is simply a garden-variety RSS feed with an enclosure tag pointing to an audio or video file. You could say it's simply a matter of perspective — instead of the enclosed file attached to each item being an optional extra, the file contains the main content which you're publishing to your audience. An iTunes-compatible podcast just takes this concept one small step further, by adding metadata (like author and category) according to Apple's podcasting specifications, which are an extension of the basic XML format, i.e. an XML namespace.

There are many modules which exist to help you create podcasts with Drupal. But Drupal's Views module makes it easy to create RSS feeds, and Drupal's templating system makes it easy to alter their formatting, so in my opinion the easiest way to make a podcast for arbitrary data is to do it by hand — for example, you might have one CCK type with an MP3 file uploaded using the FileField module, and another with a Link field pointing to an MP3 hosted on archive.org.

Here's how you do it.

First, create a View of the nodes you want to include in your podcast. In my case, the content type is called Actualité (French for "news"), and I only want to list the ten most recent published nodes sorted by the posting date. I made sure that the audio field is present by adding the filter Content: Audio (field_audio) - list and requiring that this be set to True. In the default display, I set the pager options to show 10 items per page. This is also where you set the title.

(These screenshots are of Views 3, but everything I describe here can be done just as easily with Views 2.)

Next, add a Feed display (or "Flux" in French). The RSS Feed style settings aren't important because you'll override these later in the template.

Before we go further, we need to use a theme preprocess function to make some additional information available to the view templates. For my site, it's okay if these are available to other RSS feeds, so I'll go ahead and create a function called THEMENAME_preprocess_views_view_row_rss() which does everything the default function template_preprocess_views_view_row_rss() in sites/all/modules/views/theme/theme.inc does, plus load the node object into the variable $node. My theme is called sciencepresse so my function has to be called sciencepresse_preprocess_views_view_row_rss() and be placed in sites/all/themes/sciencepresse/template.php.


<?php
/**
  * Default theme function for all RSS rows.
  */
function sciencepresse_preprocess_views_view_row_rss(&$vars) {
  
$view $vars['view'];
  
$options $vars['options'];
  
$item $vars['row'];

  // Use the [id] of the returned results to determine the nid in [results]
  
$result $vars['view']->result;
  
$id $vars['id'];
  
$node node_load$result[$id-1]->nid );

  $vars['title'] = check_plain($item->title);
  
$vars['link'] = check_url($item->link);
  
$vars['description'] = check_plain($item->description);
  
$vars['item_elements'] = empty($item->elements) ? '' format_xml_elements($item->elements);
  
// pass entire node object to template
  
$vars['node'] = $node;
}
?>


Now, you're ready to modify the Views templates which control the actual presentation of the RSS feed. These templates will go into sites/all/themes/sciencepresse/ beside the file template.php. To find out which templates are available, select the Feed display for your view and click Theme Information. Here, you'll see that two templates are used, one for the overall page ("Style output") and one for each item ("Row style output"). The list of possible filenames will be scanned when rendering this View will be listed, from most general (affecting the greatest number of Views, in this case all RSS views on the site) to most specific (affecting just this display for just this view). By default Views will use the most specific template it can find, but if nothing else is available it will use the generic templates views-view-rss.tpl.php and views-view-row-rss.tpl.php. When you first look, those two filenames will be active, and listed in bold.

You'll need to copy these templates and create files named views-view-rss--baladodiffusion.tpl.php and views-view-row-rss--baladodiffusion.tpl.php which will be used just for this View. Once you've created those templates, click the button labelled "Rescan template files" and the new files should be listed in bold. (Don't forget to save your View before going to the next step.)

Now, you need to edit our templates to include extra XML stylesheets, namespaces, and tags. First, change the template for the entire page:


<?php print "<?xml"?> version="1.0" encoding="utf-8" <?php print "?>"?>
<?php 
print "<?xml-stylesheet"?> href="http://www.sciencepresse.qc.ca/sites/www.sciencepresse.qc.ca/files/rss2enclosuresfull.xsl" type="text/xsl" media="screen" <?php print "?>"?>
<?php 
print "<?xml-stylesheet"?> href="http://www.sciencepresse.qc.ca/sites/www.sciencepresse.qc.ca/files/itemcontent.css" type="text/css" media="screen" <?php print "?>"?>
<rss version="2.0" xml:base="<?php print $link?>"<?php print $namespaces?>  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  <channel>
    <title><?php print $title?></title>
    <link>http://www.sciencepresse.qc.ca/</link>
    <description>L'Agence Science-Presse est un média à but non lucratif qui alimente des médias sur la science. "Je vote pour la science" est inspirée de Science Debate 2008, un mouvement américain pour un débat sur la science entre les candidats à la présidence.</description>
    <language><?php print $langcode?></language>
    <copyright>Copyright (C) Agence Science-Presse</copyright>
    <itunes:author>Agence Science-Presse</itunes:author>
    <itunes:explicit>no</itunes:explicit>
    <itunes:keywords>science</itunes:keywords>
    <itunes:image href="http://www.sciencepresse.qc.ca/sites/all/themes/sciencepresse/files-logo.png" />
    <image>
      <url>http://www.sciencepresse.qc.ca/sites/all/themes/sciencepresse/files-logo.png</url>
      <title>Je vote pour la science</title>
      <link>http://www.sciencepresse.qc.ca/</link>
    </image>
    <itunes:owner>
      <itunes:name>Agence Science-Presse</itunes:name>
      <itunes:email>redaction@sciencepresse.qc.ca</itunes:email>
    </itunes:owner>
    <itunes:subtitle>Parce que tout le monde s&#039;intéresse à la science</itunes:subtitle>
    <itunes:summary>L'Agence Science-Presse est un média à but non lucratif qui alimente des médias sur la science. "Je vote pour la science" est inspirée de Science Debate 2008, un mouvement américain pour un débat sur la science entre les candidats à la présidence.</itunes:summary> 
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="<?php print $link ?>" type="application/rss+xml" />
    <itunes:category text="Science &amp; Medicine">
      <itunes:category text="Social Sciences" />
      <itunes:category text="Natural Sciences" />
    </itunes:category>

    <?php print $channel_elements?>
    <?php print $items?>
  </channel>
</rss>

Next, change the template for each item in the feed. I fetched the description from the CCK text field called chapeau (French for "hat", which is used as a header field for each article). I also extracted the path, size, and MIME type for the audio field, and changed the field <dc:creator> to use the site name instead of the feed's title (the default). While I was at it changed the links to the taxonomy terms from the format /taxonomy/term/123 to /articles/tous/123 so they would point to a special View I created earlier. Those last two changes were made using the function preg_replace(), the Swiss army knife (or maybe the duct tape) of all template hacking. If you aren't familiar with regular expressions or you don't need to make similar changes, you can safely leave those lines out. In particular, if you don't happen to have a View with the path /articles/tous that takes a taxonomy ID as an argument, you should definitely remove the first preg_replace() line!

I ended up with this template:


<?php

$description check_plain(strip_tags($node->field_chapeau[0]['value']));
$audio_url url($node->field_audio[0]['filepath'], array('absolute' => TRUE));
$audio_size $node->field_audio[0]['filesize'];
$audio_type $node->field_audio[0]['filemime'];
$item_elements preg_replace('@/taxonomy/term/@''/articles/tous/'$item_elements);
$item_elements preg_replace('@<dc:creator>.*</dc:creator>@''<dc:creator>Agence Science-Presse</dc:creator>'$item_elements);

?>
  <item>
    <title><?php print $title?></title>
    <link><?php print $link?></link>
    <description><?php print $description?></description>
    <?php print $item_elements?>
    <itunes:explicit>no</itunes:explicit>
    <itunes:subtitle></itunes:subtitle>
    <itunes:keywords></itunes:keywords>
    <itunes:summary><?php print $description ?></itunes:summary>
    <itunes:author>Agence Science-Presse</itunes:author>
    <enclosure url="<?php print $audio_url ?>" length="<?php print $audio_size ?>" type="<?php print $audio_type ?>" />
  </item>

Clear the cache, and you're done! We have a beautiful RSS feed ready for our users (you can see an extract below).

You can now give your feed's URL to anyone with podcast-compatible software, including iTunes. You're now also ready to submit your feed to the iTunes store. To do this, you'll need to use iTunes and have an account — instructions are on Apple's website. Before submitting your feed, you should probably use feedvalidator.org to check that it's valid XML.

After submission, you'll get an email in a few days letting you know that your feed is listed in the iTunes store. This means your users can search for it by name and subscribe within iTunes. Congratulations on making your first podcast!


<?xml version="1.0" encoding="utf-8" ?>
<?xml
-stylesheet href="http://www.sciencepresse.qc.ca/sites/www.sciencepresse.qc.ca/files/rss2enclosuresfull.xsl" type="text/xsl" media="screen" ?>
<?xml
-stylesheet href="http://www.sciencepresse.qc.ca/sites/www.sciencepresse.qc.ca/files/itemcontent.css" type="text/css" media="screen" ?>
<rss version="2.0" xml:base="http://www.sciencepresse.qc.ca/baladodiffusion_rss.xml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  <channel>
    <title>Je vote pour la science</title>
    <link>http://www.sciencepresse.qc.ca/</link>
    <description>L'Agence Science-Presse est un média à but non lucratif qui alimente des médias sur la science. "Je vote pour la science" est inspirée de Science Debate 2008, un mouvement américain pour un débat sur la science entre les candidats à la présidence.</description>
    <language>fr</language>
    <copyright>Copyright (C) Agence Science-Presse</copyright>
    <itunes:author>Agence Science-Presse</itunes:author>
    <itunes:explicit>no</itunes:explicit>
    <itunes:keywords>science</itunes:keywords>
    <itunes:image href="http://www.sciencepresse.qc.ca/sites/all/themes/sciencepresse/files-logo.png" />
    <image>
      <url>http://www.sciencepresse.qc.ca/sites/all/themes/sciencepresse/files-logo.png</url>
      <title>Je vote pour la science</title>
      <link>http://www.sciencepresse.qc.ca/</link>
    </image>
    <itunes:owner>
      <itunes:name>Agence Science-Presse</itunes:name>
      <itunes:email>redaction@sciencepresse.qc.ca</itunes:email>
    </itunes:owner>
    <itunes:subtitle>Parce que tout le monde s&#039;intéresse à la science</itunes:subtitle>
    <itunes:summary>L'Agence Science-Presse est un média à but non lucratif qui alimente des médias sur la science. "Je vote pour la science" est inspirée de Science Debate 2008, un mouvement américain pour un débat sur la science entre les candidats à la présidence.</itunes:summary> 
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://www.sciencepresse.qc.ca/baladodiffusion_rss.xml" type="application/rss+xml" />
    <itunes:category text="Science &amp; Medicine">
      <itunes:category text="Social Sciences" />
      <itunes:category text="Natural Sciences" />
    </itunes:category>
    <item>
      <title>Science et politique, 2e année!</title>
      <link>http://www.sciencepresse.qc.ca/actualite/2010/05/25/science-politique-2e-annee</link>
      <description>De la place de la science dans les villes jusqu’à la géo-ingénierie de la planète en passant par la question qui tue: jusqu’où le scientifique a-t-il le droit de s’impliquer socialement et politiquement? C’est un retour sur une année faste que nous vous proposons avec cette dernière émission de la saison!</description>
      <comments>http://www.sciencepresse.qc.ca/actualite/2010/05/25/science-politique-2e-annee#comments</comments>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/7">Société</category>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/8">Québec</category>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/511">Je vote pour la science</category>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/98">Science et politique</category>
      <pubDate>Tue, 25 May 2010 16:06:15 +0000</pubDate>
      <dc:creator>Agence Science-Presse</dc:creator>
      <guid isPermaLink="false">31998 at http://www.sciencepresse.qc.ca</guid>
      <itunes:explicit>no</itunes:explicit>
      <itunes:subtitle></itunes:subtitle>
      <itunes:keywords></itunes:keywords>
      <itunes:summary>De la place de la science dans les villes jusqu’à la géo-ingénierie de la planète en passant par la question qui tue: jusqu’où le scientifique a-t-il le droit de s’impliquer socialement et politiquement? C’est un retour sur une année faste que nous vous proposons avec cette dernière émission de la saison!</itunes:summary>
      <itunes:author>Agence Science-Presse</itunes:author>
      <enclosure url="http://www.sciencepresse.qc.ca/sites/www.sciencepresse.qc.ca/files/audio/2010/05/jvpls250510.mp3" length="71748480" type="audio/mpeg" />
    </item>
    <item>
      <title>Science et politique: lentement mais sûrement</title>
      <link>http://www.sciencepresse.qc.ca/actualite/2010/05/18/science-politique-lentement-surement</link>
      <description>De la marée noire dans le Golfe du Mexique jusqu&#039;au congrès de l&#039;Acfas en passant par un congrès axé spécifiquement &quot;science et politique&quot;, les liens politique-science sont de plus en plus nombreux. C&#039;est à travers ces exemples que nous abordons l&#039;émission de cette semaine. </description>
      <comments>http://www.sciencepresse.qc.ca/actualite/2010/05/18/science-politique-lentement-surement#comments</comments>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/7">Société</category>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/8">Québec</category>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/511">Je vote pour la science</category>
      <category domain="http://www.sciencepresse.qc.ca/articles/tous/98">Science et politique</category>
      <pubDate>Tue, 18 May 2010 14:40:42 +0000</pubDate>
      <dc:creator>Agence Science-Presse</dc:creator>
      <guid isPermaLink="false">31969 at http://www.sciencepresse.qc.ca</guid>
      <itunes:explicit>no</itunes:explicit>
      <itunes:subtitle></itunes:subtitle>
      <itunes:keywords></itunes:keywords>
      <itunes:summary>De la marée noire dans le Golfe du Mexique jusqu&#039;au congrès de l&#039;Acfas en passant par un congrès axé spécifiquement &quot;science et politique&quot;, les liens politique-science sont de plus en plus nombreux. C&#039;est à travers ces exemples que nous abordons l&#039;émission de cette semaine. </itunes:summary>
      <itunes:author>Agence Science-Presse</itunes:author>
      <enclosure url="http://www.sciencepresse.qc.ca/sites/www.sciencepresse.qc.ca/files/audio/2010/05/jvpls180510.mp3" length="28869277" type="audio/mpeg" />
    </item>
  </channel>
</rss>