Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Last active March 6, 2020 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakubOboza/499dc287153061761d0ffc401bd898d7 to your computer and use it in GitHub Desktop.
Save JakubOboza/499dc287153061761d0ffc401bd898d7 to your computer and use it in GitHub Desktop.
require 'date'
require "open-uri"
class Covid19Data
attr_reader :date, :month, :year, :day
def initialize(date)
@date = date
@month = Covid19Data.padded(date.month)
@year = date.year
@day = Covid19Data.padded(date.day)
end
def filename
"#{@month}-#{@day}-#{@year}.csv"
end
def download
unless File.exists?(self.filename)
begin
open(self.data_url) do |csvfile|
File.open(self.filename, "wb") do |file|
puts "Downloading #{self.filename}"
file.write(csvfile.read)
end
end
rescue
#file doesnt exist, skip?
puts "no data for #{self.filename}"
end
end
end
def data_url
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/#{self.filename}"
end
def self.padded(num)
if num > 9
num
else
"0#{num}"
end
end
end
from = Date.parse("1/1/2020")
to = Date.today
step = from
loop do
covidata = Covid19Data.new(step)
covidata.download
if step >= to
break
end
step = step.next
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment