Skip to content

Instantly share code, notes, and snippets.

@ox
Created September 10, 2011 16:18
Show Gist options
  • Save ox/1208487 to your computer and use it in GitHub Desktop.
Save ox/1208487 to your computer and use it in GitHub Desktop.
Pick any genre (passed as arg) and get back a directory full of mp3 with proper ID3 tags from console.fm. Enjoy
#!/usr/bin/env ruby
require 'open-uri'
require 'hpricot'
class ID3Tag
attr_accessor :title, :artist, :album, :year, :comment, :genre, :track
def initialize
@title = @artist = @album = @comment = "\0" * 30
@year = "\0\0\0\0"
@genre= 52
end
def unpack_from fname
file = File.open name
file.seek -offset, IO::SEEK_END
tag,@title,@artist,@album,@year,@comment,@genre = file.read.unpack "A3A30A30A30A4A30C"
raise "no TAG" if tag != 'TAG'
s_com, flag, track = @comment.unpack "A28CC"
if flag == 0 and track != 0
@comment = s_com
@track = track
end
@genre = @genre || 52
end
def to_binary
return ['TAG', @title.ljust(30,"\0"),@artist.ljust(30,"\0"),@album.ljust(30,"\0"),@year.ljust(4,"\0"),@comment.ljust(30,"\0"),@genre].pack("A3A30A30A30A4A30C")
end
end
eval("Dir.mkdir %{s}; Dir.chdir %{s}" % {s: "\"#{ARGV[0]}\""})
(open("http://console.fm/electro-house") { |html| Hpricot(html) }).search("a[@href*=media.console.fm/tracks]").each do |a|
filename = "#{a.inner_html.gsub(/[\s&\/',]/, "_")}.mp3"
tag = ID3Tag.new
tag.title, tag.artist = a.inner_html.match(/(.*?) by (.*?)$/)[1..2]
begin
f = File.new(filename, "a+")
f.syswrite open(a.get_attribute('href')).read
f.syswrite tag.to_binary
f.close
puts "#{a.inner_html}"
rescue Exception => e
puts e.message
next
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment