Private/Get-TrouwCartoon.ps1
|
function Get-TrouwCartoon { param ( [Parameter(Mandatory)] [string]$Uri, [Parameter(Mandatory)] [string]$Title ) $DutchCulture = New-Object -TypeName System.Globalization.CultureInfo -ArgumentList 'nl-NL' $PreviousDate = Get-Date curl --silent --location $Uri | Join-String -Separator ' ' | ConvertTo-HtmlDocument | Select-HtmlNode -CssSelector '.paywall' | Select-HtmlNode -XPath 'div' -All | ForEach-Object { $CurrentDiv = $_ $HasFigure = [bool]($CurrentDiv.SelectSingleNode('figure')) switch ($HasFigure) { $false { $DateText = $CurrentDiv.InnerText $Date = [DateTime]::ParseExact($DateText, 'd MMMM', $DutchCulture) if ($Date -gt $PreviousDate) { $Date = $Date.AddYears(-1) } $PreviousDate = $Date break } $true { $Image = $CurrentDiv | Select-HtmlNode -CssSelector 'img' | Select-Object -First 1 | ForEach-Object { $_.GetAttributeValue('src', 'FIXME') } [PSCustomObject][Ordered]@{ PSTypeName = 'UncommonSense.Trouw.Article' Url = $Image Date = $Date Title = $Title Body = $Image } break } } } } |