summaryrefslogtreecommitdiff
path: root/wa
diff options
context:
space:
mode:
authorAnton Bobov <abobov@gmail.com>2016-03-16 20:04:34 +0500
committerAnton Bobov <abobov@gmail.com>2016-03-16 20:04:34 +0500
commit6fd535ee6cd26181cee19b8771f21b0038907480 (patch)
treef6eac8065c6e4aaac1a0b1499141c4902f5cbe2b /wa
parent12273645b13d06725852034bbed146a5c2ebeeae (diff)
Add WolframAlpha cli client.
Diffstat (limited to 'wa')
-rwxr-xr-xwa54
1 files changed, 54 insertions, 0 deletions
diff --git a/wa b/wa
new file mode 100755
index 0000000..dc88aaf
--- /dev/null
+++ b/wa
@@ -0,0 +1,54 @@
+#!/bin/bash
+#
+# by Sairon Istyar, 2012
+# distributed under the GPLv3 license
+# http://www.opensource.org/licenses/gpl-3.0.html
+#
+
+API_KEY_FILE=$HOME/.wolfram_api_key
+
+if [ ! -f "$API_KEY_FILE" ] ; then
+ echo "No key file: $API_KEY_FILE" >&2
+ exit 1
+fi
+. "$API_KEY_FILE"
+if [ -z "$API_KEY" ] ; then
+ echo "No API_KEY set in key file: $API_KEY_FILE" >&2
+ exit 2
+fi
+
+# properly encode query
+q=$(echo ${*} | sed 's/+/%2B/g' | tr '\ ' '\+')
+
+# fetch and parse result
+result=$(curl -s "http://api.wolframalpha.com/v2/query?input=${q}&appid=${API_KEY}&format=plaintext")
+
+if [ -n "$(echo ${result} | grep 'Invalid appid')" ] ; then
+ echo "Invalid API key!"
+ echo "Get one at https://developer.wolframalpha.com/portal/apisignup.html"
+ echo -n 'Enter your WolframAlpha API key:'
+ read api_key
+ echo "API_KEY=${api_key}" >> ~/.wolfram_api_key
+ exit 1
+fi
+
+result=`echo "${result}" \
+ | tr '\n' '\t' \
+ | sed -e 's/<plaintext>/\'$'\n<plaintext>/g' \
+ | grep -oE "<plaintext>.*</plaintext>|<pod title=.[^\']*" \
+ | sed -e 's!<plaintext>!!g; \
+ s!</plaintext>!!g; \
+ s!<pod title=.*!\\\x1b[1;36m&\\\x1b[0m!g; \
+ s!<pod title=.!!g; \
+ s!\&amp;!\&!g; \
+ s!\&lt;!<!g; \
+ s!\&gt;!>!g; \
+ s!\&quot;!"!g' \
+ -e "s/\&apos;/'/g" \
+ | tr '\t' '\n' \
+ | sed '/^$/d; \
+ s/\ \ */\ /g; \
+ s/\\\:/\\\u/g'`
+
+# print result
+echo -e "${result}" | less