diff --git a/Politics/five-thirty-eight.15m.rb b/Politics/five-thirty-eight.15m.rb index 5f76fa937..8b8e95028 100755 --- a/Politics/five-thirty-eight.15m.rb +++ b/Politics/five-thirty-eight.15m.rb @@ -1,52 +1,71 @@ #!/usr/bin/env ruby -# Election Tracker -# v1.0 -# Chris Metcalf -# chrismetcalf -# Scrapes election odds from FiveThirtyEight's election tracker -# http://i.imgur.com/1NeqVZ6.png -# ruby -# https://github.com/chrismetcalf/bitbar-plugins + # Election Tracker + # v1.0 + # Chris Metcalf + # chrismetcalf + # Scrapes election odds from FiveThirtyEight's election tracker + # http://i.imgur.com/1NeqVZ6.png + # ruby + # https://github.com/chrismetcalf/bitbar-plugins 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"