Skip to content

Instantly share code, notes, and snippets.

@ashinzekene
Last active June 3, 2020 19:12
Show Gist options
  • Save ashinzekene/48130af8eebb384668a60b39a0af40c0 to your computer and use it in GitHub Desktop.
Save ashinzekene/48130af8eebb384668a60b39a0af40c0 to your computer and use it in GitHub Desktop.
// Run this script on this page https://www.worldometers.info/coronavirus/
const c = $$('#main_table_countries_today tr').map(el => ({
country: $(el).find('td:nth-of-type(2)').text(),
totalCases: $(el).find('td:nth-of-type(3)').text(),
testsPerMillion: $(el).find('td:nth-of-type(13)').text(),
tests: $(el).find('td:nth-of-type(12)').text(),
}))
const toNumber = ({ totalCases, tests, testsPerMillion, country }) => ({
country,
tests: parseInt(tests.replace(/,/g, '')),
totalCases: parseInt(totalCases.replace(/,/g, '')),
testsPerMillion: parseInt(testsPerMillion.replace(/,/g, '')),
})
const filterOutUnWanted = ({ country }) => (
// We dont want data for 'World', and the continents. Just countries
// Intrestingly Continents have a '\n' character
country && !['World', 'Total:'].includes(country) && !country.includes('\n')
)
const cases = c.filter(filterOutUnWanted).map(toNumber)
const greaterThan5k = cases.filter(c => c.totalCases && c.totalCases > 5000)
const greaterThan5kAndHasTestsPerMillion = greaterThan5k.filter(({ testsPerMillion, tests }) => testsPerMillion && tests)
const sumOfTestsPerMillion = greaterThan5kAndHasTestsPerMillion.reduce((total, c) => c.testsPerMillion + total, 0)
const averageTestPerMillion = sumOfTestsPerMillion / greaterThan5kAndHasTestsPerMillion.length
const testsPerMillion = greaterThan5kAndHasTestsPerMillion.map(({ testsPerMillion, country }) => ({
testsPerMillion,
country,
}))
testsPerMillion.sort((a, b) => a.testsPerMillion - b.testsPerMillion)
const testsToPositiveRatio = greaterThan5kAndHasTestsPerMillion.map(c => ({
ratio: c.totalCases / c.tests,
country: c.country
}))
testsToPositiveRatio.sort((a, b) => a.ratio - b.ratio)
console.log(
`No of countries with cases: ${cases.length}
Greater than 5k Cases: ${greaterThan5k.length}
Greater Than 5k Cases And Has Tests Per Million: ${greaterThan5kAndHasTestsPerMillion.length}`
)
console.log(`Average tests per million: ${averageTestPerMillion}`)
console.log()
console.log('Countries with min tests per million')
testsPerMillion.slice(0, 5).forEach(({ country, testsPerMillion }) => {
console.log(`Country: ${country}, Tests Per million: ${testsPerMillion}`)
})
console.log()
console.log('Countries with high tests per million')
testsPerMillion.reverse().slice(0, 5).forEach(({ country, testsPerMillion }) => {
console.log(`Country: ${country}, Tests Per million: ${testsPerMillion}`)
})
console.log()
console.log('Countries with low positivity ratio')
testsToPositiveRatio.slice(0, 5).forEach(({ country, ratio }) => {
console.log(`Country: ${country}, Ratio: ${ratio}`)
})
console.log()
console.log('Countries with high positivity ratio')
testsToPositiveRatio.reverse().slice(0, 5).forEach(({ country, ratio }) => {
console.log(`Country: ${country}, Ratio: ${ratio}`)
})
const nigeriaPRatio = testsToPositiveRatio.find(({ country }) => country === "Nigeria")
console.log("Nigeria's ratio: ", nigeriaPRatio.ratio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment