Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CraigChamberlain/24be9c7ffb28a0c7f2b5cdaaed87c89a to your computer and use it in GitHub Desktop.
Save CraigChamberlain/24be9c7ffb28a0c7f2b5cdaaed87c89a to your computer and use it in GitHub Desktop.
Get UK Covid Protection Level From Post Code
param(
[ValidatePattern(" *[a-zA-z0-9]{2,4} +[a-zA-z0-9]{2,4} *")]
[Parameter(Position = 0, Mandatory = $true)]
[string]$postCode
)
BEGIN
{
$root = "https://coronavirus.data.gov.uk/search?postcode="
}
PROCESS{
try{
$encodedPostCode = $postCode.Trim().ToLower() -replace " +", "+"
$url = $root + $encodedPostCode
$page = Invoke-WebRequest -Uri $url -Method Get
# Get the Middle layer Support Output Area from the title
if($page.ParsedHtml.title -match "Daily summary \| ([a-zA-z, &]+)"){
$MSOA = $matches[1]
}
# Parse the covid tier and Local Authority from the dashboard
if (
(
$page.ParsedHtml.body.getElementsByTagName('div') |
? {$_.className -eq "postcode-lead"} |
Select-Object -First 1
).
getElementsByTagName('h3') |
Select-Object -ExpandProperty outerText |
? {$_.trim() -match "(?:Current alert level (?:in )*) *([a-zA-Z ]*?) *(MEDIUM$|VERY HIGH$|HIGH$|STAY AT HOME$|NATIONAL RESTRICTIONS$|PROTECTION LEVEL [0-4]$|Welsh restrictions apply.$|Northern Irish restrictions apply.$)"} ){
$tier = $matches[2]
$localAuthority = $matches[1]
}
[pscustomobject]@{
msoa = $MSOA
localAuthority = $localAuthority
covidTier = $tier
}
}catch{
Write-Warning "Problem with: $postCode"
}
}
END{
}
# LA10 5AU England
# SW1A 2AA
# NE46 3NB
# SY23 4LU Wales
# BT2 7AF Northern Ireland
# EH4 2GH Scotland
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment