GenXdev.Queries.Text.psm1
############################################################################### <# .SYNOPSIS Performs a "Wikipedia summary" text query .DESCRIPTION Performs a "Wikipedia summary" text query .PARAMETER Queries The query to perform #> function Get-WikipediaSummary { [CmdletBinding()] [Alias("wikitxt")] param( [Alias("q", "Value", "Name", "Text", "Query")] [Parameter( Mandatory = $True, Position = 0, ValueFromRemainingArguments = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [string[]] $Queries ) begin { } process { function fixWiki ([string]$text) { $input | ForEach-Object -Process { $i = $_.IndexOf("(") if ($i -lt 150) { if ($result.Length - 1 -eq $i) { Write-Output $result.SubString(0, $i).Replace(" ", " "); } else { $end = $_.IndexOf(")", $i); $result = $_.Substring(0, $i) if ($end -lt $_.Length) { $result = $result + $_.Substring($end + 1) } Write-Output $result.Replace(" ", " "); } } } } foreach ($Query in $Queries) { $urlPart = [Uri]::EscapeUriString($Query.Replace("-", " ")) $url = ("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=1&explaintext=1&titles=" + $urlPart) $r = (Invoke-WebRequest -Uri $url -MaximumRedirection 20).Content | ConvertFrom-Json $memberName = ($r.query.pages | Get-Member | Where-Object -Property "MemberType" -EQ "NoteProperty" | Select-Object -ExpandProperty "Name" | Select-Object -First 1) $value = ($r.query.pages | Select-Object -ExpandProperty $memberName) $result = $value.extract if ((!$result) -or ($result -eq "")) { "Nothing found on `"$Query`".." continue; } try { $result = ($result | fixWiki) } catch { $result = $value.extract } $result } } } ############################################################################### <# .SYNOPSIS Returns a random affirmation text .DESCRIPTION Returns a random affirmation text .PARAMETER Speak Use text-to-speech to speak out affirmation #> function Get-NextAffirmations { [CmdletBinding()] [Alias("WhatAboutIt")] param( [Parameter( Mandatory = $False, Position = 0 )] [Switch] $Speak ) $affirmation = (Invoke-RestMethod https://www.affirmations.dev/ -TimeoutSec 2).affirmation; if ($Speak -eq $true) { say $affirmation } Write-Output $affirmation } ################################################################################ ################################################################################ ################################################################################ |