Last active
December 30, 2015 16:29
-
-
Save pungoyal/7855274 to your computer and use it in GitHub Desktop.
delhi elections
This file contains hidden or 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
require 'nokogiri' | |
require 'open-uri' | |
require 'pry' | |
def get_party(full_name) | |
parties = { | |
'Aam Aadmi Party' => 'AAP', | |
'Bharatiya Janata Party' => 'BJP', | |
'Indian National Congress' => 'INC', | |
'Bahujan Samaj Party' => 'BSP' | |
} | |
parties[full_name] || full_name[0..2] | |
end | |
def get_status(full_name) | |
options = { | |
'Counting In Progress' => 'Counting', | |
'Result Declared' => 'Declared' | |
} | |
options[full_name] || full_name[0..2] | |
end | |
def get_name(full_name) | |
full_name[0..14] | |
end | |
def get_votes(votes) | |
votes = votes.to_i | |
votes.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse | |
end | |
def analyze(row) | |
leader_name = row.children[0].text | |
leader_party = row.children[1].text | |
leader_votes = row.children[2].text | |
return leader_name.strip, leader_party.strip, leader_votes.strip | |
end | |
def analyze_constituency(number) | |
doc = Nokogiri::HTML(open("http://eciresults.nic.in/ConstituencywiseU05#{number}.htm?ac=#{number}")) | |
table = doc.xpath('//table')[7].children[3].children[1].children[1].children[1].children[1].children[1].children[1] | |
constituency = table.children[0].text.split('-')[1].strip | |
printf "%-2s. %-13s", number, constituency | |
name, party, votes_first = analyze(table.children[4]) | |
printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_first) | |
name, party, votes_second = analyze(table.children[6]) | |
printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_second) | |
name, party, votes = analyze(table.children[8]) | |
printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes) | |
printf "\t\t %6s \t %7s \n", get_votes(votes_first.to_i - votes_second.to_i), get_status(table.children[1].text.strip) | |
end | |
(1..70).each {|i| analyze_constituency(i) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
quick and dirty. don't be too harsh :)