CoronaModule_US_State.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

function Get-CoronaStateInfo {
    <#
    .SYNOPSIS
        Get general details about state specific Corona information resources
    .DESCRIPTION
        Get general details about state specific Corona information resources
    .EXAMPLE
        PS C:\> Get-CoronaStateInfo
        Get state info for all states
    .EXAMPLE
        PS C:\> Get-CoronaStateInfo -StateAbbreviation CA
        Get state info for California only
    .OUTPUTS
        state - State or territory postal code abbreviation.
        name - Full state or territory name.
        fips - Federal Information Processing Standard state code
        covid19Site - Webpage dedicated to making results available to the public. More likely to contain numbers. We make regular screenshots of this URL.
        covid19SiteSecondary - Typically more informational.
        twitter - Twitter for the State Health Department
        pui - Person Under Investigation; it is meant to capture positive, negative, and pending test results.
        pum - Person Under Monitoring; we don’t collect these numbers as they are reported far less consistently across states
        notes - Notes about the information available and how we collect or record it.
    .NOTES
        Thanks covidtracking.com: https://covidtracking.com/api/states/info
    #>


    param (
        [Parameter(Mandatory=$false)]
        [ValidateScript({Confirm-StateAbbreviation $_})]
        [string]
        $StateAbbreviation
    )

    $StatesInfo = Invoke-RestMethod -Method Get -Uri "https://covidtracking.com/api/states/info"

    #Filter data if State abbreviation was specified
    if ($StateAbbreviation) {
        $SpecificStateInfo = $StatesInfo | Where-Object -Property state -EQ $StateAbbreviation
        $SpecificStateInfo
    }
    else {
        $StatesInfo
    }
}

function Get-CoronaStateData {
    <#
    .SYNOPSIS
        Get US State Corona virus tracking data from covidtracking.com
    .DESCRIPTION
        Get 'States Current Values' or 'States Historical Data' from covidtracking.com. Use the -Historical switch to get daily data values. Use -StateAbbreviation to get data only for a specific state.
    .EXAMPLE
        PS C:\> Get-CoronaStateData
        Get current Corona virus state data for all states.
    .EXAMPLE
        PS C:\> Get-CoronaStateData -StateAbbreviation CA
        Get current Corona virus state data for the state of California
    .EXAMPLE
        PS C:\> Get-CoronaStateData -Historical
        Get historical (daily) Corona virus data per state for all states
    .EXAMPLE
        PS C:/> Get-CoronaStateData
        Get current Corona virus state data for all states
    .OUTPUTS
        Properties returned for current state data (without -Historical switch):
            state - State or territory postal code abbreviation.
            positive - Total cumulative positive test results.
            positiveScore - +1 for reporting positives reliably.
            negative - Total cumulative negative test results.
            negativeScore - +1 for reporting negatives sometimes.
            negativeRegularScore - +1 for reporting negatives reliably.
            commercialScore - +1 for reporting all commercial tests.
            score - Total reporting quality score.
            grade - Letter grade based on score.
            totalTestResults - Calculated value (positive + negative) of total test results.
            hospitalized - Total cumulative number of people hospitalized.
            death - Total cumulative number of people that have died.
            dateModified - ISO 8601 date of the time the data was last updated by the state.
            dateChecked - ISO 8601 date of the time we last visited their website
            total - DEPRECATED Will be removed in the future. (positive + negative + pending). Pending has been an unstable value and should not count in any totals.

        Properties returned for historical (daily) state data (with -Historical switch):
            state - State or territory postal code abbreviation.
            positive - Total cumulative positive test results.
            positiveIncrease - Increase from the day before.
            negative - Total cumulative negative test results.
            negativeIncrease - Increase from the day before.
            pending - Tests that have been submitted to a lab but no results have been reported yet.
            totalTestResults - Calculated value (positive + negative) of total test results.
            totalTestResultsIncrease - Increase from the day before.
            hospitalized - Total cumulative number of people hospitalized.
            hospitalizedIncrease - Increase from the day before.
            death - Total cumulative number of people that have died.
            deathIncrease - Increase from the day before.
            dateChecked - ISO 8601 date of the time we saved visited their website
            total - DEPRECATED Will be removed in the future. (positive + negative + pending). Pending has been an unstable value and should not count in any totals.
    .NOTES
        Thank you covidtracking.com
        https://covidtracking.com/api/states"
        https://covidtracking.com/api/states/daily
    #>


    param (
        [Parameter(Mandatory=$false)]
        [ValidateScript({Confirm-StateAbbreviation $_})]
        [string]
        $StateAbbreviation,
        [Parameter(Mandatory=$false)]
        [switch]
        $Historical
    )

    #Get states data from covidtracking.com
    if (-not $Historical) {
        $StatesData = Invoke-RestMethod -Method Get -Uri "https://covidtracking.com/api/states"
    }
    else {
        $StatesData = Invoke-RestMethod -Method Get -Uri "https://covidtracking.com/api/states/daily"
    }
    
    #Filter data if State abbreviation was specified
    if ($StateAbbreviation) {
        $SpecificStateData = $StatesData | Where-Object -Property state -EQ $StateAbbreviation
        $SpecificStateData
    }
    else {
        $StatesData
    }
}

