Last active
December 24, 2015 04:09
-
-
Save kari/6742443 to your computer and use it in GitHub Desktop.
Ruby-koodia joka toimi ainakin vielä viime vuonna, toivottavasti css ja html eivät ole kovasti muuttuneet viimevuotisesta. Kommentoitu base_url oli 2012 budjetteihin, mutta jotain tuunausta tapahtui ja 2013 url-rakenne oli hieman erilainen kuten base_urlista näkyy. Vuoden 2014 base_url näyttäisi olevan /indox/download.jsp?lang=fi&file=/2014/tae/…
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
# (c) Kari Silvennoinen <kari at kalifi piste org>, 2011 - 2013 | |
# encoding: utf-8 | |
# require "enumerator" | |
require "csv" | |
require "rubygems" | |
#require "fastercsv" | |
require "typhoeus" | |
require "nokogiri" | |
# resp = Typhoeus::Response.new(:code => 200, :body => File.open("sample.html").read) | |
hydra = Typhoeus::Hydra.hydra | |
#hydra.stub(:get, /http:\/\/budjetti.vm.fi\//).and_return(resp) | |
# base_url = "http://budjetti.vm.fi/indox/indoxservlet?fullpathxpointer=/2012/TAE/vmkanta/pl{id}/pl{id}.xml%23/1&documentrole=tae2012" | |
# base_url = "http://budjetti.vm.fi/indox/download.jsp?lang=fi&file=/2013/tae/valtiovarainministerionKanta/YksityiskohtaisetPerustelut/{id}/{id}.html" | |
base_url = "http://budjetti.vm.fi/indox/download.jsp?lang=fi&file=/2014/tae/hallituksenEsitys/YksityiskohtaisetPerustelut/{id}/{id}.html" | |
depts = { | |
21 => "Eduskunta", | |
22 => "Tasavallan presidentti", | |
23 => "Valtioneuvoston kanslia", | |
24 => "Ulkoasiainministeriö", | |
25 => "Oikeusministeriö", | |
26 => "Sisäasiainministeriö", | |
27 => "Puolustusministeriö", | |
28 => "Valtiovarainministeriö", | |
29 => "Opetus- ja kulttuuriministeriö", | |
30 => "Maa- ja metsätalousministeriö", | |
31 => "Liikenne- ja viestintäministeriö", | |
32 => "Työ- ja elinkeinoministeriö", | |
33 => "Sosiaali- ja terveysministeriö", | |
35 => "Ympäristöministeriö", | |
36 => "Valtionvelan korot" | |
} | |
#depts = {28 => "Valtionvarainministeriö"} | |
level2 = "" | |
export_cols = [0,1,4] | |
CSV.open("budjetti-2013-vm.csv", "w", {:col_sep => ";"}) {|csv| | |
depts.keys.sort.each {|dept| | |
# csv << depts[id] | |
response = Typhoeus::Request.get(base_url.gsub("{id}",dept.to_s)) | |
unless response.code == 200 | |
p response.code + ": " + base_url.gsub("{id}", dept.to_s) | |
raise RuntimeError.new(response.code) | |
end | |
# File.open("#{depts[id]}.txt","w") {|f| f << response.body} | |
doc = Nokogiri::HTML(response.body.gsub("\x00","")) # strip nulls (\x00) and non-breaking spaces, (\xC2\xA0) | |
doc.css("div p + table").last.css("tbody tr").each {|tr| | |
header = false | |
cols = tr.css("td") | |
next if cols[1].text.gsub("\xc2\xa0","").strip.empty? | |
if cols[1].text == "Yhteensä" then | |
next | |
#header = true | |
#level2 = cols[1].content | |
#cols[1].content = "" | |
end | |
if cols[0].at_css("span.LihavaTeksti") then | |
header = true | |
level2 = cols[1].content | |
cols[1].content = "" | |
end | |
cols[0].content = level2 | |
cols[4].content = cols[4].content.gsub(/\D/,"") | |
# cols[1].content = "- " + cols[1].content unless header | |
row = [depts[dept]] + cols.each_with_index.select{|col,i| export_cols.include?(i)}.collect{|col,i| col.text.gsub("\xc2\xa0"," ").gsub("\xe2\x80\x94","0").gsub(/\s/," ").strip} | |
# p row | |
csv << row | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment