-
-
Save lojic/879a1cf3b5b1f3f988fe1971dc23308e to your computer and use it in GitHub Desktop.
Download data from NCDHHS COVID dashboards
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 | |
# Install capybara and selenium-webdriver gems | |
# | |
# $ gem install capybara | |
# $ gem install selenium-webdriver | |
# | |
# Install chromedriver | |
# | |
# $ brew install chromedriver | |
require 'capybara' | |
require 'capybara/dsl' | |
require 'selenium-webdriver' | |
# register a new driver to change the download location | |
Capybara.register_driver :headless_chrome do |app| | |
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( | |
'goog:chromeOptions': { | |
args: %w[headless disable-gpu], | |
prefs: { | |
'download.default_directory' => Dir.home | |
} | |
} | |
) | |
Capybara::Selenium::Driver.new( | |
app, | |
browser: :chrome, | |
desired_capabilities: capabilities | |
) | |
end | |
Capybara.default_driver = :headless_chrome | |
Capybara.default_max_wait_time = 10 | |
Capybara.app_host = 'https://covid19.ncdhhs.gov' | |
class NcdhhsCovidCsvDownloader | |
include Capybara::DSL | |
def initialize | |
visit('/dashboard/data-behind-dashboards') | |
click_link('View Larger') | |
end | |
def daily_cases_and_deaths | |
within_frame do | |
within('div', class: 'tabFlipboardNav') do | |
first('div', exact_text: 'Daily Cases and Deaths Metrics').click | |
end | |
find('#download-ToolbarButton').click | |
find_button('Crosstab').click | |
find('div', class: 'f1u2kjnq', text: 'CSV').click | |
find_button('Download').click | |
end | |
sleep 5 # wait for download | |
end | |
end | |
NcdhhsCovidCsvDownloader.new.daily_cases_and_deaths |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment