Appendix E: Playlist Parsing Source Code
The following is some sample code, written in Ruby, for parsing any type of playlist you are likely to encounter when interacting with internet radio. There are two files. playlist.rb defines the Playlist class. Upon initialization of a new playlist, the URL is cleaned up and checked for validity. The valid?() function reads the playlist and determines whether it is a valid playlist. extract_streams() determines the format of the playlist and then parses its content, returning the URLs of the streams listed in it. It recurses in case the playlist refers to other playlists.
utilities.rb contains data used to determine the type of playlist we’re dealing with, has user-agent definitions, and has a number of helper functions, such as functions that extract the format, protocol, and extension from the URL, and functions that download and parse the headers of playlists. The tcp_connect() function could be further useful in reading the headers of actual radio streams to determine metadata about the stream (for ICY streams, for instance).
You may download the following two files:
| ← Appendix D: Mitigating Issues with RadioTime |