forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request matryer#1481 from chrisbrandow/patch-1
Update five-thirty-eight.15m.rb
- Loading branch information
Showing
1 changed file
with
45 additions
and
26 deletions.
There are no files selected for viewing
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
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" |