Created
March 27, 2020 18:00
-
-
Save jhauge/4bf8eaba16e364a4829880063f4ac3a7 to your computer and use it in GitHub Desktop.
Powershell script to get Covid data from github.
This file contains hidden or 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
# This script gets the death numbers of yesterday and the day before that | |
# from the Johns Hopkins dataset in github and calculates the number of deaths | |
# for the last day in the third column. | |
# Set up SQL provider | |
$provider = (New-Object System.Data.OleDb.OleDbEnumerator).GetElements() ` | |
| Where-Object { $_.SOURCES_NAME -like "Microsoft.ACE.OLEDB.*" } | |
if ($provider -is [system.array]) { | |
$provider = $provider[0].SOURCES_NAME | |
} | |
else { | |
$provider = $provider.SOURCES_NAME | |
} | |
# Get latest csv from github | |
$csvPath = "$env:TEMP\covid19-deaths.csv" | |
$csvUrl = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv" | |
Invoke-WebRequest $csvUrl | Set-Content -Path $csvPath | |
$connstring = "Provider=$provider;Data Source=$(Split-Path $csvPath);Extended Properties='text;HDR=YES;';" | |
$tablename = (Split-Path $csvPath -leaf).Replace(".", "#") | |
# Get dates | |
$cultureInfo = [System.Globalization.CultureInfo]::GetCultureInfo("en-US") # Damned american dateformats | |
$startDate = [System.DateTime]::Now.AddDays(-2).ToString("M/d/yy", $cultureInfo) # 2 days ago | |
$endDate = [System.DateTime]::Now.AddDays(-1).ToString("M/d/yy", $cultureInfo) # yesterday | |
$sql = "SELECT [Country/Region], [$startDate], [$endDate], [$endDate] - [$startDAte] AS Diff FROM [$tablename] WHERE [Country/Region] IN ('Denmark', 'Sweden', 'Norway', 'Germany', 'France', 'United Kingdom', 'Korea, South', 'Singapore') AND [Province/State] IS NULL" | |
# Setup connection and command | |
$conn = New-Object System.Data.OleDb.OleDbconnection | |
$conn.ConnectionString = $connstring | |
$conn.Open() | |
$cmd = New-Object System.Data.OleDB.OleDBCommand | |
$cmd.Connection = $conn | |
$cmd.CommandText = $sql | |
# Load into datatable | |
$dt = New-Object System.Data.DataTable | |
$dt.Load($cmd.ExecuteReader("CloseConnection")) | |
# Clean up | |
$cmd.dispose | Out-Null; $conn.dispose | Out-Null | |
# Output results | |
$dt | Format-Table -AutoSize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment