forked from InteractiveAdvertisingBureau/iabtcf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-vendor-list.js
80 lines (69 loc) · 2.36 KB
/
update-vendor-list.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
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
72
73
74
75
76
77
78
79
80
const axios = require('axios');
const stream = require('stream');
const util = require('util');
const Fs = require('fs');
const baseURL = 'https://iabtcf.com';
const latestVersionURL= 'https://vendor-list.consensu.org/v2'
async function getExistingVersion() {
try{
const res = await axios.get(`${baseURL}/vendorlist/vendor-list.json`);
return res.data.vendorListVersion;
} catch(error){
return error;
}
}
async function getLatestVersion() {
try{
const res = await axios.get(`${latestVersionURL}/vendor-list.json`);
return res.data.vendorListVersion;
} catch(error){
return error;
}
}
async function updateGVLData(){
const existingVersion = await getExistingVersion();
const latestVersion = await getLatestVersion();
console.log("existingVersion", existingVersion);
console.log("latestVersion", latestVersion);
var counter = existingVersion;
if(counter < latestVersion){
while(counter < latestVersion){
counter++;
console.log(`writing file vendor-list-v${counter}.json`);
await downloadFile(`${latestVersionURL}/archives/vendor-list-v${counter}.json`, `./docs/vendorlist/archives/vendor-list-v${counter}.json`);
}
console.log(`updating file vendor-list.json`);
await removeOldFile(`./docs/vendorlist/vendor-list.json`);
await updateVendorList(`./docs/vendorlist/vendor-list.json`, `archives/vendor-list-v${latestVersion}.json`);
}
}
const finished = util.promisify(stream.finished);
async function downloadFile(fileUrl, outputLocationPath) {
const writer = Fs.createWriteStream(outputLocationPath);
return axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
}).then(response => {
response.data.pipe(writer);
return finished(writer); //this is a Promise
});
}
async function removeOldFile(path){
return Fs.unlink(path,function(err){
if(err) return console.log(err);
console.log('file deleted successfully');
});
}
async function updateVendorList(path, content){
return Fs.writeFile(path, content, (err) => {
if (err)
console.log(err);
else {
console.log("File written successfully\n");
console.log("The written has the following contents:");
console.log(Fs.readFileSync(path, "utf8"));
}
});
}
updateGVLData();