aboutsummaryrefslogtreecommitdiff
path: root/games/mtg/getScryfallJSON.sh
diff options
context:
space:
mode:
Diffstat (limited to 'games/mtg/getScryfallJSON.sh')
-rwxr-xr-xgames/mtg/getScryfallJSON.sh37
1 files changed, 37 insertions, 0 deletions
diff --git a/games/mtg/getScryfallJSON.sh b/games/mtg/getScryfallJSON.sh
new file mode 100755
index 0000000..2cc354a
--- /dev/null
+++ b/games/mtg/getScryfallJSON.sh
@@ -0,0 +1,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"