#!/usr/bin/env ruby
# Credentials
COIN_USERNAME = 'changeme'
COIN_PASSWORD = 'changeme'
# Directory to store files in
COIN_DIRECTORY = '/tmp/coin'
# Windows example:
# COIN_DIRECTORY = 'C:\\temp'
# Pages to fetch and links to follow
COIN_FILES = {
'mobielenummers.html' => [
'Mobiele Nummer Reeksen (laatste versie)',
'Mobiele Nummers (laatste versie)'
],
'geografischenummers.html' => [
'Geografische Nummer Reeksen (laatste versie)',
'Geografische Nummers (laatste versie)'
],
'servicenummers.html' => [
['Service Nummers (laatste versie)', 2]
]
}
require 'rubygems'
require 'mechanize'
websession = WWW::Mechanize.new
COIN_FILES.each_pair do |path, links|
url = "http://www.coin.nl/#{path}"
puts "Opening #{url}"
indexpage = websession.get url
loginform = indexpage.form 'loginform'
if loginform
loginform.txtUID = COIN_USERNAME
loginform.txtPWD = COIN_PASSWORD
puts "Logging in"
indexpage = websession.submit loginform, loginform.buttons.first
end
links.each do |linkspec|
if linkspec.is_a? Array
linktext, linkindex = linkspec
linkindex -= 1
else
linktext = linkspec
linkindex = 0
end
websession.transact do
link = indexpage.links.text linktext
link = link[linkindex] if link.is_a? Array
puts "Following link #{link.uri}"
page = websession.click link
# page.filename is surrounded by quotes!
filename = File.basename page.filename[1..-2]
filename = File.expand_path filename, COIN_DIRECTORY
puts " => Saving to #{filename}"
page.save filename
puts "Decompressing"
raise if not system "uncompress '#{filename}'"
end
end
end
puts 'Done.' |