Skip to content

Instantly share code, notes, and snippets.

@benkelly
Forked from donalod/covid_improved.rb
Created March 17, 2020 22:12
Show Gist options
  • Save benkelly/65b0a2c1c36594a522b2dbe3ad2edeb0 to your computer and use it in GitHub Desktop.
Save benkelly/65b0a2c1c36594a522b2dbe3ad2edeb0 to your computer and use it in GitHub Desktop.
covid_improved.rb
#!/usr/bin/env ruby
# Requires you to install "spark" with ports or homebrew + the below gems.
require "csv"
require 'time'
require 'httparty'
require 'colorize'
require 'tty-progressbar'
# Get the latest data from John Hopkins, they keep adding days to headers...
# confirmed raw: https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv
# deaths raw: https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv
# recovered raw: https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv
# Get the data?
confirmed_bar = TTY::ProgressBar.new("Downloading confirmed data [:bar]", total: 100)
100.times do
confirmed_bar.advance(1)
end
confirmed_outfile = File.new("confirmed_"+Time.now.strftime("%Y%m%dT%H%M%S")+".csv", "w")
confirmed_outfile.binmode
confirmed_response = HTTParty.get('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv', stream_body: true) do |content|
confirmed_outfile.write(content)
end
confirmed_outfile.close
death_outfile = File.new("death_"+Time.now.strftime("%Y%m%dT%H%M%S")+".csv", "w")
death_outfile.binmode
death_response = HTTParty.get('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv', stream_body: true) do |content|
death_outfile.write(content)
end
death_outfile.close
death_bar = TTY::ProgressBar.new("Downloading deaths data [:bar]", total: 100)
100.times do
death_bar.advance(1)
end
recovered_outfile = File.new("recovered_"+Time.now.strftime("%Y%m%dT%H%M%S")+".csv", "w")
recovered_outfile.binmode
recovered_response = HTTParty.get('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv', stream_body: true) do |content|
recovered_outfile.write(content)
end
recovered_outfile.close
recovered_bar = TTY::ProgressBar.new("Downloading recovery data [:bar]", total: 100)
100.times do
recovered_bar.advance(1)
end
puts "\e[H\e[2J"
##header = "Province/State,Country/Region,Lat,Long,1/22/20,1/23/20,1/24/20,1/25/20,1/26/20,1/27/20,1/28/20,1/29/20,1/30/20,1/31/20,2/1/20,2/2/20,2/3/20,2/4/20,2/5/20,2/6/20,2/7/20,2/8/20,2/9/20,2/10/20,2/11/20,2/12/20,2/13/20,2/14/20,2/15/20,2/16/20,2/17/20,2/18/20,2/19/20,2/20/20,2/21/20,2/22/20,2/23/20,2/24/20,2/25/20,2/26/20,2/27/20,2/28/20,2/29/20,3/1/20,3/2/20,3/3/20,3/4/20,3/5/20,3/6/20,3/7/20,3/8/20,3/9/20,3/10/20,3/11/20,3/12/20,3/13/20,3/14/20,3/15/20,3/16/20"
#confirmed_outfile.puts(header)
# This is grabbing the first file it's fed
#CSV.foreach(confirmed_outfile, :headers => true, :quote_char=>'"') do |row|
if !ARGV[0].nil?
input1 = ARGV[0].downcase
case input1
when "confirmed"
dataset = confirmed_outfile
when "death","deaths"
dataset = death_outfile
when "recovered"
dataset = recovered_outfile
else
puts "No dataset specified of 'confirmed', 'deaths', or 'recovered' so using 'confirmed'"
dataset = confirmed_outfile
end
end
dataset ||= confirmed_outfile
lines = CSV.read(dataset, :headers => true, :quote_char=>'"')
sorted_lines = lines.sort_by{|row| row[-1].to_i}.reverse!
sorted_lines.each do |line, index|
if line[-1].to_s == "0"
#puts "Skipping"+index.to_s
next
end
case line[-1].to_i
when 1..100
graph_color = :light_cyan
when 101..500
graph_color = :cyan
when 501..1000
graph_color = :light_yellow
when 1001..5000
graph_color = :yellow
when 5001..10000
graph_color = :light_red
when 10001..50000
graph_color = :red
when 50001..100000
graph_color = :magenta
else
graph_color ||= :white
end
text_color = :white
background_color = ""
province_state = line[0].tr(',', '').strip.to_s if line[0]
country_region = line[1].tr(',', '').strip.to_s if line[1]
latitude = line[2].tr(',', '').strip.to_s if line[2]
longtitude = line[3].tr(',', '').strip.to_s if line[3]
province_state ||= ""
country_region ||= ""
days = line[4..-1]
#puts(province_state+","+latitude+","+longtitude+","+row[-1])
location = (((province_state.nil? || province_state.empty?) ? "" : province_state+",")+country_region)
if !ARGV[1].nil? and location.downcase.include? (ARGV[1].downcase)
background_color = :light_black
end
if !ARGV[0].nil? and location.downcase.include? (ARGV[0].downcase)
background_color = :light_black
end
graph_and_number = (`spark #{days.flatten.join(' ')}`).to_s.strip+" = "+line[-1].to_s
puts location.ljust(50).colorize(:color => text_color, :background => background_color) + graph_and_number.colorize(:color => graph_color, :background => background_color)
end
exit
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment