A simple page scraper that takes a number of stats off the RIT Covid Stats page and saves them to a database and google sheets.
For fun, this scraper uses Hanami libraries for database work and logging instead of ActiveRecord or ActiveSupport.
The database is implemented using sqlite3
The core data model is:
Recorder::Entities::CovidStat- Snapshot of stats at a particular point in time
Recorder::Entities::CollectionAttempt- Log of all attempts at collecting stats and the outcome
- Links to collected stat if successful
The scraping is done by Recorder::Spiders::RitCovidSpider with a series of very dirty string and array manipulations. This never needed to be too complicated to require additional abstraction.
This application has a rails-like cli. For any command you can provide the -h|--help option.
Modify any necessary variables in lib/config.rb.
Install gems:
bundle install
Google Credentials setup for exporting to google sheets:
- Create your Google Sheets API credentials at the Google Cloud Platform and download the json file
- Create a directory somewhere to capture token:
mkdir ./credstore - Place the downloaded json file in the above directory as
credentials.json, e.h../credstore/credentials.json - Run
docker run --rm -it -e DEBUG=1 -v $PWD/credstore:/app/credstore glossawy/rit-covid-recorder:current bin/recorder authorize --home=./credstore - Follow prompts to acquire token
- Token will now be found at
./credstore/token.yaml - When running the image as daemon or with export, make sure to mount the credentials directory, e.g.
-e CREDENTIALS_HOME=/app/credstore -v $PWD/credstore:/app/credstore, it cannot be read-only
Note: Currently the scraper will assume your spreadsheet's columns matches ours.
DB setup:
bin/recorder db createbin/recorder db migrate
or just bin/recorder db prepare
To add a new model or migration u can use bin/recorder <generate|g> <model|migration> [...].
The general flow for collecting statistics is:
bin/recorder scrape fetch
- This will not persist the data, allows verification
bin/recorder scrape fetch --persist
- This will persist the data if it is sufficiently different from previous data
bin/recorder export csv > data.csvorbin/recorder export googledepending on preference
This application uses javan/whenever for local crontabs:
bundle exec whenever --update-crontab
You can modify lib/schedule.rb and then repeat the above command to update frequency of checks.
Alternatively, a daemon implementation is provided which can take a fetch frequency and a backup frequency:
bin/recorder daemon run --help
Logging output while running is written to stdout as well as:
app.log- Primary log of all application output
cron.log- Output during cron execution
database.log- Output for all database queries (can be disabled in
lib/config.rb)
- Output for all database queries (can be disabled in
scripts/backup- zsh script to use
sqlite3to backup locally
- zsh script to use
scripts/notify.ps1- powershell script to use BurntToast to display popup notifications for new stats on windows only
scripts/publishandscripts/publish.ps1- zsh and powershell scripts to publish a docker image to a docker hub repository with basic versioning