Commit d913988c authored by Michał Woźniak's avatar Michał Woźniak
Browse files

cleanups and separating functionality into functions

parent a9f17025
Pipeline #70 passed with stage
in 0 seconds
//const wtf = require('wtf_wikipedia')
let getEstimatedDays = (country) => {
console.log(`+-- fetching data for: ${country}`)
var data=false
var tdataPromise = wtf
/*
* get (and clean up) the cases data for a given Wikipedia-recognized country
*
* returns a promise that resolves to the data (if all goes well, that is)
*/
let getCountryCases = (country) => {
return wtf
.fetch(`Template:2019-20 coronavirus pandemic data/${country} medical cases chart`)
.then((doc)=>{
console.log(' +-- template fetched')
......@@ -11,6 +12,8 @@ let getEstimatedDays = (country) => {
})
.then((tdata)=>{
console.log(' +-- JSON extracted')
// this works for most templates
var data = tdata
.sections[0]
.templates
......@@ -31,25 +34,44 @@ let getEstimatedDays = (country) => {
.replace(/^([0-9,]+).*$/, '$1')
})
}
return data;
})
}
/*
* get population data of a Wikipedia-recognized country
*
* returns a promise that resolves to integer (if all goes well)
*/
let getCountryPopulation = (country) => {
return wtf
.fetch(country)
.then((doc)=>{
return doc.json()
}).then((tdata)=>{
var pop_estimate = tdata.sections[0].infoboxes[0].population_estimate
if ('number' in pop_estimate) {
return pop_estimate.number
} else {
return pop_estimate.text.split(/\n/g)[0].replace(/^([0-9,]+).*$/g, '$1').replace(/,/g, '')
}
})
}
let getEstimatedDays = (country) => {
console.log(`+-- fetching data for: ${country}`)
getCountryCases(country)
.then((data)=>{
var ratio = data[data.length-1] / data[data.length-2]
var cases = data[data.length-1]
console.log(` +-- cases: ${cases}`)
console.log(` +-- previous day day: ${data[data.length-2]}`)
console.log(` +-- ratio: ${ratio}`)
// get population data
wtf
.fetch(country)
.then((doc)=>{
return doc.json()
}).then((tdata)=>{
var pop_estimate = tdata.sections[0].infoboxes[0].population_estimate
if ('number' in pop_estimate) {
var population = pop_estimate.number
} else {
var population = pop_estimate.text.split(/\n/g)[0].replace(/^([0-9,]+).*$/g, '$1').replace(/,/g, '')
}
getCountryPopulation(country)
.then((population)=>{
console.log("Cases:", cases)
console.log("Infection ratio:", ratio)
console.log("Population:", population)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment