Covid19Stats.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<#
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.172
     Created on: 4/4/2020 8:06 PM
     Created by: DataTraveler1
     Organization:
     Filename: Covid19Stats.psm1
    -------------------------------------------------------------------------
     Module Name: Covid19Stats
    ===========================================================================
#>


function Get-Covid19Stats
{
    [CmdletBinding()]
    Param (
        [switch]$help,
        [switch]$global,
        [string]$country,
        [string]$state
    )
    function Send-API-Command
    {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory = $true)]
            [string]$command
        )
        $uri = ($covid_api_base_url + $command)
        $Error.Clear()
        Try
        {
            If ($debug_mode -eq $true)
            {
                Write-Host "Debug: Sending API command [GET] to [$uri]"
            }
            $api_response = Microsoft.PowerShell.Utility\Invoke-RestMethod -Uri $uri -Method "GET"
        }
        Catch
        {
            Write-Host ("Error - " + $Error.Exception.Message)
            Exit
        }
        Return $api_response
    }
    
    function Get-Cover19Stats_Global
    {
        $api_results = Send-API-Command -command "all"
        Return $api_results
    }
    
    function Get-Cover19Stats_Countries
    {
        $api_results = Send-API-Command -command "countries"
        Return $api_results
    }
    
    function Get-Country
    {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory = $true)]
            [string]$country_name
        )
        If ($country_name -eq "United States" -or $country_name -eq "United States of America") { $country_name = "USA" }
        $country_data = Get-Cover19Stats_Countries
        $country_stats = $country_data | Where-Object { $_.country -eq $country_name }
        Return $country_stats
    }
    
    function Get-Cover19Stats_States
    {
        $api_results = Send-API-Command -command "states"
        Return $api_results
    }
    
    function Get-State
    {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory = $true)]
            [string]$state_name
        )
        $state_data = Get-Cover19Stats_States
        $state_stats = $state_data | Where-Object { $_.state -eq $state_name }
        Return $state_stats
    }
    
    function Convert-UnixTime
    {
        [CmdletBinding()]
        Param (
            [double]$unix_time
        )
        $utc_time = (Get-Date -Date '1970/1/1').AddMilliseconds($unix_time)
        $local_time = (Get-Date -Date $utc_time).AddMinutes($timezone_utc_offset_minutes)
        $local_time_formatted = Get-Date -Date $local_time -Format "yyyy-MM-dd HH:mm:ss"
        Return $local_time_formatted
    }
    
    $debug_mode = [boolean]$false
    $covid_api_base_url = [string]"https://corona.lmao.ninja/"
    $timezone = Get-TimeZone
    $timezone_utc_offset_minutes = $timezone.BaseUtcOffset.totalminutes
    $timezone_dst = $timezone.SupportsDaylightSavingTime
    If ($timezone_dst -eq $true) { $timezone_utc_offset_minutes = $timezone_utc_offset_minutes + 60 }
    
    If ($help -eq $true)
    {
        Write-Host "Examples:"
        Write-Host ""
        Write-Host ""
        Write-Host 'Get-Covid19Stats -global'
        Write-Host 'Get-Covid19Stats -country "USA"'
        Write-Host 'Get-Covid19Stats -country "China"'
        Write-Host 'Get-Covid19Stats -state "North Dakota"'
        Write-Host 'Get-Covid19Stats -state "Florida"'
        Write-Host ""
        Return
    }
    ElseIf ($global -eq $true)
    {
        $output = Get-Cover19Stats_Global
        $new_output = @{ cases = ("{0:N0}" -f ($output.cases)); deaths = ("{0:N0}" -f ($output.deaths)); recovered = ("{0:N0}" -f ($output.recovered)); updated = (Convert-UnixTime -unix_time ([double]$output.updated)); }
        
        $final_output = "Cases: " + [string]$new_output.cases + " Recovered: " + [string]$new_output.recovered + " Deaths: " + [string]$new_output.deaths + " Updated: " + $new_output.updated
    }
    ElseIf ($country.length -gt 0)
    {
        $country_data = Get-Cover19Stats_Countries
        $country_stats = Get-Country -country_name $country
        $new_output = @{ country = ($country_stats.country); cases = "{0:N0}" -f ($country_stats.cases); todayCases = "{0:N0}" -f ($country_stats.todayCases); deaths = "{0:N0}" -f ($country_stats.deaths); todayDeaths = "{0:N0}" -f ($country_stats.todayDeaths); recovered = "{0:N0}" -f ($country_stats.recovered); active = "{0:N0}" -f ($country_stats.active); critical = "{0:N0}" -f ($country_stats.critical); casesPerOneMillion = "{0:N0}" -f ($country_stats.casesPerOneMillion); }
        If (($country_stats.country.length -gt 0))
        {
            $final_output = "Country: " + $new_output.country + " Cases: " + $new_output.cases + " Today-Cases: " + $new_output.todayCases + " Deaths: " + $new_output.deaths + " Today-Deaths: " + $new_output.todayDeaths + " Recovered: " + $new_output.recovered + " Active: " + $new_output.active + " Critical: " + $new_output.critical + " Cases-Per-One-Million: " + $new_output.casesPerOneMillion
        }
        Else
        {
            $final_output = "Invalid data received. Try again"
        }
        
    }
    ElseIf ($state.length -gt 0)
    {
        $state_data = Get-Cover19Stats_States
        $state_stats = Get-State -state_name $state
        $new_output = @{ state = ($state_stats.state); cases = "{0:N0}" -f ($state_stats.cases); todayCases = "{0:N0}" -f ($state_stats.todayCases); deaths = "{0:N0}" -f ($state_stats.deaths); todayDeaths = "{0:N0}" -f ($state_stats.todayDeaths); recovered = "{0:N0}" -f ($state_stats.recovered); active = "{0:N0}" -f ($state_stats.active); }
        If (($state_stats.state.length -gt 0))
        {
            $final_output = "State: " + $new_output.state + " Cases: " + $new_output.cases + " Today-Cases: " + $new_output.todayCases + " Deaths: " + $new_output.deaths + " Today-Deaths: " + $new_output.todayDeaths + " Recovered: " + $new_output.recovered + " Active: " + $new_output.active
        }
        Else
        {
            $final_output = "Invalid data received. Try again"
        }
    }
    $current_date = Get-Date -Format "[yyyy-MM-dd HH:mm:ss] "
    $final_output = ( $current_date + $final_output)
    Return $final_output
}