diff --git a/.utils/igdb.sh b/.utils/igdb.sh index 85d0c1b..1d25ea9 100755 --- a/.utils/igdb.sh +++ b/.utils/igdb.sh @@ -10,7 +10,9 @@ if [[ -z "$query" ]]; then exit 1 fi -source "$(git rev-parse --show-toplevel)/.env" +root=$(git rev-parse --show-toplevel) + +source "$root/.env" # login if [[ -e /tmp/igdb-login.json ]]; then @@ -25,16 +27,44 @@ if [[ "$token" == "null" ]]; then exit 1 fi -fields="name, parent_game.name, franchises.name, franchises.slug, genres.name, platforms.name, release_dates.human" +# Select entry +fields="name, slug, summary, url, parent_game.name, franchises.name, franchises.slug, genres.name, platforms.name, first_release_date, involved_companies.company.name" limit=50 -preview=".[{n}] | del(.id) | walk(if type == \"array\" then map((.name // .human)?) else . end )" -# preview=".[{n}]" +preview=".[{n}] | .first_release_date = (.first_release_date // 0 | strflocaltime(\"%Y-%m-%d\")) | del(.id, .slug, .platforms, .summary) | walk(if type == \"array\" then map((.name // .company.name)?) else . end )" -curl -s 'https://api.igdb.com/v4/games' \ +line=$(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" + fzf --preview "jq --sort-keys --color-output '$preview' /tmp/igdb-query.json" --bind "ctrl-o:execute-silent(jq -r '.[{n}].url' /tmp/igdb-query.json | xargs xdg-open)" --bind 'enter:become(echo {n})') + +if [[ -z $line ]]; then + echo "No entry selected..." + exit +fi + +filter="{ title: .name, alias: .name, release: .first_release_date | strflocaltime(\"%Y-%m-%d\"), developers: (.involved_companies | map(.company.name) | unique), genres: (.genres | map(.name)), url, status: \"Backlog\", times_played: 0}" + +# Create frontmatter +slug=$(jq --compact-output -r ".[$line] | .slug" /tmp/igdb-query.json) +entry=$(jq --compact-output -r ".[$line] | $filter" /tmp/igdb-query.json) + +# TODO: figure out how to pass $entry to python +frontmatter=$(python -c 'import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))' <<<"$entry") +frontmatter="---\n$frontmatter\n---" +frontmatter="$frontmatter\n\n# $(jq -r ".[$line] | .name" /tmp/igdb-query.json)" +frontmatter="$frontmatter\n\n## Brief" +frontmatter="$frontmatter\n\n> $(jq -r ".[$line] | .summary" /tmp/igdb-query.json)" +frontmatter="$frontmatter\n\n## Thoughts" + +if [[ -e "$root/games/$slug" ]]; then + echo "File exist, here is the frontmatter to update it:" + echo + echo -e "$frontmatter" +else + echo -e "$frontmatter" >"$root/games/$slug.md" + echo "file created at: $root/games/$slug" +fi