Skip to content

Instantly share code, notes, and snippets.

@srpomeroy
Last active February 7, 2018 05:50
Show Gist options
  • Save srpomeroy/db990c73d89da02edd5871955a41e7bf to your computer and use it in GitHub Desktop.
Save srpomeroy/db990c73d89da02edd5871955a41e7bf to your computer and use it in GitHub Desktop.
PowerShell script to parse MSNE event data
# Based on 2017 results: http://www.motorsportsne.com/
$urls = 'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2003%2026%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2004%2002%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2004%2030%20a-x%20removed%20a1%20a4%20no%20work_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2006%2004%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2007%2028%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2008%2006%20A-X_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2008%2018%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2008%2020%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2008%2027%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2009%2030%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2010%2021%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2011%2004%20a-x_fin.htm',
'https://axwaresystems.com/axorm/files/MotorSportsNE/2017%2011%2018%20a-x_fin.htm'
$allResults = @()
foreach ($url in $urls) {
$WebRequest = Invoke-WebRequest $url
$tables = @($WebRequest.ParsedHtml.getElementsByTagName("TABLE"))
$event = $url.split("/")[-1].Replace("%20","-").SubString(0,10)
$table = $tables[2] # results
$titles = @()
$rows = @($table.Rows)
foreach($row in $rows)
{
$cells = @($row.Cells)
if($cells[0].tagName -eq "TH")
{
$titles = @($cells | % { ("" + $_.InnerText).Trim() })
continue
}
$class = $titles[0].Split(' - ')[0]
$totalCones = 0
$resultObject = [Ordered] @{}
$resultObject["Event"] = $event
$nonRunCells = 7
$totalRuns = $cells.Count - $nonRunCells
$totalCells = $cells.Count
for($counter = 0; $counter -lt $cells.Count; $counter++)
{
switch ($counter) {
0{$title = "Position"}
1{$title = "Car_Number"}
2{$title = "Driver"}
3{$title = "Car"}
4{$title = "Car_Color"}
($totalCells - 2){$title = "Total"}
($totalCells - 1){$title = "Dif"}
default{$title = "Run_" + ($counter - 4)}
}
if(-not $title) { continue }
$value = ("" + $cells[$counter].InnerText).Trim()
$resultObject[$title] = $value
if(($title -like "Run_*") -and ($value -match '^\d*\.\d*\+\d*$')) {
$cones = $resultObject[$title].Split('+')[1]
$totalCones += $cones
}
}
$driver = $resultObject["Driver"]
if($driver -and ($driver -notlike "*,*")) {
$resultObject["Driver"] = $driver.Split(' ')[1] + ', ' + $driver.Split(' ')[0]
}
$resultObject["Car_Number"] = $resultObject["Car_Number"] + $class
$resultObject["Total_Cones"] = $totalCones
$allResults = $allResults + [PSCustomObject] $resultObject
}
}
$coneKillers_car = @()
foreach($item in $allResults) {
if($item.Driver) {
$driver = $item.Driver
$existing_record = $null
$existing_record = $coneKillers_car | Where-Object {$_.Driver -eq $driver -and $_.Car_Number -eq $item.Car_Number}
if($existing_record) {
$existing_record.Total_Cones += $item.Total_Cones
}
else {
$object = New-Object –TypeName PSObject –Prop (@{
'Driver'=$driver;
'Car_Number'=$item.Car_Number;
'Car'=$item.Car;
'Car_Color'=$item.Car_Color;
'Total_Cones'=$item.Total_Cones;
})
$coneKillers_car += $object
}
}
}
$coneKillers_driver = @()
foreach($item in $allResults) {
if($item.Driver) {
$driver = $item.Driver
$existing_record = $null
$existing_record = $coneKillers_driver | Where-Object {$_.Driver -eq $driver}
if($existing_record) {
$existing_record.Total_Cones += $item.Total_Cones
}
else {
$object = New-Object –TypeName PSObject –Prop (@{
'Driver'=$driver;
'Total_Cones'=$item.Total_Cones;
})
$coneKillers_driver += $object
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment