Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add covid19 example #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/covid19/covid19.rb
@@ -0,0 +1,54 @@
#!/usr/bin/env ruby

# COVID-19 statistics from https://thevirustracker.com/
#
# How to use:
# 1. install ruby
# 2. install embulk
# 4. install embulk-input-script $ embulk gem install embulk-input-script
# 5. install embulk-output-postgresql $ embulk gem install embulk-output-postgresql
# 5. run $ embulk run config.yml

require 'open-uri'
require 'json'
require 'yaml'
require 'csv'
require 'time'

case ARGV[0]
when "setup" # covid19.rb setup config.yml setup.yml
config = {
"tasks" => 1,
"columns" => [
{"name" => "countrycode", "type" => "string"},
{"name" => "date", "type" => "timestamp", "format" => "%m/%d/%y"},
{"name" => "cases", "type" => "long"},
{"name" => "deaths", "type" => "long"},
{"name" => "recovered", "type" => "long"},
]
}
File.write(ARGV[2], config.to_yaml)

when "run" # covid19.rb run setup.yml output.csv 0
uri = URI.parse('https://thevirustracker.com/timeline/map-data.json')
data = JSON.parse(uri.read)

def or_zero(val)
val.to_s.empty? ? "0" : val
end

CSV.open(ARGV[2], 'wb') do |csv|
data["data"].each do |row|
csv << [
row["countrycode"],
row["date"],
or_zero(row["cases"]),
or_zero(row["deaths"]),
or_zero(row["recovered"]),
]
end
end

when "finish" # covid19.rb finish setup.yml

end
12 changes: 12 additions & 0 deletions examples/covid19/example.yml
@@ -0,0 +1,12 @@
in:
type: script
run: ruby covid19.rb
try_named_pipe: false
out:
type: postgresql
host: localhost # Set PostgreSQL hostname
user: pg # Set PostgreSQL username
password: "" # Set PostgreSQL password
database: embulk_example # Set PostgreSQL database name
table: covid19 # Set destination table name
mode: replace