function Get-CoronaStateDeathRate {
    <#
    .SYNOPSIS
        Get Corona death rate per state
    .DESCRIPTION
        Calculate the death rate using data from covidtracking.com.
        deathrate = (death/positive)*100
    .EXAMPLE
        PS C:\> Get-CoronaStateDeathRate
        Get Corona virus death rate for all states using current data
    .EXAMPLE
        PS C:\> Get-CoronaStateDeathRate -StateAbbreviation CA
        Get Corona virus death rate for the state of California
    .EXAMPLE
        PS C:\> Get-CoronaStateDeathRate -Historical
        Get historical Corona virus death rate for all states
    .EXAMPLE
        PS C:\> Get-CoronaStateDeathRate -StateAbbreviation CA -Historical
        Get historical Corona virus death rate for the state of California
    .OUTPUTS
            state
            date
            positive - Total cumulative positive test results.
            death - Total cumulative number of people that have died.
            deathrate - (death/positive)*100
    #>

    param (
        [Parameter(Mandatory=$false)]
        [ValidateScript({Confirm-StateAbbreviation $_})]
        [string]
        $StateAbbreviation,
        [Parameter(Mandatory=$false)]
        [switch]
        $Historical
    )
    
    $DeathRateData = New-Object -TypeName System.Collections.ArrayList
    #Get state data using Get-CoronaStateData
    if (-not $Historical) {
        if ($StateAbbreviation) {
            $StatesData = Get-CoronaStateData -StateAbbreviation $StateAbbreviation
        }
        else {
            $StatesData = Get-CoronaStateData
        }
    }
    else {
        if ($StateAbbreviation) {
            $StatesData = Get-CoronaStateData -StateAbbreviation $StateAbbreviation -Historical
        }
        else {
            $StatesData = Get-CoronaStateData -Historical
        }
    }

    #Calculate death rate (positive/death)
    foreach ($item in $StatesData) {
        #Properties for result object
        $TotalPositive = $item.positive
        $TotalDeath = $item.death

        if ($item.date) {
            $date = $item.date
        }
        else {
            $date = Get-Date -UFormat "%Y%m%d"
        }

        if ( ($TotalPositive -gt 0) -and ($TotalDeath -gt 0) ) {
            $DeathRate = [System.Math]::Round(($TotalDeath/$TotalPositive)*100,2)
        }
        
        $obj = [PSCustomObject]@{
            state=$item.state
            date=$date
            positive=$TotalPositive
            death=$TotalDeath
            deathrate=$DeathRate
        }

        $DeathRateData.Add($obj) | Out-Null
    }

    $DeathRateData
}

function Get-CoronaStateTrackerUrl {
    <#
    .SYNOPSIS
        Get tracking website URL for state Corona information resources
    .DESCRIPTION
        Get tracking website URL for state Corona information resources
    .EXAMPLE
        PS C:\> Get-CoronaStateTrackerUrl
        Get state info for all states
    .EXAMPLE
        PS C:\> Get-CoronaStateTrackerUrl -StateAbbreviation CA
        Get state info for California only
    .OUTPUTS
        name: State Name
        stateId: State or territory postal code abbreviation.
        url: String
        kind: String
        filter: String
        headers: Object
        navigate: String
        options: Object
        ssl_no_verify: Boolean
    .NOTES
        Thanks covidtracking.com: https://covidtracking.com/api/urls
    #>


    param (
        [Parameter(Mandatory=$false)]
        [ValidateScript({Confirm-StateAbbreviation $_})]
        [string]
        $StateAbbreviation
    )

    $StatesUrls = Invoke-RestMethod -Method Get -Uri "https://covidtracking.com/api/urls"

    #Filter data if State abbreviation was specified
    if ($StateAbbreviation) {
        $SpecificStateUrl = $StatesUrls | Where-Object -Property state -EQ $StateAbbreviation
        $SpecificStateUrl
    }
    else {
        $StatesUrls
    }
}

function Get-CoronaStateWebsiteScreenshot {
    <#
    .SYNOPSIS
        Get URL to screen shot of state Corona virus tracking websites.
    .DESCRIPTION
        Get the URL of the covidtracking.com screen shot of state Corona virus tracking websites.
    .EXAMPLE
        PS C:\> Get-CoronaStateWebsiteScreenshot
        Get list of daily screenshots for all states
    #>

    
    $StateWebsiteScreenshot = Invoke-RestMethod -Method Get -Uri "https://covidtracking.com/api/screenshots"
    $StateWebsiteScreenshot
}

function Confirm-StateAbbreviation {
    param (
        [Parameter(Mandatory=$true)]
        [ValidatePattern("[A-Z][A-Z]")]
        [string]
        $StateAbbreviation
    )

    # validate state abreviation code against COVID tracking project current state data
    $StateData = Invoke-RestMethod -Uri "https://covidtracking.com/api/states"
    if ($StateData.state -contains $StateAbbreviation) {
        $true
    }
    else {
        $false
    }
}