Created
December 28, 2008 12:01
-
-
Save mikker/40453 to your computer and use it in GitHub Desktop.
Fetch the Queen's new-years speeches into a SQLite3 database... WHY WOULDN'T YOU?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fetch the Queen's new-years speeches into a SQLite3 database... WHY WOULD YOU? | |
require "rubygems" | |
require "open-uri" | |
require "hpricot" | |
require "dm-core" | |
class Speech | |
include DataMapper::Resource | |
property :id, Serial | |
property :year, String | |
property :body, Text | |
end | |
DataMapper.setup(:default, | |
"sqlite3:///#{Dir.pwd}/data.db") | |
DataMapper.auto_migrate! | |
class String | |
def utfy(from='ISO-8859-1') | |
require "iconv" | |
Iconv.conv('utf-8', from, self) | |
end | |
end | |
index = Hpricot(open("http://kongehuset.dk/publish.php?dogtag=k_dk_aktuelt_taler")) | |
links = (index / "div#content a").select { |a| a.html =~ /tale/i } | |
links.each do |tale| | |
h = Hpricot(open(tale.get_attribute("href"))) | |
year, body = [tale.html.utfy, | |
(h / "div#content").to_html.chop.utfy.gsub!(/(<[^>]*>)/s,"")] | |
Speech.new(:year => year, :body => body).save | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment