2020-07-22 14:43:23 +02:00
|
|
|
#!/usr/bin/sh
|
2018-08-05 18:53:07 +02:00
|
|
|
|
|
|
|
## WolframAlpha CLI
|
|
|
|
|
|
|
|
# load api key
|
|
|
|
token="${XDG_CONFIG_HOME:-$HOME}/wolfram"
|
|
|
|
source $token
|
|
|
|
|
|
|
|
# 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}" >> $token
|
|
|
|
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!^! !g; \
|
|
|
|
s!<pod title=.!\n!g; \
|
|
|
|
s!\&!\&!g; \
|
|
|
|
s!\<!<!g; \
|
|
|
|
s!\>!>!g; \
|
|
|
|
s!\"!"!g' \
|
|
|
|
-e "s/\'/'/g" \
|
|
|
|
| tr '\t' '\n' \
|
|
|
|
| sed '/^$/d; \
|
|
|
|
s/\ \ */\ /g; \
|
|
|
|
s/\\\:/\\\u/g'`
|
|
|
|
|
|
|
|
# print result
|
|
|
|
echo -e "${result}"
|