Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement telnet server
  • Loading branch information
Mark Sargento committed Apr 6, 2020
commit da18f4e059c1ba658952984c8b1e76308369da69
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
"ansi-styles": "^4.2.1",
"asciichart": "^1.5.15",
"axios": "^0.19.2",
"blessed": "^0.1.81",
"clear": "^0.1.0",
"cli-table3": "^0.5.1",
"express": "^4.17.1",
"ora": "^4.0.3",
"telnet2": "^0.0.1",
"yargs": "^15.3.1"
},
"devDependencies": {
Expand Down
193 changes: 193 additions & 0 deletions telnet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
const blessed = require('blessed');
const telnet = require('telnet2');
const axios = require('axios');
const covid19 = require('./lib/cli');
const pkg = require('./package.json'); // package.json info
const apiBaseURL = "https://corona.lmao.ninja"; // NovelCOVID API
const port = process.env.TELNET_PORT || 2300; // set port
const interval = process.env.INTERVAL || 2000

async function fetchGlobalTracker() {
const { data } = await axios.get(`${apiBaseURL}/all`);

return covid19.covid19globaltracker(
data.cases, data.deaths,
data.recovered, data.updated
);
}

async function fetchByCountry(query) {
// empty, country, history, chartType
const [_x, country, history, chartType] = query.split('/'),
countryData = await axios.get(`${apiBaseURL}/countries/${country}`),
all = await axios.get(`${apiBaseURL}/all`),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can omit this call now since we only call this to get the "updated" attribute.
The country API has updated attribute available now.

so instead of u.updated use d.updated on line 37 same with line 45

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi I already removed the extra API call on country tracker. Can you please verify if this is correct. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ianvizarra please verify this and close if necessary so we can move forward. Will improve this later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outdated nato...

u = all.data,
d = countryData.data;

if (_x.length > 0) throw "Invalid query format. Must Start with \/"

if (history) {
const history = await axios.get(`${apiBaseURL}/v2/historical/${d.country}`),
h = history.data;

return covid19.historyCountryTracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
u.updated, h, chartType || 'cases'
)
}

return covid19.covid19countrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
u.updated
);
}

function renderTracker(box, screen, query = '/all') {
const footer = '\n\n Press [ENTER] to return the main menu'
box.setContent(`\n\n\n\n\nLoading....`)
const renderTrackerContent = async () => {
if (query === '/all') {
const data = await fetchGlobalTracker();
box.setContent(data + footer);
} else {
const countryData = await fetchByCountry(query);
box.setContent(countryData + footer);
}
screen.render();
}

renderTrackerContent()

return setInterval(() => renderTrackerContent(), interval)
}

telnet({ tty: true }, client => {
let executedInterval = null;
const initialContent = `What do you want to see?
\r Global Tracking: [/all]
\r Country: [/{countryname} or /{ISO 3166-1 code}]
\r Country with history: [/{ISO 3166-1}/history/{cases or deaths} For e.g. /ph/history or /ph/history/cases or /ph/history/deaths]


\r Please enter your answer:
`

client.on('term', terminal => {
screen.terminal = terminal;
screen.render()
});

client.on('size', (width, height) => {
client.columns = width;
client.rows = height;
client.emit('resize');
});

const screen = blessed.screen({
smartCSR: true,
input: client,
output: client,
terminal: 'xterm-256color',
fullUnicode: true
});
screen.key('q', function() {
process.exit(0);
});

client.on('close', () => {
if (!screen.destroyed) {
screen.destroy();
}
});

screen.key(['C-c', 'q'], (ch, key) => {
screen.destroy();
});

const box = blessed.box({
parent: screen,
left: 'center',
top: 'center',
width: '80%',
height: '90%',
content: `
Welcome to COVID-19 Tracker CLI v${pkg.version} by Waren Gonzaga\n
Please visit: https://warengonza.ga/covid19-tracker-cli
`
})

const form = blessed.form({
parent: box,
left: 0,
top: 'center',
height: 20,
width: '100%',
content: '\r\r\nPress [ENTER] to continue'

});

form.on('submit', function(data) {
form.setContent('Submitted.');
screen.render();
});

form.on('reset', function(data) {
form.setContent('Canceled.');
screen.render();
});

const input = blessed.textbox({
parent: form,
name: 'input',
input: true,
keys: true,
top: 10,
left: 'center',
height: 20,
width: '50%',
});

input.focus();

input.key(['enter'], async () => {
try {
switch(input.value) {
case '':
case null:
case undefined:
console.log(executedInterval)
clearInterval(executedInterval);
form.hidden && form.show();
box.setContent('')
form.setContent(initialContent);
break;
case '/all':
executedInterval = renderTracker(box, screen);
form.hide();
break;
default:
executedInterval = renderTracker(box, screen, input.value);
form.hide();
break;
}
} catch(err) {
console.log(err);
form.setContent(`${initialContent}\n\n An Error Occured Please Press Enter Again`);
} finally {
input.clearValue()
screen.render();
}
})

client.on('data', () => {
console.log(input.value);
})

screen.render();
}).listen(2300);

console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`)