master-wiki/.utils/igdb.sh
2023-09-02 12:11:25 -04:00

40 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
query="$*"
shift
set -e pipefile
if [[ -z "$query" ]]; then
echo "No query provided"
exit 1
fi
source "$(git rev-parse --show-toplevel)/.env"
# login
if [[ -e /tmp/igdb-login.json ]]; then
token=$(jq -r '.access_token' /tmp/igdb-login.json)
else
login_url="https://id.twitch.tv/oauth2/token?client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=client_credentials"
token=$(curl -s -X POST "$login_url" | tee /tmp/igdb-login.json | jq -r '.access_token')
fi
if [[ "$token" == "null" ]]; then
echo "Couldn't fetch token"
exit 1
fi
fields="name, parent_game.name, franchises.name, franchises.slug, genres.name, platforms.name, release_dates.human"
limit=50
preview=".[{n}] | del(.id) | walk(if type == \"array\" then map((.name // .human)?) else . end )"
# preview=".[{n}]"
curl -s 'https://api.igdb.com/v4/games' \
-H "Client-ID: $CLIENT_ID" \
-H "Authorization: Bearer $token" \
-H 'Accept: application/json' \
-d "search \"$query\"; fields $fields; where category = (0, 8, 9, 11, 13); limit $limit;" | tee /tmp/igdb-query.json |
jq -r '.[].name' |
fzf --preview "jq --sort-keys --color-output '$preview' /tmp/igdb-query.json"