Convert a RSS / Podcast Feed to a .pls Playlist

X-Mas time...
I just got my wife a nice WiFi Radio (yes, she does not give a sh** about this blog). She loves to listen to internet radio stations so it seemed like a cool gift. We'll know for sure in less than 4 days.

Anyway. I unwrapped it to make sure it works and to preinitialize it with some presets she likes. But wait! What about my presets? Yes, I love to listen to podcasts. While the device itself has some support for RSS feeds (browsing my genre, country etc.) - it won't allow adding a RSS feed to your favorites / bookmarks. Here it will only accept playlists.  

A minor inconvenience you can fix with some PHP and SimplePie. Here is a small script which will convert any arbitrary RSS Feed to a PLS ... well, playlist :)

  1. <?php
  2. require_once("simplepie.inc");
  3.  
  4. function getFeedItems($rss, $numberOfFeedItemsToFetch = 50)
  5. {
  6. $feed = new SimplePie($rss);
  7. $feed->init();
  8. $feed->handle_content_type();
  9. if ($feed->error()) die ($feed->error());
  10. return $feed->get_items(0,$numberOfFeedItemsToFetch);
  11. }
  12.  
  13. $url = urldecode($_GET["url"]);
  14. $numEntries= $_GET["numEntries"];
  15. if (!$numEntries) $numEntries=50;
  16. if (!$url) $url = "http://feeds.feedburner.com/cnet/buzzoutloud?tag=rtcol";
  17. $items = getFeedItems($url,$numEntries);
  18. $plsItems = array();
  19. foreach($items as $item)
  20. {
  21. if ($item->get_enclosure())
  22. {
  23. $plsEntry = array();
  24. $plsEntry["title"] = $item->get_title();
  25. $plsEntry["link"] = $item->get_enclosure()->link;
  26. $plsEntry["length"] = (int) $item->get_enclosure()->length;
  27. $plsItems[] = $plsEntry;
  28. }
  29. }
  30.  
  31. header('Content-type: audio/x-scpls; charset=utf-8');
  32. header('Content-Disposition: attachment; filename="playlist.pls"');
  33. ?>[playlist]
  34. NumberOfEntries=<?= count($plsItems) ?>
  35.  
  36. <?
  37. for ($i=0; $i<count($plsItems); $i++)
  38. {
  39. print("File".($i+1)."=".$plsItems[$i]["link"]."\n");
  40. print("Title".($i+1)."=".$plsItems[$i]["title"]."\n");
  41. if ($plsItems[$i]["length"] > 0) print("Length".($i+1)."=".$plsItems[$i]["length"]."\n");
  42. else print("Length".($i+1)."=-1\n");
  43. print("\n");
  44. }
  45. print("version=2");
  46. ?>

The usage is quite simple. Call the script like this:

http://myserver/the_script.php?url=[encoded rss url]&numEntries=[optional: max number of entries in the pls]

If you don't know how to encode a URL properly, try this:

  • open a new tab in firefox
  • enter
    javascript:document.write(escape("http://your.friggin.url/feed.xml"))
    into the address bar
  • copy & paste the result

If the Feed-URL does not contain any parameters (chars such as &, ? or =) you probably won't need to encode it. Won't hurt though!

You also might want to add a fake URL part (path info) if your client needs that .pls ending in the url. so the script-call would be like:

http://myserver/the_script.php/fake.pls?url=...&numEntries=...

Oh! And you will need a copy of SimplePie for the script to work. Grab it over here

download the script bundled with SimplePie