How to build a ZenQuotes API-powered ASCII art generator using terminal commands

In this coding tutorial, we will be exploring how to use the command-line tool curl along with other utilities to perform a series of operations that involve making a GET request to an API, processing the response data, and displaying it using ASCII art.

Specifically, we will be using the ZenQuotes API to retrieve a random quote, and then using jq to format the response data, cowsay to display the quote as ASCII art, and lolcat to add some color to the output. By the end of this tutorial, you will have learned how to combine several command-line tools to create a fun and useful script that retrieves and displays a random quote.

Full Code

curl -s -X GET -H "Content-Type: application/json" https://zenquotes.io/api/random | jq -r '" " as $space | .[0] | .q + "\n\n\($space * ((.q |length)-(.a |length) - 2))--" + .a' | cowsay -nsf $(cowsay -l | tail -n +2 | tr ' ' '\n' | shuf -n1) | lolcat

Code Breakdown

This command performs a series of operations that involve making a GET request to an API, processing the response data, and displaying it using ASCII art. Here is a breakdown of the command:

curl -s -X GET -H "Content-Type: application/json" https://zenquotes.io/api/random 

makes a GET request to the ZenQuotes API to retrieve a random quote in JSON format.

| jq -r '" " as $space | .[0] | .q + "\n\n\($space * ((.q |length)-(.a |length) - 2))--" + .a' 

pipes the JSON response data to jq, a command-line JSON processor. The jq expression formats the quote and author information into a string, with the quote and author separated by a double dash “–” and the quote text aligned with the author name.

| cowsay -nsf $(cowsay -l | tail -n +2 | tr ' ' '\n' | shuf -n1) 

pipes the formatted quote string to cowsay, a program that generates ASCII art of a cow saying the input text. The options -nsf tell cowsay to display the cow without a speech bubble or thought bubble, and to use the default cow shape.

| lolcat 

pipes the output of cowsay to lolcat, a program that adds rainbow coloring to the text output.

So, the overall effect of this command is to retrieve a random quote from the ZenQuotes API, format it in a visually appealing way, and display it with a colorful ASCII art cow.

Cowsay Installation

To install cowsay on your system, you can follow these steps:

On Debian-based Linux distributions, such as Ubuntu or Debian, you can use the following command to install cowsay:

sudo apt-get install cowsay

On Red Hat-based Linux distributions, such as Fedora or CentOS, you can use the following command to install cowsay:

sudo dnf install cowsay

On macOS, you can use Homebrew to install cowsay. First, install Homebrew by running the following command in a terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, install cowsay with the following command:

brew install cowsay

On Windows, you can install cowsay using a package manager like Chocolatey. First, install Chocolatey by following the instructions on their website: https://chocolatey.org/install

Then, open a command prompt or PowerShell window and run the following command:

choco install cowsay

Once installed, you can use cowsay by running the command with a message, for example:

cowsay "Hello, world!"

This will display a cow with a speech bubble saying “Hello, world!” You can find more options and customizations in the cowsay man page by typing man cowsay in your terminal.

Lolcat Installation

To install lolcat on your system, you can follow these steps:

On Debian-based Linux distributions, such as Ubuntu or Debian, you can use the following command to install lolcat:

sudo apt-get install lolcat

On Red Hat-based Linux distributions, such as Fedora or CentOS, you can use the following command to install lolcat:

sudo dnf install rubygem-lolcat

On macOS, you can use Homebrew to install lolcat. First, install Homebrew by running the following command in a terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, install lolcat with the following command:

brew install lolcat

On Windows, you can install lolcat using a package manager like Chocolatey. First, install Chocolatey by following the instructions on their website: https://chocolatey.org/install

Then, open a command prompt or PowerShell window and run the following command:

choco install lolcat

Once installed, you can use lolcat by piping the output of a command to it, for example:

echo "Hello, world!" | lolcat

This will display the message “Hello, world!” in rainbow colors. You can find more options and customizations in the lolcat documentation by typing man lolcat or lolcat –help in your terminal.

Jq Installation

To install jq on your system, you can follow these steps:

On Debian-based Linux distributions, such as Ubuntu or Debian, you can use the following command to install jq:

sudo apt-get install jq

On Red Hat-based Linux distributions, such as Fedora or CentOS, you can use the following command to install jq:

sudo dnf install jq

On macOS, you can use Homebrew to install jq. First, install Homebrew by running the following command in a terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, install jq with the following command:

brew install jq

On Windows, you can install jq using a package manager like Chocolatey. First, install Chocolatey by following the instructions on their website: https://chocolatey.org/install

Then, open a command prompt or PowerShell window and run the following command:

choco install jq

Once installed, you can use jq by piping JSON data to it and using its command-line options to process and filter the data. You can find more information and examples in the jq documentation by typing man jq or jq –help in your terminal.

Curl Installation

In most cases, curl is likely already installed on your system. You can check by running the following command in a terminal:

curl --version

If curl is not installed, you can follow these steps to install it:

On Debian-based Linux distributions, such as Ubuntu or Debian, you can use the following command to install curl:

sudo apt-get install curl

On Red Hat-based Linux distributions, such as Fedora or CentOS, you can use the following command to install curl:

sudo dnf install curl

On macOS, curl is pre-installed, so you don’t need to install it.

On Windows, you can download the latest version of curl from the official website: https://curl.se/windows/

Once downloaded, you can extract the files to a directory of your choice and add the directory to your system’s PATH environment variable to be able to use curl from the command line.

After installing curl, you can use it to transfer data from or to a server, by running the command with appropriate parameters, for example:

curl https://www.example.com

This will download the contents of the example.com website and display them in your terminal. You can find more options and customizations in the curl documentation by typing man curl or curl –help in your terminal.

H/T Random ZenQuotes Generator by u/dusktreader on Reddit

Subscribe