Simple GUI on Linux Using Zenity and Node.js
Tags: node, linux, gui, cronjob
Published: 2021-05-27 23:42:59

It is incredible the amount of "secret" features on Linux that are not well known until today.
I came across a tool called Zenity the other day. It is related to sending UI notifications on Linux.
Zenity is an open-source tool for displaying simple GUI in shell scripts. It makes scripts more user-friendly by displaying GTK+ dialogs. It is tipically used on shell scripting to improve its usability.

My goal is to do the following:
1. Use Node.js to access financial data related to Brazilian Treasury Bonds (Tesouro Direto in Brazil);
2. Create a mechanism to sistematically check instant treasury rates;
3. Use Zenity to pop-up a notification to user;

### Acessing Tesouro Direto Data
Tesouro Direto Rates can be found [here](https://www.tesourodireto.com.br/titulos/precos-e-taxas.htm). In addition, if you press F12 and check Network Tab, you'll see that the following json is received by the browser:  https://www.tesourodireto.com.br/json/br/com/b3/tesourodireto/service/api/treasurybondsinfo.json 
We can see that it contains all the current treasury rates up to date.
So, there's not much to do other than to use Node.js to make GET commands and capture that json file as shown bellow:
```
const axios = require('axios');
const { exec } = require("child_process");
const https = require('https');



const instance = axios.create({
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  })
});

instance.get('https://www.tesourodireto.com.br/json/br/com/b3/tesourodireto/service/api/treasurybondsinfo.json')
.then(x => {...

```
Note: That `rejectUnauthorized: false` is important to be set, otherwise you will get a certificate error.

### Task Scheduler
A cron job is a Linux command used for scheduling tasks to be executed sometime in the future. This feature is exactly what is needed to check the desired rates every hour. So, we will use a cron job to call our Node script to get the treasury bond rates every hour.
To create your Cron Job go to:
`/var/spool/cron`
And access user's cron tab by typing:
`crontab -e`
The README presents orientation about how to schedule commands using crontab by adding some expressionfollowed by the command that needs to be executed.
For instance, if you want your program to run every minute from 10AM to 17PM every weekday, write the following:
`* 10-17 * * 1-5 <command>`
In order to ease this task, there is a site called [crontab.guru](https://crontab.guru/) that helps us to construct the expression. 
Note: To schedule cron to run your script EVERY HOUR of EVERY WEEKDAY from 10AM to 17PM, the expression is: 0 10-17 * * 1-5 <command>
* Remember to add the full path of Node and script to make the cron tab job to work properly.

## Notifications Using Zenity
Zenity is a tool that enables the generation of dialogs from terminal commands. [Notifications](https://help.gnome.org/users/zenity/stable/notification.html.en) look like this:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckjsjqgap60xf32vhglv.png)
 You can generate them by typing on Terminal:
`zenity --notification\
    --window-icon="info" \
    --text="There are system updates necessary!"`

There's a good chance that Zenity is alredy installed on your machine (In Ubuntu it is). If you do not have it, please type:
`sudo apt-get update -y`
next, type:
`sudo apt-get install -y zenity`
To conclude our goals we need a way to make Node run shell commands and process their I/O. This is achieved by using exec library as shown bellow:
```
  exec('zenity --notification --window-icon="info" --text="Taxa Atual TD 2035: IPCA+"' + taxa, (error, stdout, stderr) => {
    if (error) {
      console.log(`error: ${error.message}`);
      return;
    }
    if (stderr) {
      console.log(`stderr: ${stderr}`);
      return;
    }
  });
```
The entire Github project is available [here](https://github.com/ferfox1981/td-monitor).

Fonts:
https://help.gnome.org/users/zenity/stable/index.html.en
https://www.howtoforge.com/how-to-display-gui-dialogs-in-bash-script-using-zenity/
https://crontab.guru/