-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation.js
42 lines (37 loc) · 1.53 KB
/
population.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import unzipper from 'unzipper'
import { parse } from 'csv-parse/sync'
import { countryToCode } from './utils.js'
const readRemoteZippedCSV = async (url) => {
const response = await fetch(url)
const buffer = await response.arrayBuffer()
const directory = await unzipper.Open.buffer(Buffer.from(buffer))
const mainFile = await directory.files[0].buffer()
return parse(mainFile,
{ columns: true, skipEmptyLines: true, encoding: 'utf8' })
}
const fixBrokenUtf8 = (s) => Buffer.from(s, 'ascii').toString('utf8')
const getPopulationDataFromItem = (item) => {
const countryName = fixBrokenUtf8(item['Country or Area'])
const population = Math.round(parseFloat(fixBrokenUtf8(item.Value)) * 1000)
const countryCode = countryToCode(countryName)
return { countryCode, population }
}
const getPopulationZip = async () => {
const populationUrl = 'http://data.un.org/Handlers/DownloadHandler.ashx?DataFilter=variableID:12;varID:2&DataMartId=PopDiv&Format=csv&c=2,4,7&s=_crEngNameOrderBy:asc,_timeEngNameOrderBy:desc,_varEngNameOrderBy:asc'
return await readRemoteZippedCSV(populationUrl)
}
export const populationInfo = async () => {
const itemsRaw = await getPopulationZip()
const thisYear = new Date().getFullYear().toString()
const items = itemsRaw.filter(i => i['Year(s)'] === thisYear)
const result = {}
for (const item of items) {
try {
const { countryCode, population } = getPopulationDataFromItem(item)
result[countryCode] = population
} catch (e) {
console.log(e.message)
}
}
return result
}