Skip to content

Commit

Permalink
Merge pull request matryer#1481 from chrisbrandow/patch-1
Browse files Browse the repository at this point in the history
Update five-thirty-eight.15m.rb
  • Loading branch information
matryer authored Dec 11, 2020
2 parents 34c9fa1 + 3a9e00d commit 044dac8
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions Politics/five-thirty-eight.15m.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,71 @@
#!/usr/bin/env ruby
# <bitbar.title>Election Tracker</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Chris Metcalf</bitbar.author>
# <bitbar.author.github>chrismetcalf</bitbar.author.github>
# <bitbar.desc>Scrapes election odds from FiveThirtyEight's election tracker</bitbar.desc>
# <bitbar.image>http://i.imgur.com/1NeqVZ6.png</bitbar.image>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/chrismetcalf/bitbar-plugins</bitbar.abouturl>
# <bitbar.title>Election Tracker</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Chris Metcalf</bitbar.author>
# <bitbar.author.github>chrismetcalf</bitbar.author.github>
# <bitbar.desc>Scrapes election odds from FiveThirtyEight's election tracker</bitbar.desc>
# <bitbar.image>http://i.imgur.com/1NeqVZ6.png</bitbar.image>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/chrismetcalf/bitbar-plugins</bitbar.abouturl>

require 'open-uri'
require 'nokogiri'
require 'csv'

page = Nokogiri::HTML(open("http://projects.fivethirtyeight.com/2016-election-forecast/"))
feed = Nokogiri::XML(open("https://fivethirtyeight.com/tag/2016-election/feed/"))
feed = Nokogiri::XML(open("https://fivethirtyeight.com/tag/2020-election/feed/"))

d_pct = page.css('.candidates.heads .candidate.dem .candidate-text .candidate-val.winprob')
.first.text.gsub(/[^0-9.]/, '').to_f
r_pct = page.css('.candidates.heads .candidate.rep .candidate-text .candidate-val.winprob')
.first.text.gsub(/[^0-9.]/, '').to_f

if d_pct > r_pct
puts ":princess: #{d_pct}%"
else
puts ":imp: #{r_pct}%"
table = []
projectDataLink = 'https://projects.fivethirtyeight.com/2020-general-data/presidential_national_toplines_2020.csv'
begin
open(projectDataLink) do |f|
table = CSV.parse(f, headers: true)
end
rescue => e
puts "⚠️⚠️"
puts "something went wrong with this link: #{projectDataLink}"
return
end

d_pct = "%.1f" % (table[0]["ecwin_chal"].to_f*100.0)
r_pct = "%.1f" % (table[0]["ecwin_inc"].to_f*100.0)

puts d_pct > r_pct ? "😎 #{d_pct}%" : ":imp: #{r_pct}%"

democratName = "😎 #{table[0]["candidate_chal"]}"
republicanName = ":imp: #{table[0]["candidate_inc"]}"
thirdParty = table[0]["candidate_3rd"]
thirdPartyName = "🤦 #{thirdParty}"
puts "---"
puts "Chance of Winning:"
puts ":princess: Hillary Clinton: #{d_pct}%"
puts ":imp: Donald Trump: #{r_pct}%"
puts "#{democratName}: #{d_pct}%"
puts "#{republicanName}: #{r_pct}%"

puts "---"
puts "Electoral Votes:"
page.css(".card-natl-tables .table.one .cand").each do |row|
puts "#{row.css(".name.desktop").text}: #{row.css('.candidate-val').text}"
puts "#{democratName}: #{table[0]["ev_chal"].to_i}"
puts "#{republicanName}: #{table[0]["ev_inc"].to_i}"
if thirdParty.to_s.length > 0
puts "#{thirdPartyName}: #{table[0]["ev_3rd"]}"
end
puts "💆 No majority: #{"%.1f" % (table[0]["ec_nomajority"].to_f*100.0)}%"

puts "---"
puts "Popular Vote:"
page.css(".card-natl-tables .table.two .cand").each do |row|
puts "#{row.css(".name.desktop").text}: #{row.css('.candidate-val').text}"
puts "#{democratName}: #{"%.1f" % table[0]["national_voteshare_chal"].to_f}%"
puts "#{republicanName}: #{"%.1f" % table[0]["national_voteshare_inc"].to_f}%"
if thirdParty.to_s.length > 0
puts "#{thirdPartyName}: #{"%.1f" % table[0]["national_voteshare_3rd"]}%"
end
puts "🤷 Other: #{"%.1f" % table[0]["nat_voteshare_other"].to_f}%"

puts "---"
puts ":wolf: FiveThirtyEight Election Forecast | href=http://projects.fivethirtyeight.com/2016-election-forecast/"
puts "📜 FiveThirtyEight Election Feed"
feed.css("item")[0..2].each do |item|
date = Date::parse(item.css('pubDate').text)
puts "#{date.strftime("%Y-%m-%d")}: #{item.css('title').text} | href=#{item.css('link').text}"
end

puts ":wolf: Visit FiveThirtyEight Election Forecast | href=http://projects.fivethirtyeight.com/2020-election-forecast/"

puts "---"
puts "Refresh... | refresh=true"

0 comments on commit 044dac8

Please sign in to comment.