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

we're not talking about countries anymore, but locations

parent 6a0ec30f
Pipeline #80 passed with stage
in 1 second
......@@ -2,7 +2,7 @@
* yes, hard-coding. shoot me.
* if you want to go fancy, you can use the getCountries() function below instead.
*/
var countries = [
var sites = [
"Afghanistan",
"Albania",
"Algeria",
......@@ -142,8 +142,8 @@ var countries = [
"Poland",
"Portugal",
"Qatar",
"Republic of Korea",
"Republic of Moldova",
"South Korea",
"Moldova",
"Romania",
"Russia",
"Rwanda",
......@@ -195,19 +195,23 @@ var countries = [
"Viet Nam",
"Yemen",
"Zambia",
"Zimbabwe"
"Zimbabwe",
"Hong Kong"
]
/*
* get a list of countries
* get a list of infection outbreak sites
* from https://en.wikipedia.org/wiki/Member_states_of_the_United_Nations
*
* yes, naïve and limited, I know.
*
* also, no, we should not be using this everytime the interface is displayed
* world political situation is unstable, but not *that* unstable
*
* we probably should take the list from here instead:
* https://en.wikipedia.org/wiki/Template:2019%E2%80%9320_coronavirus_pandemic_data
*/
let getCountries = () => {
let getSite = () => {
return wtf
.fetch('Member states of the United Nations')
.then((doc)=>{
......@@ -218,8 +222,8 @@ let getCountries = () => {
.sections[2]
.tables[0]
.map((row)=>{
var sane_country_name = row["Member state"].text.replace(/ \(.+\)$/, '')
switch (sane_country_name) {
var sane_site_name = row["Member state"].text.replace(/ \(.+\)$/, '')
switch (sane_site_name) {
case 'United Kingdom of Great Britain and Northern Ireland':
return "United Kingdom";
break;
......@@ -235,8 +239,14 @@ let getCountries = () => {
case 'Russian Federation':
return "Russia";
break;
case 'Republic of Korea':
return "South Korea";
break;
case 'Republic of Moldova':
return "Moldova";
break;
default:
return sane_country_name;
return sane_site_name;
}
})
})
......@@ -271,23 +281,23 @@ let getCovidData = () => {
/*
* get (and clean up) the cases data for a given Wikipedia-recognized country
* get (and clean up) the cases data for a given Wikipedia-recognized outbreak site
*
* returns a promise that resolves to the an array of objects, each containing two keys:
* - date
* - cases
* (if all goes well, that is)
*/
let getCountryCases = (country) => {
let getSiteCases = (site) => {
// snowflake templates are snowflake
if (country === "China") {
country = "Mainland China"
if (site === "China") {
site = "Mainland China"
}
console.log(`+-- fetching covid data for: ${country}`)
console.log(`+-- fetching covid data for: ${site}`)
return wtf
.fetch(`Template:2019-20 coronavirus pandemic data/${country} medical cases chart`)
.fetch(`Template:2019-20 coronavirus pandemic data/${site} medical cases chart`)
.then((doc)=>{
console.log(`+-- got covid data for: ${country}`)
console.log(`+-- got covid data for: ${site}`)
tdata = doc.json()
// this works for most templates
var data = tdata
......@@ -332,18 +342,18 @@ let getCountryCases = (country) => {
/*
* get population data of a Wikipedia-recognized country
* get population data of a Wikipedia-recognized site
*
* returns a promise that resolves to integer (if all goes well)
*/
let getCountryPopulation = (country) => {
// snowflake country pages are snowflake
if (country === "Georgia") {
country = "Georgia (country)"
let getSitePopulation = (site) => {
// snowflake site pages are snowflake
if (site === "Georgia") {
site = "Georgia (site)"
}
console.log(`+-- fetching population data for ${country}`)
console.log(`+-- fetching population data for ${site}`)
return wtf
.fetch(country)
.fetch(site)
.then((doc)=>{
infobox = doc.json().sections[0].infoboxes[0]
if (`population_estimate` in infobox) {
......@@ -357,7 +367,7 @@ let getCountryPopulation = (country) => {
} else {
population = pop_data.text.split(/\n/g)[0].replace(/^([0-9,]+).*$/g, '$1').replace(/,/g, '')
}
console.log(`+-- got population data for ${country}:`
console.log(`+-- got population data for ${site}:`
+ `\n population: ${population}`)
return population
})
......@@ -365,8 +375,8 @@ let getCountryPopulation = (country) => {
let getEstimatedDays = (country) => {
getCountryCases(country)
let getEstimatedDays = (site) => {
getSiteCases(site)
.then((wikiData)=>{
data = wikiData.data
var ratio = data[data.length-1].cases / data[data.length-2].cases
......@@ -375,7 +385,7 @@ let getEstimatedDays = (country) => {
console.log(` +-- cases: ${cases}`)
console.log(` +-- previous day day: ${data[data.length-2].cases}`)
console.log(` +-- ratio: ${ratio}`)
getCountryPopulation(country)
getSitePopulation(site)
.then((population)=>{
console.log("Cases:", cases)
console.log("Infection ratio:", ratio)
......@@ -394,47 +404,47 @@ let getEstimatedDays = (country) => {
*/
document.addEventListener('DOMContentLoaded', (e)=>{
// fill out country drop-down
var countrySelect = document.getElementById("countries");
for (country in countries) {
// fill out site drop-down
var siteSelect = document.getElementById("sites");
for (site in sites.sort()) {
var cOpt = document.createElement("option");
cOpt.value = countries[country];
cOpt.innerHTML = countries[country];
countrySelect.appendChild(cOpt);
cOpt.value = sites[site];
cOpt.innerHTML = sites[site];
siteSelect.appendChild(cOpt);
}
// handle user interaction
var countrySelectTimeout = false
var countryData = document.getElementById("country-data");
countrySelect.addEventListener('change', (e) => {
var siteSelectTimeout = false
var siteData = document.getElementById("site-data");
siteSelect.addEventListener('change', (e) => {
if (countrySelectTimeout !== false) {
clearTimeout(countrySelectTimeout)
countrySelectTimeout = false
if (siteSelectTimeout !== false) {
clearTimeout(siteSelectTimeout)
siteSelectTimeout = false
}
// need to handle the corner cases, eh
var country = event.target.value
var site = event.target.value
countrySelectTimeout = setTimeout(function(){
countrySelectTimeout = false
console.log(`+-- fetching data for: ${country}`)
countryData.innerHTML = `<div class="please-wait">Fetching data, please wait...</div>`
getCountryCases(country)
siteSelectTimeout = setTimeout(function(){
siteSelectTimeout = false
console.log(`+-- fetching data for: ${site}`)
siteData.innerHTML = `<div class="please-wait">Fetching data, please wait...</div>`
getSiteCases(site)
.then((wikiData)=>{
var data = wikiData.data
var ratio = data[data.length-1].cases / data[data.length-2].cases
var cases = data[data.length-1].cases
console.log(`+-- covid data for: ${country}`
console.log(`+-- covid data for: ${site}`
+ `\n date : ${data[data.length-1].date}`
+ `\n cases : ${cases}`
+ `\n cases day prior : ${data[data.length-2].cases}`
+ `\n ratio : ${ratio}`
+ `\n source link : ${wikiData.source_link}`
+ `\n source title : ${wikiData.source_title}`)
getCountryPopulation(country)
getSitePopulation(site)
.then((population)=>{
countryData.innerHTML = `
siteData.innerHTML = `
<div><span class="label">Population: </span><span class="value">${population}</span></div>
<div><span class="label">Cases: </span><span class="value">${cases}</span></div>
<div><span class="label">Infected as percent of population: </span><span class="value">${Math.round((cases/population)*10000)/100}%</span></div>
......@@ -446,7 +456,7 @@ document.addEventListener('DOMContentLoaded', (e)=>{
})
})
.catch((e)=>{
countryData.innerHTML = `<div class="error">Something went wrong. Either a bug, or no data.</div>`
siteData.innerHTML = `<div class="error">Something went wrong. Either a bug, or no data.</div>`
})
}, 500)
});
......
......@@ -16,15 +16,15 @@
</div>
<hr/>
<div>
<label for="countries">Choose a country:</label>
<select id="countries" tabindex="1">
<label for="sites">Choose a site:</label>
<select id="sites" tabindex="1">
<option value="select one">(select one)</option>
</select>
</div>
<div id="country-data"><div>
<div id="site-data"></div>
<hr/>
<footer>
<p>License: <a href="./LICENSE">Affero GPL</a>; uses <a href="https://github.com/spencermountain/wtf_wikipedia"><code>wtf_wikipedia</code></a>, licensed under the MIT license. Data from Wikipedia.</p>
<p><a href="https://git.rys.io/rysiek/covid/">Code</a>. License: <a href="./LICENSE">Affero GPL</a>; uses <a href="https://github.com/spencermountain/wtf_wikipedia"><code>wtf_wikipedia</code></a>, licensed under the MIT license. Data from Wikipedia.</p>
</footer>
</body>
</html>
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