42 lines
1.2 KiB
Bash
Executable File
42 lines
1.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
## WolframAlpha CLI
|
|
|
|
# load api key
|
|
token="$XDG_CONFIG_HOME/wolfram"
|
|
. "$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 echo "$result" | grep -q 'Invalid appid'; then
|
|
echo "Invalid API key!"
|
|
echo "Get one at https://developer.wolframalpha.com/portal/apisignup.html"
|
|
printf 'Enter your WolframAlpha API key:'
|
|
read -r api_key
|
|
echo "API_KEY=${api_key}" >> "$token"
|
|
exit 1
|
|
fi
|
|
|
|
echo "$result" |
|
|
tr '\n' '\t' |
|
|
sed -e 's/<plaintext>/\n<plaintext>/g' |
|
|
grep -oE "<plaintext>.*</plaintext>|<pod title=.[^\']*" |
|
|
sed -e 's!<plaintext>!!g' \
|
|
-e 's!</plaintext>!!g' \
|
|
-e 's!<pod title=.*!\x1b[1;36m&\x1b[0m!g' \
|
|
-e 's!^! !g' \
|
|
-e 's!<pod title=.!\n!g' \
|
|
-e 's!\&!\&!g' \
|
|
-e 's!\<!<!g' \
|
|
-e 's!\>!>!g' \
|
|
-e 's!\"!"!g' \
|
|
-e "s/\'/'/g" |
|
|
tr '\t' '\n' |
|
|
sed -e '/^$/d;' \
|
|
-e 's/\ \ */\ /g;' \
|
|
-e 's/\\\:/\\\u/g'
|