-
-
Notifications
You must be signed in to change notification settings - Fork 43
Implement telnet server #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement telnet server
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`), | ||
| 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}!`) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
updatedattribute available now.so instead of
u.updatedused.updatedon line 37 same with line 45There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outdated nato...