Skip to content

Instantly share code, notes, and snippets.

@josheinstein
Last active December 16, 2015 16:29
Show Gist options
  • Save josheinstein/5463114 to your computer and use it in GitHub Desktop.
Save josheinstein/5463114 to your computer and use it in GitHub Desktop.
A PowerShell implementation of my Wolfram Alpha API search script. Unlike the Bash version, this one doesn't use XSLT to produce the output so it's easier to read.
# Module manifest for module 'WolframAlpha'
# Generated by: Josh Einstein
# Generated on: 4/25/2013
@{
RootModule = 'WolframAlpha.psm1'
ModuleVersion = '1.0'
GUID = 'bddce958-afa6-46d5-baa4-a277e0f9ccc1'
Author = 'Josh Einstein'
CompanyName = 'Einstein Technologies'
Copyright = '(c) 2013 Josh Einstein. All rights reserved.'
Description = 'Wolfram Alpha Query'
DotNetFrameworkVersion = '4.0'
CLRVersion = '4.0'
RequiredAssemblies = @()
TypesToProcess = @()
FormatsToProcess = @()
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
}
$WA_APPID = 'YOUR APPID' # get one at https://developer.wolframalpha.com/portal/apisignup.html
$WA_APIROOT = 'http://api.wolframalpha.com/v2/query'
#.SYNOPSIS
# Searches Wolfram Alpha for the specified query and writes the results as
# text or xml to the pipeline.
#.EXAMPLE
# Search-WolframAlpha 'today'
function Search-WolframAlpha {
[CmdletBinding(DefaultParameterSetName='Text')]
param(
# The Wolfram Alpha query
[Parameter(Position=1, Mandatory=$true)]
[String]$Query,
# If specified, the XML of the result is returned instead
# of strings being written to the pipeline
[Parameter(ParameterSetName='XML')]
[Switch]$XML,
# If specified, returns the simple result of the query
# instead of printing out a detailed result.
[Parameter(ParameterSetName='Result')]
[Switch]$Result
)
process {
$URI = [Uri]"${WA_APIROOT}?input=$([Uri]::EscapeDataString($Query))&appid=${WA_APPID}&format=plaintext"
$Response = Invoke-WebRequest -Uri:$URI -UseBasicParsing
$QueryResult = ([XML]$Response.Content).QueryResult
if ($XML) {
Write-Output $QueryResult
}
elseif ($Result) {
$ResultNode = $QueryResult.SelectSingleNode("//pod[@id='Result' and @primary='true']/subpod[@primary='true']/plaintext/text()")
Write-Output $ResultNode.Value
}
else {
Write-Output ""
Write-Output "RESULT (Success: $($QueryResult.Success))"
Write-Output "=========================================="
foreach ($Pod in @($QueryResult.pod)) {
Write-Output ""
Write-Output $Pod.title
foreach ($Text in $Pod.SelectNodes('.//plaintext/text()')) {
Write-Output " $($Text.Value -replace '\n',"`n ")"
}
}
Write-Output ""
}
}
}
Set-Alias -Name wa -Value Search-WolframAlpha -Option AllScope -Scope Global
Export-ModuleMember -Function Search-WolframAlpha
Export-ModuleMember -Alias wa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment