-
-
Save dziban303/6e50145082a938e07bdc68127f5067e6 to your computer and use it in GitHub Desktop.
Simple Wolfram Alpha queries from the Mac terminal. The XML parsing is incredibly basic (it just looks complicated because of the work needed to indent the lines to make it look good) so I haven't really tested it with any other results yet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url="http://api.wolframalpha.com/v2/query" | |
appid="YOUR APP ID" # get one at https://developer.wolframalpha.com/portal/apisignup.html | |
query="$@" | |
value="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$query")" | |
echo "Computing: $query" | |
curl -s -L -G -d "input=$value&appid=$appid&format=plaintext" $url >/tmp/wolfram-alpha.xml | |
xsltproc ~/Scripts/wolf.xslt /tmp/wolfram-alpha.xml | |
rm /tmp/wolfram-alpha.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>: wolf.sh "today" | |
RESULT (Success: true) | |
========================================== | |
Input interpretation | |
today | |
Result | |
Wednesday, April 24, 2013 | |
Date formats | |
04/24/2013 (month/day/year) | |
Time in 2013 | |
114th day | |
17th week | |
Observances for April 24 (United States) | |
(no official holidays or major observances) | |
(observances for the day include: Administrative Professionals' Day, ...) | |
Anniversaries for April 24, 2013 | |
birth of Lance Thomas (1988- ): 25th anniversary | |
birth of Jermaine Cunningham (1988- ): 25th anniversary | |
President Kennedy accepts responsibility for Bay of Pigs invasion (1961): 52nd anniversary | |
Mary, Queen of Scotland, marries French heir Francis II (1558): 455th anniversary | |
Discovery blasts off with Hubble Space Telescope (1990): 23rd anniversary | |
Daylight information for April 24 in Philadelphia, Pennsylvania | |
sunrise | 6:11 am EDT (12 hours 24 minutes ago) | |
sunset | 7:50 pm EDT (1 hour 15 minutes from now) | |
duration of daylight | 13 hours 39 minutes | |
Phase of the Moon | |
waxing gibbous moon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0'?> | |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> | |
<xsl:output omit-xml-declaration="yes" indent="no"/> | |
<xsl:output method="text"/> | |
<xsl:variable name="indent" select="'
    '" /> | |
<xsl:template match="/"> | |
<xsl:apply-templates select="//queryresult"/> | |
</xsl:template> | |
<xsl:template match="queryresult[@success='true']"> | |
<xsl:text>
</xsl:text> | |
<xsl:text>RESULT (Success: </xsl:text> | |
<xsl:value-of select="@success"/> | |
<xsl:text>)</xsl:text> | |
<xsl:text>
</xsl:text> | |
<xsl:text>==========================================</xsl:text> | |
<xsl:text>
</xsl:text> | |
<xsl:for-each select="pod"> | |
<xsl:text>
</xsl:text> | |
<xsl:value-of select="@title" /> | |
<xsl:for-each select=".//plaintext"> | |
<xsl:value-of select="$indent" /> | |
<xsl:call-template name="insertBreaks"> | |
<xsl:with-param name="pText" select="text()" /> | |
</xsl:call-template> | |
</xsl:for-each> | |
<xsl:text>
</xsl:text> | |
</xsl:for-each> | |
<xsl:text>
</xsl:text> | |
</xsl:template> | |
<xsl:template name="insertBreaks"> | |
<xsl:param name="pText" select="."/> | |
<xsl:choose> | |
<xsl:when test="not(contains($pText, '
'))"> | |
<xsl:copy-of select="$pText"/> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="substring-before($pText, '
')"/> | |
<xsl:value-of select="$indent" /> | |
<xsl:call-template name="insertBreaks"> | |
<xsl:with-param name="pText" select="substring-after($pText, '
')"/> | |
</xsl:call-template> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment