-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcis_scrapper.rb
71 lines (48 loc) · 2.06 KB
/
cis_scrapper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'yaml'
# LOADING CONFIG STUFF ########################################################
settings = YAML::load( File.open( 'settings.yml' ) )
# LET'S GO ####################################################################
global_data = {}
# we first download each survey
settings["predictions"].each do |survey_key, survey_zone|
categories = {}
puts "Downloading #{survey_zone["name"]}: #{survey_zone["url"]}"
survey_web = Nokogiri::HTML(open(survey_zone["url"]))
# now we grab the results
results = survey_web.xpath(survey_zone["xpath"]["voting_expression"])
puts "#{results.count} opciones de voto posibles"
# for each result we must categorize it
results.each do |result|
fonts = result.xpath(".//td")
result = {:type => fonts[0].content.strip,
:value => fonts[1].content.strip.to_f}
puts result.inspect
# If it's a party, we include it in its group
if !settings["labels"]["parties"][result[:type]].nil?
categories[settings["labels"]["parties"][result[:type]]] ||= 0
categories[settings["labels"]["parties"][result[:type]]] += result[:value]
# If it's a blank vote, to the blank group
elsif settings["labels"]["blank"].include? result[:type]
categories["blank"] ||= 0
categories["blank"] += result[:value]
# If it's not in the 'no label' group, to the 'others' group
elsif not(settings["labels"]["no_label"].include? result[:type])
categories["other"] ||= 0
categories["other"] += result[:value]
end
end
# now we normalize, so all those citizen which don't answer are not taken into account
total_votes = categories.values.inject(0){|v, total| v+total}
global_data[survey_zone["name"]] = {}
categories.each do |category, value|
global_data[survey_zone["name"]][category] = value * 100 / total_votes
end
end
puts "************* RESULTADOS *****************"
puts "zone|#{global_data.values.first.keys.join("|")}"
global_data.each do |zone, results|
puts "#{zone}|#{results.values.join("|")}"
end