-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtungsten.sh
147 lines (126 loc) · 3.56 KB
/
tungsten.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#
# Copyright 2012 Sairon Istyar
# Copyright 2013-2020 Alex Szczuczko
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Setup
#
set -euo pipefail
error () {
echo "$@" >&2
}
#
# Load stored API key
#
key_path="$HOME/.wolfram_api_key"
key_pattern='[A-Z0-9]{6}-[A-Z0-9]{10}'
if ! [ -v WOLFRAM_API_KEY ]
then
if [ -f "$key_path" ]
then
# Load the contents of $key_path file as WOLFRAM_API_KEY
WOLFRAM_API_KEY="$(<"$key_path")"
if ! [[ "$WOLFRAM_API_KEY" =~ $key_pattern ]]
then
error "WolframAlpha API key read from file '$key_path' doesn't match the validation pattern"
exit 2
fi
elif [ -e "$key_path" ]
then
error "WolframAlpha API key path '$key_path' exists, but isn't a file."
exit 3
else
WOLFRAM_API_KEY=""
fi
fi
#
# Read the query
#
query="${@:-}"
if [ -z "$query" ]
then
error "You must specify a query to make"
exit 5
fi
#
# Perform the query
#
curl_args=(
-sSf -G
--data-urlencode "output=JSON"
--data-urlencode "format=plaintext"
--data-urlencode "input=$query"
)
if [ -n "$WOLFRAM_API_KEY" ]
then
curl_args+=(
--data-urlencode "appid=$WOLFRAM_API_KEY"
"https://api.wolframalpha.com/v2/query"
)
iconv_from="UTF-8"
else
error "Querying without an API key. There may be incorrect / missing characters."
error "To query with an API key, add the key to $key_path as described in the README"
curl_args+=(
--data-urlencode "type=full"
-e "https://products.wolframalpha.com/api/explorer/"
"https://www.wolframalpha.com/input/apiExplorer.jsp"
)
iconv_from="ISO-8859-1"
fi
result="$(curl "${curl_args[@]}" | iconv -f "$iconv_from" -t UTF-8)"
#
# Process the result of the query
#
query_result () {
set -euo pipefail
jq -r "$1" <<<"$result"
}
# Handle error results
if [ "$(query_result '.queryresult.error')" != "false" ]
then
error_msg="$(query_result '.queryresult.error.msg')"
if [ "$error_msg" = "Invalid appid" ]
then
error "WolframAlpha API rejected the given API key"
error "Modify or remove the key stored in file '$key_path'"
exit 6
else
error "WolframAlpha API returned an error: $error_msg"
exit 7
fi
fi
# Only colourise if stdout is a terminal
if [ -t 1 ]
then
titleformat='\e[1;34m%s\e[m\n'
else
titleformat='%s\n'
fi
# Handle "cannot iterate over null" jq error
if [ "$(query_result '.queryresult.pods[0].subpods[0].plaintext')" == "null" ]
then
error "WolframAlpha API returned no results for the given query"
exit 8
fi
# Print title+plaintext for each pod with text in it's subpod/plaintext child
while read -r title
do
printf "$titleformat" "$title"
# Process the >0 plaintext elements decendent of the current pod
query_result ".queryresult.pods[] | select(.title == \"$title\") | .subpods[].plaintext"
done < <(query_result '.queryresult.pods[] | select(.subpods[0].plaintext != "") | .title')