Get-JackPot.ps1
<#PSScriptInfo
.VERSION 1.0.0 .GUID 0fd916fe-3a0d-48c4-a196-18ea085e071f .AUTHOR Craig Dayton .COMPANYNAME Example.com .COPYRIGHT Absolute Zero .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .SYNOPSIS Display the current Washington State lottery game results and optionally stores the results in a local history file. .DESCRIPTION Invokes a web request to http://www.walottery.com/WinningNumbers and extracts the game results onto the console and optionally saves the results locally. .PARAMETER game Displays all the history results of a specified lottery game in the Out-Gridview. .PARAMETER online Displays the current lottery game results on the console. .PARAMETER update Displays the current lottery game results on the console and updates the history file. .PARAMETER all Displays all the history lottery game results in a Out-Gridview .INPUTS A history csv file named, JackPot-Results.csv http://www.walottery.com/WinningNumbers .OUTPUTS A history csv file named, JackPot-Results.csv .EXAMPLE Get-JackPot Displays the last 12 game history records on the console. .EXAMPLE Get-JackPot -game PowerBall Displays all the PowerBall records in the history file. .EXAMPLE Get-JackPot -online Queries the lottery web page and then extracts and displays the current game results. .EXAMPLE Get-Update -update Queries the lottery web page and then extracts and displays the current game results. The history file is then updated with new game results. .NOTES Author: Craig Dayton Updated: 03/22/2017 - initial release. #> # Get-JackPot Params [cmdletbinding()] Param( [Parameter(Position=0, Mandatory=$false, HelpMessage = "Enter a lottery game name (i.e. PowerBall)", ValueFromPipeline=$True)] #[ValidateNotNullorEmpty("^[a-zA-Z]{12}$")] [string]$game, [Parameter(Position=1, Mandatory=$false, HelpMessage = "Display Online Lottery Results", ValueFromPipeline=$True)] [ValidateNotNullorEmpty()] [switch]$online, [Parameter(Position=2, Mandatory=$false, HelpMessage = "Display Online Lottery Results & update history file", ValueFromPipeline=$True)] [ValidateNotNullorEmpty()] [switch]$update, [Parameter(Position=3, Mandatory=$false, HelpMessage = "Display all game history file records", ValueFromPipeline=$True)] [ValidateNotNullorEmpty()] [switch]$all ) # # Declarations $URI1 = "http://www.walottery.com/WinningNumbers"; [String[]]$JackPotHeader = "Game", "DrawDate","DrawDay", "Numbers", "Multiplier", "JackPot", "NextDraw", "NextDay"; # # Functions function Convert-fileToCSV { param ([string]$fname) $bufLine = $null; $game = $Global:game; [bool]$needNums = $true; $DrawDate, $DrawDay, $Numbers, $Multiplier, $Prize, $NextDate, $NextDay = $null; get-content -Path $fname | ForEach-Object { # process each input line # evaluate any non-blank line if ($_ -notmatch "^ ") { $curLine = $_; if ($curLine -match "Latest Draw:" -and $DrawDate -eq $null) { $junk,$ld = $curline.Split(":"); $day,$date = $ld.Split("/"); $DrawDay = $day; $DrawDate = Get-Date $date -format yyyy-MM-dd; } if ($curLine -match "^\d" -and $needNums) { $Numbers += $curLine + " "; } if ($curLine -match "Power Play" -and $Multiplier -eq $null) { $Multiplier = $curLine.Substring(11,3); } if ($curLine -match "Megaplier" -and $Multiplier -eq $null) { $Multiplier = $curLine.Substring(10,3); } if ($curLine -match "[$]" -and $Prize -eq $null) { $needNums = $false; $Prize = $curLine; } if ($curLine -match "Next Draw:" -and $NextDate -eq $null) { $junk,$ld = $curline.Split(":"); $day,$date = $ld.Split("/"); if ($date -ne $null ) { $NextDay = $day; $NextDate = Get-Date $date -format yyyy-MM-dd; } else { $NextDate = $day; }; } if ($curLine -match "Daily" -and $NextDate -eq $null) { $NextDate = $curLine}; } } $bufLine = $game + ";" + $DrawDate + ";" + $DrawDay + ";" + $Numbers + ";"; $bufLine += $Multiplier + ";" + $Prize + ";" + $NextDate + ";" + $NextDay; $bufLine | Out-File -FilePath $temp2 -Append -Encoding ascii; if (Test-Path $fname) { Remove-Item -Path $fname }; } function Update-JackPotHistory { param ([string]$fname) if (Test-Path $JackPot ) { $histLine = Get-Content -Path $JackPot | Select-Object -Last 1; $currLine = Get-Content -Path $fname | Select-Object -Last 1; if ($histLine -ne $currLine) { # Update history file $histLine = Get-Content -Path $JackPot | Select-Object -Last 6; [bool]$duplicate = $false; get-content -Path $fname | ForEach-Object { # process each input line $curLine = $_; # Elimate duplcate entries in the history file $histLine | ForEach-Object { if ($curLine -eq $_) { $duplicate = $true } } if (!($duplicate)) { $curLine | Out-File -FilePath $JackPot -Append -Encoding ascii; } } } } else { get-content -Path $fname | ForEach-Object { # process each input line $_ | Out-File -FilePath $JackPot -Append -Encoding ascii; } } } # # Main Routine $sPath = Get-Location; $temp1 = "$sPath\temp1.txt"; $temp2 = "$sPath\temp2.txt"; $JackPot = "$sPath\JackPot-Results.csv"; if (Test-Path $temp1 ) { Remove-Item -Path $temp1 }; if (Test-Path $temp2 ) { Remove-Item -Path $temp2 }; if ($online -or $update) { $response = Invoke-WebRequest -URI $URI1; if ($response.StatusCode -eq "200") { Write-Progress -Activity "Processing response from $URI1 ..." -Status "Please wait" $Global:game = "PowerBall"; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-powerball" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; $Global:game = "MegaMillions"; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-megamillions" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; Write-Progress -Activity "Go away" -Completed; $Global:game = "Lotto"; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-lotto" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; $Global:game = "Hit5"; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-hit5" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; $Global:game = "Match4"; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-match4" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; $Global:game = "DailyGame"; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-dailygame" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; <# commented out Keno $Global:game = "Keno"; $Global:ID = 7; Write-Host " Processing $Global:game results" -ForegroundColor Green $data = $($response.ParsedHtml.getElementsByTagName("div") | Where-Object classname -eq "game-bucket game-bucket-keno" ); $data.innerText | Out-File -FilePath $temp1 -Append -Encoding ascii; Convert-fileToCSV $temp1; #> $currentGames = Import-CSV -Path $temp2 -Delimiter ";" -Header $JackPotHeader; $currentGames | Format-Table -AutoSize -Wrap if ($update) { Update-JackPotHistory $temp2; }; if (Test-Path $temp2 ) { Remove-Item -Path $temp2 }; } else { Write-Host "Received error code: $response.StatusCode from $URI1"; } } elseif ($game) { $currentGames = Import-CSV -Path $JackPot -Delimiter ";" -Header $JackPotHeader | Where-Object {$_.Game -match $game }; if ($currentGames) { $currentGames | Out-GridView -Title $game } else { Write-Host "$game not found" -ForegroundColor Red Write-Host "Valid game names are: 'PowerBall, MegaMillions, Hit5, Match4, and DailyGame' " -ForegroundColor Green } } elseif ($all) { Import-CSV -Path $JackPot -Delimiter ";" -Header $JackPotHeader | Out-GridView -Title "Listing of lottery game records" } else { $currentGames = Import-CSV -Path $JackPot -Delimiter ";" -Header $JackPotHeader; $currentGames | Select-Object -Last 12 | Format-Table -AutoSize -Wrap; } # |