Skip to content

Instantly share code, notes, and snippets.

@pungoyal
Last active December 30, 2015 16:29

Revisions

  1. pungoyal revised this gist Feb 10, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion delhi.rb
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,15 @@ def get_party(full_name)
    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
    @@ -47,7 +56,7 @@ def analyze_constituency(number)
    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 %20s \n", get_votes(votes_first.to_i - votes_second.to_i), table.children[1].text.strip
    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) }
  2. pungoyal revised this gist Feb 10, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions delhi.rb
    Original file line number Diff line number Diff line change
    @@ -32,22 +32,22 @@ def analyze(row)

    def analyze_constituency(number)
    doc = Nokogiri::HTML(open("http://eciresults.nic.in/ConstituencywiseU05#{number}.htm?ac=#{number}"))
    table = doc.xpath('//table')[7].children.last.children.first.children.last.children.first.children.first.children[1].children[1]
    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[3])
    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[4])
    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[5])
    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 %20s \n", get_votes(votes_first.to_i - votes_second.to_i), table.children[1].text.strip
    end

    (1..70).each {|i| analyze_constituency(i) }
    (1..70).each {|i| analyze_constituency(i) }
  3. pungoyal revised this gist Dec 8, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion delhi.rb
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,10 @@ def analyze_constituency(number)
    printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_first)

    name, party, votes_second = analyze(table.children[4])
    printf "\t\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_second)
    printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_second)

    name, party, votes = analyze(table.children[5])
    printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes)

    printf "\t\t %6s \t %20s \n", get_votes(votes_first.to_i - votes_second.to_i), table.children[1].text.strip
    end
  4. pungoyal created this gist Dec 8, 2013.
    50 changes: 50 additions & 0 deletions delhi.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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_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.last.children.first.children.last.children.first.children.first.children[1].children[1]

    constituency = table.children[0].text.split('-')[1].strip

    printf "%-2s. %-13s", number, constituency

    name, party, votes_first = analyze(table.children[3])
    printf "\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_first)

    name, party, votes_second = analyze(table.children[4])
    printf "\t\t %-15s %-4s |%7s|", get_name(name), get_party(party), get_votes(votes_second)

    printf "\t\t %6s \t %20s \n", get_votes(votes_first.to_i - votes_second.to_i), table.children[1].text.strip
    end

    (1..70).each {|i| analyze_constituency(i) }