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
source :gemcutter | |
gem 'hpricot' | |
gem 'dm-core' | |
gem 'dm-sqlite-adapter' | |
gem 'dm-migrations' |
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
$ gem install bundler | |
$ bundle install | |
$ ruby queenish.rb |
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
# encoding: utf-8 | |
# Fetch the Queen's new-years speeches into a SQLite3 database... WHY WOULDN'T YOU? | |
require "rubygems" | |
require "bundler" | |
Bundler.require(:default) | |
require 'open-uri' | |
class Speech | |
include DataMapper::Resource | |
property :id, Serial | |
property :year, String | |
property :body, Text | |
end | |
FileUtils.rm_rf("data.sqlite3") | |
DataMapper.setup(:default, | |
"sqlite3:///#{Dir.pwd}/data.sqlite3") | |
DataMapper.auto_migrate! | |
def fetch_and_convert(url) | |
open(url, "r:iso-8859-1").read.encode("utf-8") | |
end | |
index = Hpricot(fetch_and_convert("http://kongehuset.dk/publish.php?dogtag=k_dk_aktuelt_taler")) | |
links = (index / "div#content a").select { |a| a.html =~ /tale/i } | |
links.first(1).each do |tale| | |
h = Hpricot(fetch_and_convert(tale.get_attribute("href"))) | |
year, body = [tale.html, | |
(h / "div#content").to_html.gsub!(/(<[^>]*>)/,"")] | |
puts body | |
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