Last active
December 18, 2015 13:10
-
-
Save arashm/5788351 to your computer and use it in GitHub Desktop.
Script to Report Iran Election Results (2013) Every 1 Minute
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
# /usr/bin/env ruby | |
# if you have "rufus-scheduler" gem installed you can uncomment corresponding lines in order to | |
# run the script in every certain time. | |
# require 'rufus/scheduler' | |
require 'open-uri' | |
class Election | |
VALUES = %w{Rohani Qalibaf Rezaei Jalili Velayati Qarazi } | |
def initialize | |
parse_js | |
end | |
def parse_js | |
open('http://election.farsnews.com/electioninfo.js') do |f| | |
f.each_line do |line| | |
if line =~ /^var last/ | |
name, value = line.delete(';').chomp.gsub(/(^var last)/, '').split(' = ') | |
instance_variable_set '@' + name.downcase, value | |
end | |
end | |
end | |
end | |
def show_result | |
puts '================' | |
puts 'Results in:' + Time.now.strftime('%H:%M:%S') | |
puts '================' | |
VALUES.each do |value| | |
puts value + ": \tVotes #{eval('@' + value.downcase)}\tPercent: #{(((eval '@' + value.downcase).to_f/@total.to_f)*100.00).round(2)}" | |
end | |
end | |
end | |
# scheduler = Rufus::Scheduler.start_new | |
# scheduler.every '1m', first_at: Time.now do | |
# elec = Election.new | |
# elec.show_result | |
# end | |
# scheduler.join | |
elec = Election.new | |
elec.show_result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment