This page describes how to re-write an XML file (aka Podcast / RSS) using SimplePie. You might want to do this because the original XML is not quite the right format for your Reciva radio; it might have the wrong characters in a title, or be the wrong files
A website with the ability to set permissions on folders and run PHP scripts
A copy of SimplePie which comes from SimplePie.org .
Setup and install SimplePie as the instructions specify; so you have two sub-directories, one called "php" for the SimplePie code, and one called "cache" for the cache-files. The cache-files mean that SimplePie doesn't have to read the original XML site everytime it is called. (There is probably an option to turn off the cache in the SimplePie documentation if you wish to do that).
This example takes the "Naked Scientists" podcast and "corrects" a few issues which cause problems with Reciva radios. If the code were saved as "naked_science.php" it generates the required XML. The problems with this XML were two-fold; firstly various illegal characters were getting into the code which caused the radio to not offer the podcasts; typically these were ampersand (&) characters. Secondly, the titles all start "Naked Scientists ....."; with the limited line length of a Reciva display, one cannot tell one pod-cast from the next.
Firstly, examine the original Naked Scientists XML file, http://www.thenakedscientists.com/naked_scientists_podcast.xml, use "view source" in your browser if you want to try it. Here are a few lines from the top, which starts a very long header section, most of which seems to be to drive the iTunes service:
<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0" >
<channel>
<title >- The Naked Scientists Naked Science Radio Show PODCAST - Stripping Down Science</title>
<link >http://www.thenakedscientists.com/</link>
<itunes:author >Dr Chris Smith</itunes:author>
Skipping down a little, and we come to the first "item", this being the first actual podcast programme, subsequent bits of the file repeat the "item" component many times.
<item>
<title >Naked Scientists 07.12.26 - Ask the Naked Scientists</title> <link >http://www.thenakedscientists.com/HTML/podcasts/show/2007.12.26/</link> <pubDate >Wed, 26 Dec 2007 18:43:53 +0000</pubDate> <description>Happy Christmas! To keep you entertained while we're off tucking into Turkey, this week and next we've got special editions of a new series of programmes we're launching in 2008 called Ask The Naked Scientists, our new live interactive science radio phone-in, with Dave Ansell, Sue Marchant and Chris Smith.</description> <itunes:author >Dr Chris Smith, The Naked Scientists</itunes:author> <itunes:subtitle >Ask the Naked Scientists - Naked Scientists 07.12.26</itunes:subtitle> <itunes:summary >Happy Christmas! To keep you entertained while we're off tucking into Turkey, this week and next we've got special editions of a new series of programmes we're launching in 2008 called Ask The Naked Scientists, our new live interactive science radio phone-in, with Dave Ansell, Sue Marchant and Chris Smith.</itunes:summary> <itunes:explicit>no</itunes:explicit> <itunes:duration >24:24</itunes:duration> <itunes:keywords >177</itunes:keywords> <enclosure url="http://media.libsyn.com/media/nakedscientists/Ask_The_Naked_Scientists_07.12.26.mp3" length="8802064" type="audio/mpeg" ></enclosure> <source url="http://www.thenakedscientists.com/naked_scientists_podcast.xml">The Naked Scientists Science PODCAST</source> <guid >http://tnsstore.caret.cam.ac.uk/Ask_The_Naked_Scientists_07.12.26.mp3</guid> </item>
Within the <item> tag, there are three items of real interest, the <title> which is displayed on the Reciva radio display, the <enclosure> which is the podcast audio file, and the <description> which is handy to see on a computer display, though not strictly needed for the radio.
So, were we to reduce the "<item>" to that required for the Reciva radio, it would look like:
<item>
<title >Ask the Naked Scientists - 07.12.26</title>
<description>Happy Christmas! To keep you entertained while we're off tucking into Turkey,
this week and next we've got special editions of a new series of
programmes we're launching in 2008 called Ask The Naked Scientists,
our new live interactive science radio phone-in, with Dave Ansell, Sue Marchant
and Chris Smith.</description>
<enclosure url="http://media.libsyn.com/media/nakedscientists/Ask_The_Naked_Scientists_07.12.26.mp3"
length="8802064" type="audio/mpeg" />
</item>
Note that I've changed the title by removing the words "Naked Scientists" from the start of the title, and moving the date to the end. This makes it easier to read on the limited line-length of a Reciva radio display.
Also note that I've made the enclosure tag "self-closing" by putting the "/" character at the end of the tag.
So, to my PHP code which calls SimplePie
<?php// Series title is used to write top of XML file title $series_title = "Naked Scientists Podcasts parsed";
// Write header is a function call to write the top of the XML file writeheader($series_title);
// Now some very standard setup stuff, copied from SimplePie demonstration code. Includes // the source XML file for the re-write. include_once('./php/simplepie.inc'); $feed = new SimplePie(); // Create a new instance of SimplePie $feed->set_feed_url('http://www.thenakedscientists.com/naked_scientists_podcast.xml'); $feed->set_cache_duration (3600); //The cache duration $feed->enable_xml_dump(isset($_GET['xmldump']) ? true : false); $success = $feed->init(); // Initialize SimplePie $feed->handle_content_type(); // Take care of the character encoding ?> <?php if ($success): // initialise $itemlimit, which limits the maximum length of the resulting XML file $itemlimit=0; foreach($feed->get_items() as $item): // set the maximum length to 150 items
if ($itemlimit==150) { break; } // we proceed around the loop to write an <item> section ?> <item>
<title><? // Start by removing "Naked Scientists " from the title string
$temp_title = str_replace("Naked Scientists ", "", $item->get_title() ); // with the titles, need to replace erroneous & characters with the full form
$temp_title = str_replace("&", "&", $temp_title) ;
$temp_title = str_replace("&amp;", "&", $temp_title) ; // Finally split the title into two parts, the actual title and the transmission date list($trans_date, $prog_title) = split(" - ", $temp_title, 2); // Reassemble the title with the date after the title string $new_title = $prog_title . " - ". $trans_date ; echo $new_title ; ?></title>
<link><? echo $item->get_permalink(); ?></link>
<description><? echo str_replace("&", "&", $item->get_description() ); ?>
</description><?
$the_enclosure = $item->get_enclosure();
$new_link = $the_enclosure->get_link() ;
echo '<enclosure url="' . $new_link .'" ';
echo ' length="'. $the_enclosure->get_length() .'" ';
echo ' type="audio/mpeg" />';
?> </item> <? $itemlimit++ ?>
<?php endforeach; ?> <?php endif; ?>
<?php
writefooter();
function writeheader($title) {
echo '<?xml version="1.0" encoding="UTF-8"?> ' ;
echo '<rss version="2.0"> ' ;
echo '<channel> ' ;
echo '<title>' . $title .'</title> ' ;
echo '<description>For use with Reciva internet radios, add this file to my-podcasts</description> ';
echo '<link>http://www.fleetwith.co.uk/radiorss/</link>' ;
} function writefooter() {
echo '</channel></rss> ';
} ?>
If you read down the code, the main work is in re-writing the title area. The work on the "<enclosure>" is unnecessary for this example, but comes into its own for the next.
This is a tiny bit more advanced, but uses the same method as example 1. In this case, the broadcaster Europe1 has its podcasts buried in a complicated enclosure statement. Their podcast XML file is at http://www.europe1.fr/podcast/revue_de_presque.jsp
Inspecting the source file, one finds the <enclosure> tag to be:
<enclosure url="http://stat3.cybermonitor.com/m/media/europe1pod_v.mp3?R=01_01_2008_09H30&S=revue_de_presque&
media_url=http%3A%2F%2Fdl.europe1.fr.ipercast.net%2FEurope1_revue_de_presque_01_01_2008_09H30.mp3"
length="2347049" type="audio/mpeg" />
This form of enclosure tag doesn't work in a Reciva radio, the problem appears to be the hand-off via the stat-counter. Fortunately, the full URL of the actual audio file is present in the statement, it appears after the "media_url=" component.
The PHP and SimplePie code to transform this URL is similar to Example 1, here is where it differs, when re-writing the <enclosure> tag:
<?
$the_enclosure = $item->get_enclosure();
$old_link = $the_enclosure->get_link();
// define what we are looking for within the enclosure link
$split_target = "media_url=";
// calculate where in the string we wish to split it; after the $split_target
$split_place = strpos($old_link,$split_target) + strlen($split_target);
// and split the string, leaving just the end after the target
$new_link = substr($old_link,$split_place);
echo '<enclosure url="' . $new_link .'" ';
echo ' length="'. $the_enclosure->get_length() .'" ';
echo ' type="audio/mpeg" />';
?>
Last Update - 1st Jan 2008