Skip to content

Commit d3a0301

Browse files
committed
feat: image for web
1 parent 194b79c commit d3a0301

File tree

3 files changed

+318
-12
lines changed

3 files changed

+318
-12
lines changed

app.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,52 @@ app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) =>
130130
return next();
131131
});
132132

133+
// historical chart by country
134+
app.get('/history/charts/web/:country', async (req, res, next) => {
135+
const userAgent = req.headers['user-agent'],
136+
countryData = req.params.country,
137+
chartType = req.params.chartType || 'cases',
138+
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
139+
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`),
140+
s = summary.data,
141+
h = history.data;
142+
terminator = '\r\n'+'\033[?25h',
143+
xtermHTMLTemplate = require('./lib/cli/gfx/terminal-template'),
144+
bufferEmitter = new EventEmitter();
145+
146+
let HttpOutputBuffer = {
147+
send: (data) => {
148+
bufferEmitter.emit('screenBuffer', data)
149+
this.data = data
150+
return this;
151+
},
152+
get: () => {
153+
return this.data
154+
}
155+
};
156+
157+
covid19GFX.historyCountryTracker(
158+
req, HttpOutputBuffer,
159+
s.country, s.cases, s.todayCases,
160+
s.deaths, s.todayDeaths, s.recovered,
161+
s.active, s.critical, s.casesPerOneMillion,
162+
s.updated, h, chartType, s.countryInfo
163+
);
164+
165+
let screenOutput = (await new Promise((resolve, reject) => {
166+
bufferEmitter.on('screenBuffer', (screen) => {
167+
resolve(screen)
168+
})
169+
setTimeout(() => {
170+
resolve('')
171+
},5000)
172+
}))
173+
174+
if(!screenOutput.length || !h) res.send('Unable to load the chart.Please refresh the page')
175+
176+
return res.send(xtermHTMLTemplate(screenOutput.replace("", "\\'")))
177+
})
178+
133179
// historical chart by country
134180
app.get('/history/charts/:country', async (req, res, next) => {
135181
const userAgent = req.headers['user-agent'],
@@ -143,7 +189,7 @@ app.get('/history/charts/:country', async (req, res, next) => {
143189
xtermHTMLTemplate = require('./lib/cli/gfx/terminal-template'),
144190
bufferEmitter = new EventEmitter();
145191

146-
let customHttpResponse = {
192+
let HttpOutputBuffer = {
147193
send: (data) => {
148194
bufferEmitter.emit('screenBuffer', data)
149195
this.data = data
@@ -155,7 +201,7 @@ app.get('/history/charts/:country', async (req, res, next) => {
155201
};
156202

157203
covid19GFX.historyCountryTracker(
158-
req, customHttpResponse,
204+
req, HttpOutputBuffer,
159205
s.country, s.cases, s.todayCases,
160206
s.deaths, s.todayDeaths, s.recovered,
161207
s.active, s.critical, s.casesPerOneMillion,
@@ -171,9 +217,20 @@ app.get('/history/charts/:country', async (req, res, next) => {
171217
},5000)
172218
}))
173219

174-
if(!screenOutput.length || !h) res.send('Unable to load the chart.Please refresh the page')
220+
if (util.isCommandline(userAgent)) {
221+
return res.send(screenOutput+terminator)
222+
}
223+
224+
175225

176-
return util.isCommandline(userAgent) ? res.send(screenOutput+terminator) : res.send(xtermHTMLTemplate(screenOutput.replace("'", "\\'")))
226+
const puppeteer = require('puppeteer');
227+
const browser = await puppeteer.launch();
228+
const page = await browser.newPage();
229+
page.setViewport({width: 1440, height: 800})
230+
await page.goto(req.protocol + '://' + req.get('host') +'/history/charts/web/'+s.country);
231+
let p = await page.screenshot({encoding: 'base64', type: 'png'});
232+
await browser.close();
233+
return res.send(`<html><body style="margin:0;padding:0;"><img src="data:image/png;base64, ${p}" width="100%"/></body></html>`)
177234
})
178235

179236
app.get('*', (req, res) => res.send(`

0 commit comments

Comments
 (0)