DEV Community

Cover image for Creating a Linux Terminal Pokedex
Paula
Paula

Posted on

Creating a Linux Terminal Pokedex

I recently found an API for Pokemon and I wrote a simple Pokedex for fun using the data available.

The api is pretty straightforward, you give it the ID or name of a Pokemon, it give you back information in a json file. I used bash and jq to parse the information.

curl -s https://pokeapi.co/api/v2/pokemon/<my pokemon>
Enter fullscreen mode Exit fullscreen mode

So, imagine we want to get the type of the Pokemon. Taking a quick look at the json, I check were the type is and then write:

strings $mipoke.json | jq -r '.types[].type.name'
Enter fullscreen mode Exit fullscreen mode

While $mypoke is the Pokemon name. SOme of them have two types so I save them in variables and check the existence of the second one:

first_type=$(strings $mipoke.json | jq -r '.types[0].type.name')
second_type=$(strings $mipoke.json | jq -r '.types[1].type.name')


if  [ -z "$second_type" ]; then
      echo -e "$second_type"
fi
Enter fullscreen mode Exit fullscreen mode

I though it would be fun to set colors according to the type, so created a palette in the header:

export COLOR_NC='\e[0m' # No Color

export COLOR_BLACK='\e[0;30m'
export COLOR_GRAY='\e[1;30m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_LIGHT_GRAY='\e[0;37m'
export COLOR_WHITE='\e[1;37m'
Enter fullscreen mode Exit fullscreen mode

And then I created a basic conditional to set the color for the types:

# initializing color variables
color1="${COLOR_WHITE}"
color2="${COLOR_WHITE}"
...
#creating a function so I don't 
#commit redundancy in both types
check_colors() {
tipo=$1
color="${COLOR_WHITE}"

if [[ "$tipo" = "fairy" || "$tipo" = "psychic" ]]; then
        color="${COLOR_LIGHT_PURPLE}"
elif [[ "$tipo" = "steel" || "$tipo" = "normal" ]]; then
        color="${COLOR_LIGHT_GRAY}"
elif [[ "$tipo" = "grass" || "$tipo" = "bug" ]]; then
        color="${COLOR_GREEN}"      
elif [[ "$tipo" = "water" || "$tipo" = "ice" ]]; then
        color="${COLOR_BLUE}"
elif [ "$tipo" = "poison" ]; then                           
        color="${COLOR_PURPLE}"
elif [ "$tipo" = "electric" ]; then
        color="${COLOR_YELLOW}"

fi

echo "$color"
}
...
#setting the color for the first type
color1="$(check_colors $first_type)"

#if second type exists, setting the color, too
if  [ -z "$second_type" ]; then
      color2="$(check_colors $second_type)"
      echo -e "$color2 $second_type ${COLOR_NC}"
fi

Enter fullscreen mode Exit fullscreen mode

Now the next two interesting things might be moves and abilities.I realized in a quick test that moves are too many, so I opted for showing a small sample and saving all the rest in a different file:

strings $mipoke.json | jq -r '.moves[].move.name' | head -n5
echo "...\nCheck the whole list in the file $mipoke.moves"
strings $mipoke.json | jq -r '.moves[].move.name' >> $mipoke.moves
Enter fullscreen mode Exit fullscreen mode

Abilities aren't usually too many, so I kept them simple:

strings $mipoke.json | jq -r '.abilities[].ability.name'
Enter fullscreen mode Exit fullscreen mode

I decided I wanted to do a charming detail so I captured the weight:

weight=$(strings $mipoke.json | jq -r '.weight')
Enter fullscreen mode Exit fullscreen mode

Converted it to kg (it's in lbs):

inkg=$(bc <<< "scale=2; $weight*0.45")
Enter fullscreen mode Exit fullscreen mode

And then use it in a conditional so to give advice on how to handle the Pokemon:

inkg_int=$(bc <<< "$inkg/1")
inkg_int=$(($inkg_int))

if [[ "$inkg_int" -lt 35 && "$inkg_int" -gt 10 ]]; then
        consejo="That means you can carry it for a while, but you can hurt you back! let them walk"
elif [[ "$inkg_int" -lt 11  && "$inkg_int" -gt 5 ]]; then
        consejo="That means is your pokemon is quite light and you might be able to carry it around a lot."
elif [ "$inkg_int" -lt 6 ]; then
        consejo="That means your pokemon is very light, tiny cute baby, and you might even be able to carry it around in a bag, if it pleases them."
elif [[ "$inkg_int" -lt 60  && "$inkg_int" -gt 34 ]]; then
        consejo="Your pokemon weights like a human teen or an adult even, so try to look for ways of taking care of them without carrying it to much unless you want to hurt your back!"
elif [[ "$inkg_int" -lt 200  && "$inkg_int" -gt 59 ]]; then
        consejo="Your pokemon might be a little bit too heavy for carrying it, so don't attempt to do it. Nevertheless look for alternatives such as patting and saying nice words."
else
        consejo="Your pokemon is a thicc baby, they won't expect you to carry it. But they will probably like pats and nice words."
fi

echo "$consejo"
Enter fullscreen mode Exit fullscreen mode

It was pretty much done. I added some visual details (further colors, sparkles) and the possibility of tooting in your mastodon the final information. For that I duplicated every print into a file and then:

read -p"Do you want to toot it? y/n: " ANS

if [[ "$ANS" = "y" || "$ANS" = "Y" ]]; then
        toot post "$(strings mytoot)"
else
        echo "okay, not tooting!"
fi

Enter fullscreen mode Exit fullscreen mode

And that's it!

capture of the pokedex
capture of the pokedex

Here's the repo.

Top comments (1)

Collapse
 
cosmicwanderer7 profile image
Prithvi Yewale

Its Awesome šŸ¤©