fuzzy-pokedex/pokeParse

129 lines
4.6 KiB
Plaintext
Raw Normal View History

#!/bin/bash
### Assign first arg as pokemon
POKEMON="$1"
### Set temp files to hold raw data
2023-04-29 21:52:56 -05:00
DATADIR="/usr/bin/fuzzy-pokedex/pokeData"
### GET various pokeAPI endpoint URLs
endpoint_SPECIES="$(curl -s "https://pokeapi.co/api/v2/pokemon-species/$POKEMON")"
2023-04-29 21:52:56 -05:00
ID="$(jq -r '.order' <<< "$endpoint_SPECIES")"
endpoint_POKEMON="$(curl -s "https://pokeapi.co/api/v2/pokemon/$POKEMON")"
endpoint_ENCOUNTER="$(curl -s "https://pokeapi.co/api/v2/pokemon/$POKEMON/encounters")"
endpoint_CHAIN="$(curl -s "$(jq -r '.evolution_chain["url"]' <<< "$endpoint_SPECIES")")"
2023-04-29 21:52:56 -05:00
### Test for alt_endpoint
if NAME="$(jq -e -r '[.name]' <<< "$endpoint_POKEMON")" && [[ -n "$NAME" ]]
then
:
else
endpoint_POKEMON="$(curl -s "https://pokeapi.co/api/v2/pokemon/$ID")"
fi
### Parse JSON for attributes and assign to vars
2023-04-29 21:52:56 -05:00
IFS=$'\t' read -r NAME HEIGHT WEIGHT BASEX < <( jq -e -r '[.name, .height, .weight, .base_experience] | @tsv' <<< "$endpoint_POKEMON")
2023-04-28 08:44:48 -05:00
IFS=$'\t' read -r BASEHAPPY CAPTURE GENUS < <( jq -r '[.base_happiness, .capture_rate, .genera[7].genus] | @tsv' <<< "$endpoint_SPECIES")
mapfile -t TYPE < <(jq -r '.types[].type["name"]' <<< "$endpoint_POKEMON"); TYPE_ITEM="${TYPE[0]}"
mapfile -t STAT < <(jq -r '.stats[].stat["name"]' <<< "$endpoint_POKEMON")
mapfile -t VAL < <(jq -r '.stats[].base_stat' <<< "$endpoint_POKEMON")
mapfile -t FLAVOR_ARRAY < <( jq '.flavor_text_entries[] | select(.language["name"] == "en") | .flavor_text' <<< "$endpoint_SPECIES")
2023-04-28 08:44:48 -05:00
FLAVOR="$(tr -d '\"' < <(sed -e 's/\\./ /g' -e 's/\./& /g' -e 's/.\{28\}\s/&\n /g' -e "s/this POKéMON/${NAME^}/g" \
-e "s/This POKéMON/${NAME^}/g" -e 's/POKéMON/Pokémon/g' <<< "${FLAVOR_ARRAY[0]}"))"
mapfile -t CHAIN1 < <(jq -r '.chain.species["name"]' <<< "$endpoint_CHAIN")
mapfile -t CHAIN2 < <(jq -r '.chain.evolves_to[].species.name' <<< "$endpoint_CHAIN")
mapfile -t CHAIN3 < <(jq -r '.chain.evolves_to[].evolves_to[].species.name' <<< "$endpoint_CHAIN")
2023-04-29 11:51:18 -05:00
mapfile -t ABILITIES < <(jq -r '.abilities[].ability | [.name, .url] | @tsv' <<< "$endpoint_POKEMON")
2023-04-28 08:44:48 -05:00
mapfile -t LOCATE < <(jq -r '.[].location_area["name"]' <<< "$endpoint_ENCOUNTER")
### Print formatted data
2023-04-29 22:24:52 -05:00
## - a lot if/then to test for empty vars not found in API data
## - would like to catch this in jq step and avert any on screen errors
2023-04-28 08:44:48 -05:00
{ printf "\n %s %s Type Pokémon\n" \
"${NAME^}" "${TYPE_ITEM^}";
printf "\n %s\n" "$FLAVOR";
if [[ -n "${CHAIN3[0]}" ]] ;
then
2023-04-28 08:44:48 -05:00
printf "\t %s\n %s %s %s %s %s\n\n" \
"Evolution Chain" "${CHAIN1^}" "►" "${CHAIN2^}" "►" "${CHAIN3^}";
2023-04-28 08:44:48 -05:00
elif [[ -z "${CHAIN2[0]}" ]] ;
then
2023-04-28 08:44:48 -05:00
printf " %s\n\n" "Single Stage Evolution Pokémon";
else
printf "\t%s\n %s %s %s\n\n" \
"Evolution Chain" "${CHAIN1^}" "►" "${CHAIN2^}";
fi;
2023-04-28 08:44:48 -05:00
if [[ ${#CHAIN1[@]} -gt 1 ]] ;
then
printf "%s:\n" "Stage 1 Forms";
for ((i=0; i<${#CHAIN1[@]}; i++)); do
printf " %s\n" "${CHAIN1[i]}";
done;
printf "\n";
fi;
if [[ ${#CHAIN2[@]} -gt 1 ]] ;
then
2023-04-29 11:56:20 -05:00
printf " %s:\n" "Stage 2 Forms";
2023-04-28 08:44:48 -05:00
for ((i=0; i<${#CHAIN2[@]}; i++)); do
printf " %s\n" "${CHAIN2[i]}";
done;
printf "\n";
fi;
if [[ ${#CHAIN3[@]} -gt 1 ]] ;
then
2023-04-29 11:56:20 -05:00
printf " %s:\n" "Stage 3 Forms";
2023-04-28 08:44:48 -05:00
for ((i=0; i<${#CHAIN3[@]}; i++)); do
printf " %s\n" "${CHAIN3[i]}";
done;
printf "\n";
fi;
printf " ↞--------| STATS |--------↠ \n";
for ((i=0; i<${#STAT[@]}; i++)); do
2023-04-28 08:44:48 -05:00
printf " %15s: %10s\n" "${STAT[i]}" "${VAL[i]}";
done;
2023-04-29 22:24:52 -05:00
if [[ -n "$GENUS" ]]
then
printf "\n Genus: %s" "${GENUS^}";
else
:
fi;
if [[ -n "$BASEX" ]] && [[ -n "$CAPTURE" ]]
then
printf "\n Happiness: %s\tPokedex ID: %s\n Base Exp: %s\tCapture Rate: %s\n Height: %s\tWeight: %s\n" \
"$BASEHAPPY" "$ID" "$BASEX" "$CAPTURE" "$HEIGHT" "$WEIGHT";
else
printf "\n Happiness: %s\tPokedex ID: %s\n Height: %s\tWeight: %s\n" \
"$BASEHAPPY" "$ID" "$HEIGHT" "$WEIGHT";
fi
2023-04-29 11:51:18 -05:00
printf "\n Types:\n"
for ((i=0; i<${#TYPE[@]}; i++)); do
TYPE_LIST_ITEM="${TYPE[i]}"
printf " %s\n" "${TYPE_LIST_ITEM^}"
2023-04-29 21:52:56 -05:00
done;
2023-04-29 22:24:52 -05:00
if [[ -n "$ABILITIES" ]]
then
printf "\n%s\n\n" " ↞---------------|ABILITIES|---------------↠"
for ((i=0; i<${#ABILITIES[@]}; i++)); do
ABILITY_NAME="$(awk '{print $1}' <<< "${ABILITIES[i]}")"
EFFECT_ENTRY="$(sed -e 's/\s\s/ /g' -e 's/^\w/ &/g' -e 's/.\{28\}\s/&\n /g' \
< <(jq -r '.effect_entries[] | select(.language["name"] == "en") | .effect' \
< <(curl -s "$(awk '{print $2}' <<< "${ABILITIES[i]}")")))"
printf " %s:\n\n" "${ABILITY_NAME^}"
printf " %s\n\n" "$EFFECT_ENTRY"
done;
else
:
fi
2023-04-29 21:52:56 -05:00
if [[ -n "$LOCATE" ]]
then
printf "\n Location Areas:\n";
for ((i=0; i<${#LOCATE[@]}; i++)); do
LOCATION_ITEM="$(sed 's/-/ /g' <<< "${LOCATE[i]}")";
printf " %s\n" "${LOCATION_ITEM^}";
done;
else
printf "\n Location Areas Unknown\n";
fi; } > "$DATADIR/$POKEMON"
2023-04-28 08:44:48 -05:00