|
| 1 | +const blessed = require('blessed'); |
| 2 | +const telnet = require('telnet2'); |
| 3 | +const axios = require('axios'); |
| 4 | +const covid19 = require('./lib/cli'); |
| 5 | +const pkg = require('./package.json'); // package.json info |
| 6 | +const apiBaseURL = "https://corona.lmao.ninja"; // NovelCOVID API |
| 7 | +const port = process.env.TELNET_PORT || 2300; // set port |
| 8 | +const interval = process.env.INTERVAL || 2000 |
| 9 | + |
| 10 | +async function fetchGlobalTracker() { |
| 11 | + const { data } = await axios.get(`${apiBaseURL}/all`); |
| 12 | + |
| 13 | + return covid19.covid19globaltracker( |
| 14 | + data.cases, data.deaths, |
| 15 | + data.recovered, data.updated |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +async function fetchByCountry(query) { |
| 20 | + // empty, country, history, chartType |
| 21 | + const [_x, country, history, chartType] = query.split('/'), |
| 22 | + countryData = await axios.get(`${apiBaseURL}/countries/${country}`), |
| 23 | + all = await axios.get(`${apiBaseURL}/all`), |
| 24 | + u = all.data, |
| 25 | + d = countryData.data; |
| 26 | + |
| 27 | + if (_x.length > 0) throw "Invalid query format. Must Start with \/" |
| 28 | + |
| 29 | + if (history) { |
| 30 | + const history = await axios.get(`${apiBaseURL}/v2/historical/${d.country}`), |
| 31 | + h = history.data; |
| 32 | + |
| 33 | + return covid19.historyCountryTracker( |
| 34 | + d.country, d.cases, d.todayCases, |
| 35 | + d.deaths, d.todayDeaths, d.recovered, |
| 36 | + d.active, d.critical, d.casesPerOneMillion, |
| 37 | + u.updated, h, chartType || 'cases' |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + return covid19.covid19countrytracker( |
| 42 | + d.country, d.cases, d.todayCases, |
| 43 | + d.deaths, d.todayDeaths, d.recovered, |
| 44 | + d.active, d.critical, d.casesPerOneMillion, |
| 45 | + u.updated |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +function renderTracker(box, screen, query = '/all') { |
| 50 | + const footer = '\n\n Press [ENTER] to return the main menu' |
| 51 | + box.setContent(`\n\n\n\n\nLoading....`) |
| 52 | + const renderTrackerContent = async () => { |
| 53 | + if (query === '/all') { |
| 54 | + const data = await fetchGlobalTracker(); |
| 55 | + box.setContent(data + footer); |
| 56 | + } else { |
| 57 | + const countryData = await fetchByCountry(query); |
| 58 | + box.setContent(countryData + footer); |
| 59 | + } |
| 60 | + screen.render(); |
| 61 | + } |
| 62 | + |
| 63 | + renderTrackerContent() |
| 64 | + |
| 65 | + return setInterval(() => renderTrackerContent(), interval) |
| 66 | +} |
| 67 | + |
| 68 | +telnet({ tty: true }, client => { |
| 69 | + let executedInterval = null; |
| 70 | + const initialContent = `What do you want to see? |
| 71 | + \r Global Tracking: [/all] |
| 72 | + \r Country: [/{countryname} or /{ISO 3166-1 code}] |
| 73 | + \r Country with history: [/{ISO 3166-1}/history/{cases or deaths} For e.g. /ph/history or /ph/history/cases or /ph/history/deaths] |
| 74 | +
|
| 75 | +
|
| 76 | + \r Please enter your answer: |
| 77 | + ` |
| 78 | + |
| 79 | + client.on('term', terminal => { |
| 80 | + screen.terminal = terminal; |
| 81 | + screen.render() |
| 82 | + }); |
| 83 | + |
| 84 | + client.on('size', (width, height) => { |
| 85 | + client.columns = width; |
| 86 | + client.rows = height; |
| 87 | + client.emit('resize'); |
| 88 | + }); |
| 89 | + |
| 90 | + const screen = blessed.screen({ |
| 91 | + smartCSR: true, |
| 92 | + input: client, |
| 93 | + output: client, |
| 94 | + terminal: 'xterm-256color', |
| 95 | + fullUnicode: true |
| 96 | + }); |
| 97 | + screen.key('q', function() { |
| 98 | + process.exit(0); |
| 99 | + }); |
| 100 | + |
| 101 | + client.on('close', () => { |
| 102 | + if (!screen.destroyed) { |
| 103 | + screen.destroy(); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + screen.key(['C-c', 'q'], (ch, key) => { |
| 108 | + screen.destroy(); |
| 109 | + }); |
| 110 | + |
| 111 | + const box = blessed.box({ |
| 112 | + parent: screen, |
| 113 | + left: 'center', |
| 114 | + top: 'center', |
| 115 | + width: '80%', |
| 116 | + height: '90%', |
| 117 | + content: ` |
| 118 | + Welcome to COVID-19 Tracker CLI v${pkg.version} by Waren Gonzaga\n |
| 119 | + Please visit: https://warengonza.ga/covid19-tracker-cli |
| 120 | + ` |
| 121 | + }) |
| 122 | + |
| 123 | + const form = blessed.form({ |
| 124 | + parent: box, |
| 125 | + left: 0, |
| 126 | + top: 'center', |
| 127 | + height: 20, |
| 128 | + width: '100%', |
| 129 | + content: '\r\r\nPress [ENTER] to continue' |
| 130 | + |
| 131 | + }); |
| 132 | + |
| 133 | + form.on('submit', function(data) { |
| 134 | + form.setContent('Submitted.'); |
| 135 | + screen.render(); |
| 136 | + }); |
| 137 | + |
| 138 | + form.on('reset', function(data) { |
| 139 | + form.setContent('Canceled.'); |
| 140 | + screen.render(); |
| 141 | + }); |
| 142 | + |
| 143 | + const input = blessed.textbox({ |
| 144 | + parent: form, |
| 145 | + name: 'input', |
| 146 | + input: true, |
| 147 | + keys: true, |
| 148 | + top: 10, |
| 149 | + left: 'center', |
| 150 | + height: 20, |
| 151 | + width: '50%', |
| 152 | + }); |
| 153 | + |
| 154 | + input.focus(); |
| 155 | + |
| 156 | + input.key(['enter'], async () => { |
| 157 | + try { |
| 158 | + switch(input.value) { |
| 159 | + case '': |
| 160 | + case null: |
| 161 | + case undefined: |
| 162 | + console.log(executedInterval) |
| 163 | + clearInterval(executedInterval); |
| 164 | + form.hidden && form.show(); |
| 165 | + box.setContent('') |
| 166 | + form.setContent(initialContent); |
| 167 | + break; |
| 168 | + case '/all': |
| 169 | + executedInterval = renderTracker(box, screen); |
| 170 | + form.hide(); |
| 171 | + break; |
| 172 | + default: |
| 173 | + executedInterval = renderTracker(box, screen, input.value); |
| 174 | + form.hide(); |
| 175 | + break; |
| 176 | + } |
| 177 | + } catch(err) { |
| 178 | + console.log(err); |
| 179 | + form.setContent(`${initialContent}\n\n An Error Occured Please Press Enter Again`); |
| 180 | + } finally { |
| 181 | + input.clearValue() |
| 182 | + screen.render(); |
| 183 | + } |
| 184 | + }) |
| 185 | + |
| 186 | + client.on('data', () => { |
| 187 | + console.log(input.value); |
| 188 | + }) |
| 189 | + |
| 190 | + screen.render(); |
| 191 | +}).listen(2300); |
| 192 | + |
| 193 | +console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`) |
0 commit comments