# encoding: ascii
# api: powershell
# title: Buying groceries
# description: Buying groceries with PowerShell, because why not? ;-)
# version: 0.1
# type: function
# author: DollarUnderscore
# license: CC0
# function: Connect-Mathem
# x-poshcode-id: 5036
# x-archived: 2014-12-15T17:49:46
# x-published: 2014-04-01T21:52:00
#
# This service is only available for people living in certain cities in Sweden. The rest of you can just look at it as sample code for doing something similiar.
# It contains a couple of advanced functions for buying groceries through a internet based service here. More information available at my blog:
# http://dollarunderscore.azurewebsites.net/?p=2031
# Even though this is mostly made to generate a few smiles, it actually works… :-)
# Happy automating anything, as always :-)
#
#========================================================================
# Created By: Anders Wahlqvist
# Website: DollarUnderscore (http://dollarunderscore.azurewebsites.net)
#========================================================================
function Connect-Mathem
{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[System.Management.Automation.PSCredential] $Credential)
$PostURL = "http://www.mathem.se/Account/Login"
$LoginRequest = @{'UserName'= $Credential.UserName ;'Password' = $Credential.GetNetworkCredential().Password }
$LoginData = Invoke-WebRequest -Uri $PostURL -Body $LoginRequest -Method POST -SessionVariable Global:MathemSession -UseBasicParsing
}
function Get-FoodItem
{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[Alias('ProductName')]
$SearchString)
BEGIN {
if ($MathemSession -eq $null) {
Write-Error "You must first connect using the Connect-Mathem cmdlet"
return
}
}
PROCESS {
$SearchURL = "http://www.mathem.se/WebServices/ProductService.asmx/SearchAndAddResult?searchText="
$SearchQuery = $SearchString -replace " ","+"
$SearchResults = Invoke-RestMethod -Uri ($SearchURL + $SearchQuery) -WebSession $MathemSession
foreach ($Product in $SearchResults) {
$returnObject = New-Object System.Object
$returnObject | Add-Member -Type NoteProperty -Name ProductID -Value $Product.ProductID
$returnObject | Add-Member -Type NoteProperty -Name ProductName -Value $([text.encoding]::utf8.getstring([text.encoding]::default.GetBytes($Product.ProductName)))
$returnObject | Add-Member -Type NoteProperty -Name Price -Value $Product.Price
$returnObject | Add-Member -Type NoteProperty -Name PricePerUnit -Value $Product.PricePerUnit
Write-Output $returnObject
}
}
}
function Add-FoodItem
{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[string] $ProductID,
[int] $NumberOfItems = 1)
BEGIN {
if ($MathemSession -eq $null) {
Write-Error "You must first connect using the Connect-Mathem cmdlet"
return
}
}
PROCESS {
$AddURL = "http://www.mathem.se/Pages/products/AddToCart.aspx?AddProduct=true&ProductID=$ProductID&noOfFooditem=$NumberOfItems"
Write-Verbose "Adding item $ProductID to the cart..."
$AddItem = Invoke-WebRequest -Uri $AddURL -WebSession $MathemSession -Method Get
}
END { }
}
function Get-FoodBasket
{
if ($MathemSession -eq $null) {
Write-Error "You must first connect using the Connect-Mathem cmdlet"
return
}
$FoodCartURL = "http://www.mathem.se/umbraco/Surface/MiniShoppingCartSurface/RenderCart"
$FoodBasket = Invoke-RestMethod -Uri $FoodCartURL -WebSession $MathemSession
$ItemsFoundInBasket = $FoodBasket.div.div.form.ul.li
$ProductsInBasket = @()
if ($ItemsFoundInBasket -ne $null) {
$ProductsInBasket += $ItemsFoundInBasket
}
if ($ProductsInBasket.count -gt 0) {
foreach ($Product in $ProductsInBasket) {
$returnObject = New-Object System.Object
$returnObject | Add-Member -Type NoteProperty -Name ProductID -Value $Product."data-highlight-id"
$returnObject | Add-Member -Type NoteProperty -Name ProductName -Value $($Product.table.tbody.tr.td | select -Skip 1 -First 1 | % { $_.a."#text" })
$returnObject | Add-Member -Type NoteProperty -Name ProductLink -Value $($Product.table.tbody.tr.td | select -Skip 1 -First 1 | % { $_.a.href })
$returnObject | Add-Member -Type NoteProperty -Name TotalPrice -Value $($Product.table.tbody.tr.td | select -Skip 2 -First 1 | % { $_.strong })
$returnObject | Add-Member -Type NoteProperty -Name NumberOfItems -Value $Product.table.tbody.tr.td.div.input.value
Write-Output $returnObject
}
}
}
function Remove-FoodItem
{
[cmdletbinding()]
param(
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[string] $ProductID)
begin {
if ($MathemSession -eq $null) {
Write-Error "You must first connect using the Connect-Mathem cmdlet"
return
}
}
PROCESS {
$DeleteURL = "http://www.mathem.se/umbraco/Surface/ShoppingCartSurface/Delete/$ProductID"
Write-Verbose "Removing item $ProductID from cart..."
$DeleteItem = Invoke-WebRequest -Uri $DeleteURL -WebSession $MathemSession -Method Get
}
END { }
}