Nightsounds Radio RSS Feed

Nightsounds Radio is a pretty special radio program that was started and ran by Bill Pearce for over 35 years. Though Pearce has since passed away, the show is still in syndication both on the Nightsounds Radio website and on many, many radio stations all around the world.

The show is special because of its format. Pearce’s aim was to provide a relaxing broadcast show for those working late hours OR (the reason I use it) for falling asleep to. The tone of the broadcast is very slow paced. Pearce has a very deep, slow-speaking voice and manner of speech that help facilitate this. Intermixed throughout the broadcast are musical selections; some of which are performed by Pearce himself.

I really can’t speak highly enough of the program, so if you’ve never heard of it, check it out. Even better, if you are able to donate to them, they have a way to do so on the left hand side of their homepage.

I got to looking at the Nightsounds Radio website and I noticed they have a little widget where they post a new show every weekday. While that’s great, I would love this in RSS or podcast form, so I could more easily download and listen to the show. So I set out to do a little scraping to provide an RSS feed that can be used by others. I’ve been using this project every night since Christmas 2010 (thanks for pointing me to Nightsounds, Dan!).

Technically, the project turned out to be pretty fun for a few reasons. I’ve done HTML scraping in PHP before, but I found a neat plugin that allows me to easily traverse the DOM with jQuery-like syntax/selectors. The plugin, PHP Simple HTML DOM Parser, is one I’m sure I’ll use more of in the future. It works great, but for this scenario I still had to combine the plugin with some RegEx to pull the specific information I needed.

Second of all, though it’s not super difficult, I’d never done any RSS generation before. The XML syntax of RSS is pretty easy to learn, too.

All I had to do was write a little script to generate an RSS file based off of the Nightsounds homepage widget and voila! I set the PHP script up as a cron job to run daily so that the RSS is always up-to-date.

The code isn’t the most elegant (it’s usually not when HTML scraping), but here it is:

find('#daily_display table') as $posting) {		//each broadcast posting
		$elems = $posting->find('td');
		//elems[0] contains file location
		preg_match('%theFile\=(clips.*\.mp3)%', $elems[0], $matches);
		$files[] = $matches[1];
		//elems[1] contains file name and date posted
		preg_match('%(.*)%', $elems[1], $matches);
		$values = explode("
", $matches[1]); //
splits [0] date and [1] name $date = str_replace("/", "", $values[0]); //mmddyyyy $date = substr($date, 4, 4) . substr($date, 0, 2) . substr($date, 2, 2); //yyyymmdd $dates[] = $date; $names[] = $values[1]; } //memory release needed for DOM traversal plugin $html->clear(); unset($html); //generate rss file $lastBuildDate = date("D, d M Y H:i:s") . " CST"; //can be "CDT" at times but it's not worth checking $output .= ""; $output .= ""; $output .= ""; $output .= "Nightsounds"; $output .= "$url"; $output .= "Nightsounds with Bill Pearce"; $output .= "en-us"; $output .= "$lastBuildDate"; $num_items = (sizeof($files) < $num_items) ? sizeof($files) : $num_items; //this loop assumes that all arrays were populated with the same amount of data (and in parallel) for ($i = 0; $i < sizeof($files); $i++) { $file = $url . str_replace("\\", "/", $files[$i]); //remote location: ...clips/dddddd.mp3 $date = $dates[$i]; //date captured: yyyymmdd $name = htmlentities($names[$i]); //name of broadcast $filesize = remote_file_size($file); $pub_date = date("D, d M Y H:i:s", mktime(12, 0, 0, substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) . " CST"; $output .= ""; $output .= "$name"; $output .= "$file"; $output .= "$file"; $output .= "$name"; $output .= "$pub_date"; $output .= ""; $output .= "\n"; } $output .= ""; $output .= ""; //save the file to public server $write_handle = fopen("public_html/nightsounds/rss.xml", "wb"); fwrite($write_handle, $output); fclose($write_handle); } //only retrieves headers of the remote file - will NOT download // function remote_file_size($file_location) { $ch = curl_init($file_location); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $data = curl_exec($ch); curl_close($ch); if ($data === false) { echo 'cURL failed'; exit; } $content_length = 'unknown'; if (preg_match('/Content-Length: (\d+)/', $data, $matches)) { $content_length = (int) $matches[1]; } return $content_length; } ?>

Download nightsounds_rss.php.txt

And the finished product? A fully functioning RSS feed:

Though I can’t promise the RSS feed won’t break if Nightsounds were to change the layout of their page, feel free to subscribe and start enjoying the benefits of its ministry!


One Response to “Nightsounds Radio RSS Feed”

  1. Michael

    Thanks so much!! What a great idea!! I have listened to Bill Pierce since the 1970’s. His music, message and ministry still speaks volumes to this generation today!