Skip to content

Instantly share code, notes, and snippets.

@flazz
Created February 15, 2010 18:41
Show Gist options
  • Save flazz/304867 to your computer and use it in GitHub Desktop.
Save flazz/304867 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
require 'nokogiri'
require 'open-uri'
class PlayList < Array
attr_accessor :version
def PlayList.parse s
lines = s.lines.map { |l| l.chomp }
magic = lines.shift
raise "not a playlist" unless magic == "[playlist]"
entries = lines.first[/numberofentries=(\d+)/,1].to_i or raise "missing number of entries"
lines.shift
pl = PlayList.new(entries) { Hash.new }
lines.each do |line|
case line
when /^(\w+?)(\d+?)=(.+)$/
field = $1
index = $2.to_i - 1
value = $3
pl[index][field] = value
when /Version=(\d+)/
pl.version = $1
else
raise "invalid playlist entry: #{line}"
end
end
pl
end
def + other
a = dup
other.each { |e| a << e }
a
end
def to_s
io = StringIO.new
io.puts "[playlist]"
io.puts "numberofentries=#{size}"
each_with_index do |entry, index|
%w(File Title Length).each do |field|
io.puts "#{field}#{index + 1}=#{entry[field]}"
end
end
io.puts "Version=#{@version}"
io.string
end
end
if __FILE__ == $0
# grab the home page
somafm_url = URI.parse 'http://somafm.com'
doc = Nokogiri::HTML(open(somafm_url))
# get all the stations
stations = doc.css("a[href ^= '/play/']").map { |a| a[:href][%r{/play/(.+)},1] }.uniq
stations.reject! { |s| s =~ %r{^fw/} } # ditch the firewall ones
stations.reject! { |s| s =~ %r{\d+$} } # ditch the individual bitrates
# get the playlists
playlists = stations.map { |s| PlayList.parse open("http://somafm.com/startstream=#{s}.pls").read }
# print one big playlist
master_pl = playlists.inject(:+)
puts "#{master_pl}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment