Skip to content

Instantly share code, notes, and snippets.

@pca2
Last active July 1, 2024 00:21
Show Gist options
  • Save pca2/09532345909594c8f1def0921aa56ee5 to your computer and use it in GitHub Desktop.
Save pca2/09532345909594c8f1def0921aa56ee5 to your computer and use it in GitHub Desktop.
538 2024 Election Model BitBar/xbar plugin
#!/usr/bin/env ruby
# <bitbar.title>538 Midterm Tracker</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Carleton Atwater</bitbar.author>
# <bitbar.author.github>pca2</bitbar.author.github>
# <bitbar.desc>Scrapes election odds from FiveThirtyEight's election tracker</bitbar.desc>
# <bitbar.image>https://imgur.com/bCF8fyg.png</bitbar.image>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# This code is pretty hacky, but it was a fun 1 hour project to track something 20ish days away.
require 'nokogiri'
require 'open-uri'
url = 'https://projects.fivethirtyeight.com/2024-election-forecast/' # Replace with your target URL
doc = Nokogiri::HTML(URI.open(url))
biden_percent = doc.css('.dem .odds').text
trump_percent = doc.css('.rep .odds').text
last_updated_at = doc.css('#intro .timestamp').text
puts "😎 #{biden_percent}%"
puts "---"
puts "🍊 #{trump_percent}%"
puts "---"
puts last_updated_at
puts "---"
puts "🦊 FiveThirtyEight Election Forecast | href=#{url}"
puts "---"
puts "Refresh... | refresh=true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment