Skip to content

Instantly share code, notes, and snippets.

@opariffazman
Last active March 27, 2020 03:54
Show Gist options
  • Save opariffazman/7dee31e0b7127a72f3ee69d0d2b36fd9 to your computer and use it in GitHub Desktop.
Save opariffazman/7dee31e0b7127a72f3ee69d0d2b36fd9 to your computer and use it in GitHub Desktop.
Get Live Status of Current Covid19 Cases using this free API [https://covid19api.com/] with powershell specifying country. Defaults to my country.
param(
[Parameter(Mandatory=$false)]
[ValidateSet('-azerbaijan','afghanistan','albania','algeria','andorra','angola','antigua-and-barbuda','argentina','armenia','aruba','australia','austria','azerbaijan','bahamas','bahamas-the','bahrain','bangladesh','barbados','belarus','belgium','belize','benin','bhutan','bolivia','bosnia-and-herzegovina','brazil','brunei','bulgaria','burkina-faso','cabo-verde','cambodia','cameroon','canada','cape-verde','cayman-islands','central-african-republic','chad','channel-islands','chile','china','colombia','congo-(brazzaville)','congo-(kinshasa)','costa-rica','cote-divoire','croatia','cruise-ship','cuba','curacao','cyprus','czech-republic','czechia','denmark','diamond-princess','djibouti','dominica','dominican-republic','east-timor','ecuador','egypt','el-salvador','equatorial-guinea','eritrea','estonia','eswatini','ethiopia','faroe-islands','fiji','finland','france','french-guiana','gabon','gambia','gambia-the','georgia','germany','ghana','gibraltar','greece','greenland','grenada','guadeloupe','guam','guatemala','guernsey','guinea','guinea-bissau','guyana','haiti','holy-see','honduras','hong-kong','hong-kong-sar','hungary','iceland','india','indonesia','iran','iran-(islamic-republic-of)','iraq','ireland','israel','italy','ivory-coast','jamaica','japan','jersey','jordan','kazakhstan','kenya','korea-south','kosovo','kuwait','kyrgyzstan','laos','latvia','lebanon','liberia','libya','liechtenstein','lithuania','luxembourg','macao-sar','macau','madagascar','mainland-china','malaysia','maldives','mali','malta','martinique','mauritania','mauritius','mayotte','mexico','moldova','monaco','mongolia','montenegro','morocco','mozambique','namibia','nepal','netherlands','new-zealand','nicaragua','niger','nigeria','north-ireland','north-macedonia','norway','occupied-palestinian-territory','oman','others','pakistan','palestine','panama','papua-new-guinea','paraguay','peru','philippines','poland','portugal','puerto-rico','qatar','republic-of-ireland','republic-of-korea','republic-of-moldova','republic-of-the-congo','reunion','romania','russia','russian-federation','rwanda','saint-barthelemy','saint-kitts-and-nevis','saint-lucia','saint-martin','saint-vincent-and-the-grenadines','san-marino','saudi-arabia','senegal','serbia','seychelles','singapore','slovakia','slovenia','somalia','south-africa','south-korea','spain','sri-lanka','st.-martin','sudan','suriname','sweden','switzerland','syria','taipei-and-environs','taiwan','taiwan','tanzania','thailand','the-bahamas','the-gambia','timor-leste','togo','trinidad-and-tobago','tunisia','turkey','uganda','uk','ukraine','united-arab-emirates','united-kingdom','uruguay','us','uzbekistan','vatican-city','venezuela','viet-nam','vietnam','west-bank-and-gaza','zambia','zimbabwe')]
$Country = "malaysia" # defaults
)
<#
.SYNOPSIS
This script get status update of current COVID19 Cases
.DESCRIPTION
.NOTES
File Name : get-covidstatus.ps1
Author : ariff.azman
Version : 1.0
.LINK
https://github.com/PrateekKumarSingh/Graphical
https://covid19api.com/
https://api.covid19api.com/countries
.INPUTS
Country selection, defaults to Malaysia
.OUTPUTS
COVID19 Cases for Confirmed, Deaths, Recovered with plotted Graphs
#>
if(!(Get-Module -Name Graphical -ListAvailable) ) {
$showGraph = $false
}else {
$showGraph = $true
}
function ConvertFrom-Unicode {
param (
[Parameter(Mandatory)]
[String]$FullUnicode
)
$StrippedUnicode = $FullUnicode -replace 'U\+',''
$UnicodeInt = [System.Convert]::toInt32($StrippedUnicode,16)
$UnicodeFinal = [System.Char]::ConvertFromUtf32($UnicodeInt)
$UnicodeFinal
}
function Write-Loading {
param (
[Parameter(Mandatory=$false)]
[Int]$Count = 39,
[Parameter(Mandatory=$false)]
[Float]$Delay = 0.6,
[Parameter(Mandatory=$false)]
[String]$Char = "-",
[switch]$Newline
)
for ($i = 0; $i -lt $Count; $i++) {
Write-Host $Char -NoNewline
Start-Sleep -Milliseconds $Delay
}
if ($Newline) {
Write-Host " "
}
}
function Get-LatestCase {
param(
[Parameter(Mandatory)]
[string]$Country,
[Parameter(Mandatory)]
[ValidateSet('confirmed','deaths','recovered')]
[string]$Type
)
$Url = "https://api.covid19api.com/live/country/$Country/status/$Type"
$Parsed = (Invoke-WebRequest $Url).Content | ConvertFrom-Json
$Latest = $Parsed | Select-Object -Last 1 | ForEach-Object Cases
$Latest
}
function Get-LiveCase {
param(
[Parameter(Mandatory)]
[string]$Country,
[Parameter(Mandatory)]
[ValidateSet('confirmed','deaths','recovered')]
[string]$Type
)
$currentMonth = Get-Date -Format yyyy-MM
$Url = "https://api.covid19api.com/country/$Country/status/$Type/live"
$Parsed = (Invoke-WebRequest $Url).Content | ConvertFrom-Json
$Parsed | Where-Object {($_.Date).Substring(0,10) -match $currentMonth} | ForEach-Object Cases
}
function Get-StepValue {
param (
[Parameter(Mandatory)]
[Int]$Value
)
# Getting Place Value for the number passed
$numString = $Value.ToString()
$numLength = $numString.Length - 1
$firstNum = $numString.Substring(0,1)
$placeValue = "1"
$placeValue += for ($i = 0; $i -lt $numLength; $i++) {
$zero = "0"
$zero
}
$placeValue = $placeValue -replace " "
$placeValue = $placeValue -as [int]
$firstNum = $firstNum -as [int]
$finalNum = $firstNum * $placeValue
$finalNum = $finalNum/4
$finalNum
}
function Show-Cases {
param (
[Parameter(Mandatory)]
[ValidateSet('confirmed','deaths','recovered')]
[String]$Type
)
$skull = ConvertFrom-Unicode -FullUnicode 'U+1F480'
$sick = ConvertFrom-Unicode -FullUnicode 'U+1F637'
$recovered = ConvertFrom-Unicode -FullUnicode 'U+1F600'
$currentMonth = Get-Date -Format MMMM
$latest = Get-LatestCase -Country $Country -Type $Type
$live = Get-LiveCase -Country $Country -Type $Type
$step = Get-StepValue -Value $latest
switch ($Type) {
'confirmed' {
$icon = $sick
$barType = 'Bar'
$colorText = 'Yellow'
$colorMap = @{ 10000000 = 'Yellow' }
}
'deaths' {
$icon = $skull
$barType = 'Scatter'
$colorText = 'Red'
$colorMap = @{ 10000000 = 'Red' }
}
'recovered' {
$icon = $recovered
$barType = 'Line'
$colorText = 'Green'
$colorMap = @{ 10000000 = 'Green' }
}
Default { $colorMap = @{ 10000000 = 'Cyan' } }
}
Write-Loading
Write-Host "`n`t $($Type.ToUpper())" -ForegroundColor $colorText
Write-Loading -Newline
if ($showGraph -and $step -gt 5) {
Show-Graph -GraphTitle $currentMonth -Datapoints $live -Type $barType -XAxisStep 6 -YAxisStep $step -ColorMap $colorMap
}
Write-Host " TOTAL `t`t$latest $icon"
}
$defaultPP = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$dateString = (get-date -DisplayHint date | Out-String).Trim()
Write-Loading
Write-Host "`n`t$dateString" -ForegroundColor Magenta
Write-Loading
Write-Host "`n`t$($Country.ToUpper()) COVID19 STATUS" -ForegroundColor Cyan
# Confirmed Cases ===============================================
Show-Cases -Type 'confirmed'
# Death Cases ===================================================
Show-Cases -Type 'deaths'
# Recovered Cases ================================================
Show-Cases -Type 'recovered'
Write-Loading
$ProgressPreference = $defaultPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment