From b21fe87fb117aeb095d52ee985a5b46532094ac2 Mon Sep 17 00:00:00 2001 From: Eric Lay Date: Sun, 23 Apr 2023 19:54:32 -0500 Subject: [PATCH] update parsing --- PKGBUILD | 2 +- pokeParse | 51 ++++++++++++++++++++++++++++++++++ pokeUpdate | 79 ----------------------------------------------------- pokedex-dev | 4 +-- 4 files changed, 54 insertions(+), 82 deletions(-) create mode 100755 pokeParse delete mode 100755 pokeUpdate diff --git a/PKGBUILD b/PKGBUILD index 28bca23..d8771b2 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -25,6 +25,6 @@ package() { cd "$srcdir/${pkgname:0:-4}" install -Dm666 pokeData/* -t "$pkgdir/usr/share/$pkgname/pokeData" install -Dm666 keybindings-preview -t "$pkgdir/usr/share/$pkgname" - install -Dm755 pokeUpdate -t "$pkgdir/usr/bin" + install -Dm755 pokeParse -t "$pkgdir/usr/bin" install -Dm755 pokedex-dev -t "$pkgdir/usr/bin" } diff --git a/pokeParse b/pokeParse new file mode 100755 index 0000000..f380420 --- /dev/null +++ b/pokeParse @@ -0,0 +1,51 @@ +#!/bin/bash + +### Assign first arg as pokemon +POKEMON="$1" + +### Set temp files to hold raw data +DATADIR="/usr/share/fuzzy-pokedex-dev/pokeData" + +### GET various pokeAPI endpoint URLs +endpoint_POKEMON="$(curl -s "https://pokeapi.co/api/v2/pokemon/$POKEMON")" +endpoint_SPECIES="$(curl -s "https://pokeapi.co/api/v2/pokemon-species/$POKEMON")" +endpoint_ENCOUNTER="$(curl -s "https://pokeapi.co/api/v2/pokemon/$POKEMON/encounters")" +endpoint_CHAIN="$(curl -s "$(jq -r '.evolution_chain["url"]' <<< "$endpoint_SPECIES")")" + +### Parse JSON for attributes and assign to vars +IFS=$'\t' read -r NAME HEIGHT WEIGHT ID BASEX < <( jq -r '[.name, .height, .weight, .id, .base_experience] | @tsv' <<< "$endpoint_POKEMON") +IFS=$'\t' read -r BASEHAPPY CAPTURE < <( jq -r '[.base_happiness, .capture_rate] | @tsv' <<< "$endpoint_SPECIES") +LOCATE=$(jq -r '.[].location_area["name"]' <<< "$endpoint_ENCOUNTER") +GENUS=$(jq -r '.genera[7].genus' <<< "$endpoint_SPECIES") +mapfile -t TYPE < <(jq -r '.types[].type["name"]' <<< "$endpoint_POKEMON") +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") +FLAVOR="$(tr -d '\"' < <(sed -e 's/\\./ /g' <<< ${FLAVOR_ARRAY[0]} | sed 's/\./& /g' | sed 's/.\{36\}\s/&\n/g'))" +CHAIN1=$(jq -r '.chain.species["name"]' <<< "$endpoint_CHAIN") +CHAIN2=$(jq -r '.chain.evolves_to[].species["name"]' <<< "$endpoint_CHAIN") +CHAIN3=$(jq -r '.chain.evolves_to[].evolves_to[].species["name"]' <<< "$endpoint_CHAIN") + +### Print formatted data +{ printf "\n%s\t A %s type Pokémon\n" \ + "${NAME^}" "${TYPE[0]}"; +printf "\n%20s\n\n" "$FLAVOR"; +if [[ -n "$CHAIN3" ]]; + then + printf "\t%s\n%s %s %s %s %s\n\n" \ + "Evolution Chain" "${CHAIN1^}" "►" "${CHAIN2^}" "►" "${CHAIN3^}"; + elif [[ -z "$CHAIN2" ]]; + then + printf " %s\n\n" "Single Stage Evolution Pokémon"; + else + printf "\t%s\n %s %s %s\n\n" \ + "Evolution Chain" "${CHAIN1^}" "►" "${CHAIN2^}"; +fi; +printf " ↞--------| STATS |--------↠ \n"; +for ((i=0; i<${#STAT[@]}; i++)); do + printf "%15s: %10s\n" "${STAT[i]}" "${VAL[i]}"; +done; +printf "\nPokedex ID: %s\tCapture Rate: %s\nBase Exp: %s\tHappiness: %s\nHeight: %s\tWeight: %s\n" \ + "$ID" "$CAPTURE" "$BASEX" "$BASEHAPPY" "$HEIGHT" "$WEIGHT"; +printf "\nGenus: %s" "${GENUS^}"; +printf "\nLocation Areas:\n%s\n" "$LOCATE";} > "$DATADIR/$POKEMON" diff --git a/pokeUpdate b/pokeUpdate deleted file mode 100755 index e3ea473..0000000 --- a/pokeUpdate +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/bash - -### Set up working dirs -DATADIR="/usr/share/fuzzy-pokedex-dev/pokeData" -TMPDIR="$(mktemp -p /tmp -d pokeTemp.XXXXXX)" - -### Declare some intergers for calculating percentage of task -declare -i WC -WC=$(pokemon-colorscripts -l | wc | awk '{print $1}' ) -declare -i COUNT -COUNT="$WC" - -### Parse PokeAPI json results and format data -pokeParser() { - ### Assign first arg as pokemon - POKEMON="$1" - - ### Set temp files to hold raw data - e_P="$(mktemp -p $TMPDIR -t e_P.$POKEMON.XXXXXX)" - e_E="$(mktemp -p $TMPDIR -t e_E.$POKEMON.XXXXXX)" - e_S="$(mktemp -p $TMPDIR -t e_S.$POKEMON.XXXXXX)" - #e_C="/tmp/poke.e_C" - - ### Set various pokeAPI endpoint URLs - endpoint_POKEMON="https://pokeapi.co/api/v2/pokemon" - endpoint_SPECIES="https://pokeapi.co/api/v2/pokemon-species" - endpoint_ENCOUNTER="https://pokeapi.co/api/v2/pokemon/$POKEMON/encounters" - #endpoint_CHAIN="https://pokeapi.co/api/v2/evolution-chain" - - ### GET from pokeAPI endpoints - curl -s "$endpoint_POKEMON/$POKEMON" -o "$e_P" - curl -s "$endpoint_ENCOUNTER" -o "$e_E" - curl -s "$endpoint_SPECIES/$POKEMON" -o "$e_S" - #curl -s "$endpoint_CHAIN/$ID" -o "$e_C" - - ### Parse JSON for attributes - NAME=$(jq -r '. | .name' < "$e_P") - HEIGHT=$(jq -r '.height' < "$e_P") - WEIGHT=$(jq -r '.weight' < "$e_P") - ID=$(jq -r '.id' < "$e_P") - BASEX=$(jq -r '.base_experience' < "$e_P") - BASEHAPPY=$(jq -r '.base_happiness' < "$e_S") - CAPTURE=$(jq -r '.capture_rate' < "$e_S") - LOCATE=$(jq -r '.[].location_area["name"]' < "$e_E") - FLAVOR=$(tr -d '\f' < <(jq -r '.flavor_text_entries[7].flavor_text' < "$e_S")) - GENUS=$(jq -r '.genera[7].genus' < "$e_S") - #mapfile -t CHAIN < <(jq -r '. | .["chain"].evolves_to[].species["name"]' < "$e_C") - mapfile -t TYPE < <(jq -r '. | .types[].type["name"]' < "$e_P") - mapfile -t STAT < <(jq -r '. | .stats[].stat["name"]' < "$e_P") - mapfile -t VAL < <(jq -r '. | .stats[].base_stat' < "$e_P") - - ### Print formatted data - { printf "%s\tA %s type Pokémon\n" \ - "$NAME" "${TYPE[0]}"; - printf "\n%20s\n\n" "$FLAVOR"; - #for ((i=0; i<${#CHAIN[@]}; i++)); do - # printf "%s\t" "${CHAIN[@]}" - #done - printf " ↞--------| STATS |--------↠ \n"; - for ((i=0; i<${#STAT[@]}; i++)); do - printf "%15s: %10s\n" "${STAT[i]}" "${VAL[i]}"; - done; - printf "\nPokedex ID: %s\tCapture Rate: %s\nBase Exp: %s\tHappiness: %s\nHeight: %s\tWeight: %s\n" \ - "$ID" "$CAPTURE" "$BASEX" "$BASEHAPPY" "$HEIGHT" "$WEIGHT"; - printf "\nGenus: %s" "$GENUS"; - printf "\nLocation Areas:\n%s\n" "$LOCATE";} > "$DATADIR/$POKEMON" - - ### Print completion status updates - ((COUNT--)) - PCTLEFT="$((COUNT*100/WC))" - PCTDONE="$((100-PCTLEFT))" - printf "\t%s - %s \t%s%%\n" "$COUNT" "$POKEMON" "$PCTDONE" -} - -for POKEMON in $(pokemon-colorscripts -l); do - pokeParser "$POKEMON" -done - -rm -rf "$TMPDIR" diff --git a/pokedex-dev b/pokedex-dev index 3a787b8..fa83bbc 100755 --- a/pokedex-dev +++ b/pokedex-dev @@ -62,7 +62,7 @@ quick_view() { ### Update Pokemon stats file set update_pokeData() { printf "\n\t\t%s\n\t%s\n\n" "WARNING!!!" "This WILL take a long time" - pokeUpdate + pokemon-colorscripts -l | parallel --bar -j 200% pokeParse } if ! (( $# )); then @@ -78,7 +78,7 @@ while (( $# )); do shift ;; -u|--update) - update_pokeData "$1" + update_pokeData shift ;; -h|--help)