@@ -5,6 +5,7 @@ const express = require('express'),
55 axios = require ( 'axios' ) ,
66 covid19 = require ( './lib/cli' ) ,
77 covid19GFX = require ( './lib/cli/gfx' ) ,
8+ EventEmitter = require ( 'events' ) ,
89 pkg = require ( './package.json' ) , // package.json info
910 apiBaseURL = "https://corona.lmao.ninja/v2" , // NovelCOVID API
1011 port = process . env . port || 7070 ; // set port
@@ -153,6 +154,59 @@ app.get('/history/charts/:country/:chartSize(sm|md|lg)?', async (req, res, next)
153154 return next ( ) ;
154155} ) ;
155156
157+
158+ // historical chart by country
159+ app . get ( '/history/web/charts/:country/:chartSize(sm|md|lg)?' , async ( req , res , next ) => {
160+ const userAgent = req . headers [ 'user-agent' ] ,
161+ countryData = req . params . country ,
162+ chartType = req . params . chartType || 'cases' ,
163+ chartSize = req . params . chartSize || 'sm' ,
164+ summary = await axios . get ( `${ apiBaseURL } /countries/${ countryData } ` ) ,
165+ history = await axios . get ( `${ apiBaseURL } /historical/${ summary . data . country } ?lastdays=all` ) ,
166+ s = summary . data ,
167+ h = history . data ,
168+ terminator = `\r\n` + '\033[?25h' ,
169+ xtermHTMLTemplate = require ( './lib/cli/gfx/terminal-html-template' ) ,
170+ bufferEmitter = new EventEmitter ( ) ;
171+
172+ let customHttpResponse = {
173+ send : ( data ) => {
174+ bufferEmitter . emit ( 'screenBuffer' , data )
175+ this . data = data
176+ return this ;
177+ } ,
178+ get : ( ) => {
179+ return this . data
180+ }
181+ } ;
182+
183+ covid19GFX . historyCountryTracker (
184+ req , customHttpResponse ,
185+ s . country , s . cases , s . todayCases ,
186+ s . deaths , s . todayDeaths , s . recovered ,
187+ s . active , s . critical , s . casesPerOneMillion ,
188+ s . updated , h , chartType , s . countryInfo , chartSize
189+ ) ;
190+
191+ let screenOutput = ( await new Promise ( ( resolve , reject ) => {
192+ bufferEmitter . on ( 'screenBuffer' , ( screen ) => {
193+ resolve ( screen )
194+ } )
195+ setTimeout ( ( ) => {
196+ resolve ( '' )
197+ } , 5000 )
198+ } ) ) ;
199+
200+
201+ if ( ! screenOutput . length || ! h ) res . send ( 'Unable to load the chart.Please refresh the page' )
202+
203+ return util . isCommandline ( userAgent ) ? res . send ( screenOutput + terminator ) : res . send ( xtermHTMLTemplate ( screenOutput . replace ( terminator , '' ) ) )
204+
205+ } ) ;
206+
207+ app . use ( '/xterm' , express . static ( __dirname + '/node_modules/xterm' ) ) ;
208+ app . use ( '/xterm/addons' , express . static ( __dirname + '/node_modules/xterm-addon-fit' ) ) ;
209+
156210// route for web browser clients
157211app . get ( '*' , ( req , res ) => res . send ( `
158212Welcome to COVID-19 Tracker CLI v${ pkg . version } by Waren Gonzaga\n
0 commit comments