-
Notifications
You must be signed in to change notification settings - Fork 0
/
importUN.mjs
71 lines (49 loc) · 1.93 KB
/
importUN.mjs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
import {get, post, waitFor, asyncForEach} from './utils.mjs';
const api_token = process.env.API_TOKEN;
const api_url = process.env.HOST;
const config = {
headers: {'User-Agent': 'python-requests', 'Content-Type': 'application/json'}
};
async function runProcess() {
const result = await axios.get('https://unstats.un.org/SDGAPI/v1/sdg/Series/List?allreleases=false', config);
let UNPackages = result.data;
console.log('UN', UNPackages);
console.log('Datasets found:', UNPackages.length)
let apiUNDatasetResponse = await get('v1/dataset?provider=un&page[size]=10000', api_url, api_token)
console.log(apiUNDatasetResponse.data)
let tableNames = apiUNDatasetResponse.data.map(x => x.attributes.tableName)
await asyncForEach(UNPackages, async (unPackage) => {
await waitFor(50)
await addDataset(unPackage, tableNames);
})
//apiUNDatasetList = list(map(lambda x: x['attributes']['tableName'], apiUNDatasetResponse['data']))
}
async function addDataset(dataset, tableNamesCodes) {
if (tableNamesCodes.indexOf(dataset.code) > -1) {
console.warn('Dataset ' + dataset.code + ' already exists, skipping...');
return;
}
console.log('Adding dataset ' + dataset['code'])
let result = await post(
{
"name": dataset.description,
"provider": "un",
"connectorType": "rest",
"tableName": dataset.code
}, 'v1/dataset', api_url, api_token)
let dataset_id = result.data.id
let status = 'pending'
while (status == 'pending'){
let get_result = await get('v1/dataset/' + dataset_id, api_url, api_token)
status = get_result.data.attributes.status;
if (status == 'pending') {
console.log('Sleeping...')
await waitFor(2000)
}
}
console.log('dataset added')
}
runProcess();