Vivaldi’s Four Seasons for free?

I was surprised to see the wikipedia page for Vivaldi’s Four Seasons to have freely available Ogg/Vorbis files. I know I could have downloaded them all manually, yes. However, I thought it to be a good evening sport to write a small script that downloads it for me and converts the OGG files (not much liked by iTunes) into MP3 (it also sets ID3-tags with a bit of awk-magic).
The result can be found below (or download the script directly). It requires the rubygem Nokogiri and expects the command line tools ogg123, vorbiscomment (both available in a packaged named “vorbis-tools” on Macports) and lame (MP3 encoder) to be installed in your $PATH.

Obligatory legal stuff: the file comes “as is” with no warranty whatsoever. The downloaded files are from John Harrison and licensed under the Creative Commons Attribution-Share-Alike Generic 1.0 License. The script itself is hereby released as beer-ware.

The script is brittle: if Wikipedia decides to change their link styles, all hell breaks loose. It may or may not work for you – YMMV. But here goes:

#!/usr/bin/env ruby
 
require 'rubygems'
require 'open-uri'
require 'nokogiri'
require 'net/http'
 
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
url = "http://en.wikipedia.org/wiki/The_Four_Seasons_%28Vivaldi%29"
fourseasonspage = Nokogiri::HTML(open(url, "User-Agent" => user_agent))
 
@destdir = ENV['DESTDIR'] || 'vivaldi'
FileUtils.mkdir_p(@destdir) unless File.exists?(@destdir)
 
@uri = URI.parse(url)
 
# Aquiring the Four Seasons
puts "\e[1mGetting Files...\e[0m"
puts
 
fourseasonspage.css("div.medialist a").each do |ogg_link|
  url = "http://"+ @uri.host + ogg_link[:href]
  ogg_page = Nokogiri::HTML(open(url, "User-Agent" => user_agent))
 
  ogg_page.css("div.fullMedia a.internal").each do |link|
      puts "Downloading: " + link[:href]
      filename = File.basename(link[:href])
      uri = URI.parse(link[:href])
      Net::HTTP.start(uri.host) do |http|
        resp = http.get(uri.path)
        open(File.join(@destdir, filename), "wb") do |file|
          file.write(resp.body)
        end
      end
  end if /File:/.match(ogg_link[:href])
end
 
# Now comes cmd line file converting
puts "\e[1mStarting conversion...\e[0m"
puts
 
`for file in #{@destdir}/*.ogg ; do \
  echo > id3.tag; \
  vorbiscomment -l -e $file |awk -F= '{print $1"=""'\''" $2 "'\''" }' > id3.tag ; \
  source id3.tag; \
  ogg123 -d wav -f - $file \
  | lame -h -v -m s -b 192 --tt "$title" --tn "$tracknumber" --ta "$artist" --tg "$genre" --tc "$comment" --tl "$album" --id3v2-only - #{@destdir}/$(basename "$file" .ogg).mp3; \
done; rm id3.tag`
 
puts "\e[1mAll done.\e[0m"
puts "(You may wish to delete #{@destdir}/*.ogg)"

Getting started:

DESTDIR=path/to/dir ruby vivaldi.rb

Leave a Comment