aboutsummaryrefslogtreecommitdiff
path: root/games/mtg/getScryfallJSON.sh
blob: 2cc354a61727e97bd97083d7405257e84c5baa44 (plain)
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
#!/bin/sh

# getScryfallJSON.sh: Gets the JSON of a fuzzy Scryfall search.
#
# Usage: getScryfallJSON.sh CARDNAME

usageMessage="Usage: ${0} FILE..."

error() {
	printf '%s error: %s\n' "$0" "$*" >&2
}

errorAndUsage() {
	printf '%s error: %s\n%s\n' "$0" "$*" "$usageMessage" >&2
}

# Process options
while getopts ':' opt; do
	case $opt in

		'?' )
			printf '%s\n' "$usageMessage" >&2
			exit 1

	esac
done
shift $((OPTIND - 1))

url="$(echo "https://api.scryfall.com/cards/named?fuzzy=${*}" | sed 's/ /+/g')"
json="$(curl "$url" 2>/dev/null)"

if echo "$json" | jq -r .object | grep -q 'error'; then
	echo "Error: $(echo "$json" | jq)" >&2
	exit 1
fi

echo "$json"