Skip to content

Instantly share code, notes, and snippets.

@benbuckman
Last active November 11, 2018 23:11
Show Gist options
  • Save benbuckman/61962786acc7b64c43a5fd1f0c9d416d to your computer and use it in GitHub Desktop.
Save benbuckman/61962786acc7b64c43a5fd1f0c9d416d to your computer and use it in GitHub Desktop.
CA-10 vote tally watcher (Nov 2018, Josh Harder vs Jeff Denham)
#! /usr/bin/env ruby
require 'net/http'
require 'json'
# Periodically check the vote tallies in CA-10
# (Nov 2018 midterms, Jash Harder vs Jeff Denham)
class CA10Checker
URL = 'https://api.sos.ca.gov/returns/us-rep/district/10'
class Result < Struct.new(:name, :votes, :percent)
def to_s
"#{name}: #{votes} (#{percent}%)"
end
end
def check
uri = URI(URL)
res = Net::HTTP.get(uri)
body = JSON.parse(res)
puts "Currently #{Time.now}"
puts "Reporting as of #{body['ReportingTime']}"
results = body['candidates'].map do |c|
Result.new(
c['Name'],
c['Votes'].sub(/[^\d]/, '').to_i,
c['Percent'].to_f
)
end.sort_by(&:votes).reverse # winner first
results.each {|r| puts "- #{r.to_s}"}
total_votes = results.sum(&:votes)
puts "Total votes: #{total_votes}"
if @last_total_votes && @last_total_votes != total_votes
puts "🗳 #{total_votes - @last_total_votes} more votes counted!"
end
@last_total_votes = total_votes
winner = results.first.name
emoji = if winner.match?(/Harder/i); "👍 🎉"; else; "👎 😢"; end
puts "Leading: #{winner} (by #{results[0].votes - results[1].votes} votes) #{emoji}"
end
end
if $0 == __FILE__
ch = CA10Checker.new
while true do
ch.check
puts ""
sleep (60*5)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment