PoshCode Logo PowerShell Code Repository

Order pizza with PoSh by DollarUnderscore 20 months ago (modification of post by DollarUnderscore view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6016"></script>download | new post

This code is from a web scrape guide I’ve written on my blog.
It’s about interacting with a site where you can order pizza.

You’ll find part one of this guide here:
http://dollarunderscore.azurewebsites.net/?p=3831

And part two here:
http://dollarunderscore.azurewebsites.net/?p=3991

Happy automating anything :-)

Edit: The good people at onlinepizza.se has now enforced https on login, updated the code aswell now.

  1. function Connect-OnlinePizza
  2. {
  3.     [cmdletbinding()]
  4.     param(
  5.           [Parameter(Mandatory=$True)]
  6.           [System.Management.Automation.PSCredential] $Credential)
  7.  
  8.     $Username = $Credential.UserName
  9.     $Password = $Credential.GetNetworkCredential().Password
  10.  
  11.     $Request = @{'username' = $Username
  12.                  'password'= $Password
  13.                  'action'= 'loggain'}
  14.  
  15.     Invoke-WebRequest -Uri "https://onlinepizza.se/loggain" -Method Post -Body $Request -SessionVariable Global:OnlinePizzaSession -OutFile .\dump.htm
  16.  
  17.     $LoggedIn = Select-String -Path .\dump.htm -Pattern "inloggad som $Username" -Quiet
  18.  
  19.     Remove-Item .\dump.htm -Force -Confirm:$false -ErrorAction SilentlyContinue
  20.  
  21.     if ($LoggedIn) {
  22.         Write-Verbose "You are now logged in!"
  23.     }
  24.     else {
  25.         Write-Error "Login failed!"
  26.     }
  27. }
  28.  
  29. function Get-MyOnlinePizzaAccountInfo
  30. {
  31.     [cmdletbinding()]
  32.     param()
  33.  
  34.     BEGIN {
  35.         if ($OnlinePizzaSession -eq $null) {
  36.             Write-Error "You must first connect using the Connect-OnlinePizza cmdlet"
  37.             break
  38.         }
  39.     }
  40.  
  41.     PROCESS {
  42.  
  43.         Invoke-WebRequest -Uri "http://onlinepizza.se/?view=andraKonto" -Method Get -WebSession $Global:OnlinePizzaSession -OutFile .\dump.htm
  44.  
  45.         $AccountInfo = Get-Content .\dump.htm -Encoding UTF8
  46.  
  47.         Remove-Item .\dump.htm -Force -Confirm:$false -ErrorAction SilentlyContinue
  48.  
  49.         $Username = ((($AccountInfo | Select-String -Pattern "name=username id=username") -split "value=`"")[1] -split "`" />")[0]
  50.         $AccountHolderName = ((($AccountInfo | Select-String -Pattern "name=namn id=namn") -split "value=`"")[1] -split "`"/>")[0]
  51.         $AccountHolderMail = ((($AccountInfo | Select-String -Pattern "name=epost id=epost") -split "value=`"")[1] -split "`"/>")[0]
  52.         $AccountHolderStreet = ((($AccountInfo | Select-String -Pattern "name=adress1 id=adress1") -split "value=`"")[1] -split "`"/>")[0]
  53.         $AccountHolderPostalCode = ((($AccountInfo | Select-String -Pattern "name=postnummer id=postnummer") -split "value=`"")[1] -split "`"/>")[0]
  54.         $AccountHolderPhone = ((($AccountInfo | Select-String -Pattern "name=telefon id=telefon") -split "value=`"")[1] -split "`"/>")[0]
  55.  
  56.         $returnObject = New-Object System.Object
  57.         $returnObject | Add-Member -Type NoteProperty -Name Username -Value $Username
  58.         $returnObject | Add-Member -Type NoteProperty -Name Name -Value $AccountHolderName
  59.         $returnObject | Add-Member -Type NoteProperty -Name Email -Value $AccountHolderMail
  60.         $returnObject | Add-Member -Type NoteProperty -Name Address -Value $AccountHolderStreet
  61.         $returnObject | Add-Member -Type NoteProperty -Name PostalCode -Value $AccountHolderPostalCode
  62.         $returnObject | Add-Member -Type NoteProperty -Name Phone -Value $AccountHolderPhone
  63.  
  64.         Write-Output $returnObject
  65.  
  66.     }
  67.  
  68.     END { }
  69. }
  70.  
  71. function Get-PizzaRestaurant
  72. {
  73.     [cmdletbinding()]
  74.     param(
  75.           [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
  76.           [int] $PostalCode)
  77.  
  78.     BEGIN {
  79.         if ($OnlinePizzaSession -eq $null) {
  80.             Write-Error "You must first connect using the Connect-OnlinePizza cmdlet"
  81.             break
  82.         }
  83.     }
  84.  
  85.     PROCESS {
  86.  
  87.         Invoke-WebRequest -Uri "http://onlinepizza.se/postnummer/$PostalCode" -Method Get -WebSession $Global:OnlinePizzaSession -OutFile .\dump.htm
  88.  
  89.         $ResturantList = ((Get-Content .\dump.htm) -join "`n") -split "<UL>" | select -Skip 1
  90.  
  91.         Remove-Item .\dump.htm -Force -Confirm:$false -ErrorAction SilentlyContinue
  92.  
  93.         foreach ($Restaurant in $ResturantList) {
  94.  
  95.             $RestaurantName = (($Restaurant -split "<h4>")[1] -split "</h4>")[0]
  96.  
  97.             if ($RestaurantName -eq '') {
  98.                 Continue
  99.             }
  100.  
  101.             $RestaurantStreet = (($Restaurant -split "<address>")[1] -split "</address>")[0]
  102.             $OpeningHoursDelivery = ((($Restaurant -split "Utkörning:</strong><br />")[1] -split "<br />")[0]).Trim()
  103.             $OpeningHoursTakeAway = ((($Restaurant -split "Avhämtning:</strong><br />")[1] -split "<br />")[0]).Trim()
  104.             $RestaurantLink = ((($Restaurant -split "meny")[0] -split "href=`"")[1] -split "`"")[0]
  105.  
  106.             $returnObject = New-Object System.Object
  107.             $returnObject | Add-Member -Type NoteProperty -Name RestaurantName -Value $RestaurantName
  108.             $returnObject | Add-Member -Type NoteProperty -Name RestaurantStreet -Value $RestaurantStreet
  109.             $returnObject | Add-Member -Type NoteProperty -Name OpeningHoursDelivery -Value $OpeningHoursDelivery
  110.             $returnObject | Add-Member -Type NoteProperty -Name OpeningHoursTakeAway -Value $OpeningHoursTakeAway
  111.             $returnObject | Add-Member -Type NoteProperty -Name RestaurantLink -Value $RestaurantLink
  112.  
  113.             Write-Output $returnObject
  114.  
  115.             Remove-Variable RestaurantName, RestaurantStreet, OpeningHoursDelivery, OpeningHoursTakeAway, RestaurantLink -ErrorAction SilentlyContinue
  116.         }
  117.     }
  118.  
  119.     END { }
  120. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me