Last active
July 1, 2024 00:21
-
-
Save pca2/09532345909594c8f1def0921aa56ee5 to your computer and use it in GitHub Desktop.
538 2024 Election Model BitBar/xbar plugin
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
#!/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