Creating Podcast XML from Drupal content

Submitted by Dan on Thu, 07/09/2015 - 13:11

I needed to create some podcasts for Kansas Public Radio, which uses the Drupal Content Management System (CMS). I couldn't find a satisfactory Drupal module that could create the RSS data that iTunes needs, so I had to come up with my own solution. IIRC, Drupal Feeds would have worked great but iTunes needs specific XML tags (i.e. <itunes:description>, <itunes:category>, etc) that Drupal Feeds couldn't provide. I also dried Drupal Views PHP but it was too buggy and I also couldn't get the code to output outside of the template.

My solution was to create a stand alone PHP page that bootstraps Drupal, then I could do anything I wanted with the data. 

The first step was to create a new view in Drupal that the PHP could execute. Much easier than writing the SQL manually. No need to reinvent the wheel. 

Then I created a new PHP file called kpr-presents.php and I put it in a directory named /widgets/podcats. 

Before the PHP works on the tagging the data, it can just output the static data at the beginning of the XML document.

 <?php
// set the content type to XML
header("Content-type: text/xml");
 
// it likes to be echo'd for some reason...
echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";;
?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
    <channel>
        <title>KPR Presents</title>
        <link>http://kansaspublicradio.org/kpr-news/kpr%20presents</link>
        <language>en-us</language>
        <copyright>&#x2117; &amp; &#xA9; 2015 Kansas Public Radio</copyright>
        <itunes:subtitle>Join Kay McIntyre on Kansas Public Radio&#39;s weekly talk-radio show.</itunes:subtitle>
        <itunes:author>Kansas Public Radio</itunes:author>
        <itunes:summary>KPR Presents is an opportunity to showcase high-profile, thought-provoking lectures, discussions and dialogues recorded throughout the region. There are so many fascinating people who come to this area, everyone from Supreme Court Justice Sonya Sotomayor to syndicated columnist Leonard Pitts. KPR Presents is a great way to share some of those lectures with our listeners. We have also been able to expand the program to cover a broad range of topics, including the Kansas Sesquicentennial, the National Day of Listening and the Kansas Reads program sponsored by the State Library of Kansas, just to name a few.</itunes:summary>
        <description>KPR Presents is an opportunity to showcase high-profile, thought-provoking lectures, discussions and dialogues recorded throughout the region. There are so many fascinating people who come to this area, everyone from Supreme Court Justice Sonya Sotomayor to syndicated columnist Leonard Pitts. KPR Presents is a great way to share some of those lectures with our listeners. We have also been able to expand the program to cover a broad range of topics, including the Kansas Sesquicentennial, the National Day of Listening and the Kansas Reads program sponsored by the State Library of Kansas, just to name a few.</description>
        <itunes:owner>
            <itunes:name>Kansas Public Radio</itunes:name>
            <itunes:email>kprdeveloper@gmail.com</itunes:email>
        </itunes:owner>
        <itunes:image href="http://kansaspublicradio.org/widgets/podcasts/kpr-presents-logo_3000x3000.png" />
        <itunes:category text="Society &amp; Culture" />
        <itunes:explicit>no</itunes:explicit>

Now it's time to put the power of Drupal to use. First we need to bootstrap it.

 // load Drupal
define('DRUPAL_ROOT', '/home3/kpr/public_html');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
date_default_timezone_set('America/Chicago');

Now we can use the Drupal functions to execute the View that we created earlier. Then we can parse through that data and put it into XML tags.

 // load the drupal view that does all the sql work for us
$view = views_get_view('podcast_data', true); 
$view->set_display('kpr_presents');
$view->pre_execute();
$view->execute();
 
foreach ($view->result as $item) {
    $node = node_load($item->nid);
 
    // now do stuff with the node
    //print_r($node);
 
    $mp3url = $node->npr_audio[LANGUAGE_NONE][0]['mp3'];
 
    // some things are getting duplicated, need to watch out for that, it's a no no
    if (($mp3url == '') || ($mp3url == NULL)) {
      continue;
    }
 
    echo '<item>'."\r\n";
    echo '    <title>'.check_plain($node->title).'</title>'."\r\n";
    echo '    <itunes:subtitle>This week on KPR Presents...</itunes:subtitle>'."\r\n";
    echo '    <pubDate>'.date("D, d M Y H:i:s",$node->created).' GMT</pubDate>'."\r\n";
    echo '    <itunes:author>Kay McIntyre</itunes:author>'."\r\n";
    echo '    <itunes:summary><![CDATA[ ' . htmlspecialchars($node->body[LANGUAGE_NONE][0]['summary']) . ' ]]></itunes:summary>'."\r\n";
    echo '    <itunes:image href="'.str_replace('/widgets/podcasts', '', file_create_url($node->field_image[LANGUAGE_NONE][0]['uri'])).'" />'."\r\n";
 
    // <enclosure> is the media of the podcast
    // length is the file size in bytes
    $bytes = curl_get_file_size($mp3url);
    echo '    <enclosure url="'.$mp3url.'" length="'.$bytes.'" type="audio/mpeg" />'."\r\n";
 
    $duration = $node->npr_audio[LANGUAGE_NONE][0]["duration"];
    // actually I don't think we NEED to worry about what format it's in, itunes accept either seconds or hh:mm:ss formats.
    //if (!strpos($duration, ':')) {
    //  // if no colon, then it's in seconds. We need to covert it to human readable format.
    //  $duration = gmdate("i:s", $duration);
    //}
    if ($duration == '') $duration = '59:00'; // most KPR Presents shows are 59 minutes long
    echo '    <itunes:duration>'.$duration.'</itunes:duration>'."\r\n";
 
    // guid can just be the url since that's unique
    echo '    <guid>$node->nid.'</guid>'."\r\n";
 
    echo '</item>'."\r\n";   
}

I should explain that this PHP code is specific to my website, which saves the MP3 URL in a field called NPR Audio. You can download and install the NPR Drupal module which provides this field and is pretty easy to use.

Attachment Size
kpr-presents.php6.91 KB 6.91 KB
example podcast XML3.63 KB 3.63 KB