forked from Mireia17/fetching-data-reply
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
100 lines (65 loc) · 2.13 KB
/
main.js
File metadata and controls
100 lines (65 loc) · 2.13 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const google = require('./googlefetch.js');
const axios = require('axios');
const headers = {
Authorization: 'Token '
}
const url = 'https://www.reply.ai/api/v1/content/';
const getUrl = 'https://www.reply.ai/api/v1/contents.json';
function getAllData() {
const data = [];
let numPage = 1;
const getData = function(numPage) {
return axios.get(getUrl + '?page=' + numPage , { headers }).then(response => {
response.data.results.map(result => {
data.push(result);
//console.log(result);
});
if (response.data.next) {
numPage ++;
return getData(numPage);
} else {
return data;
}
});
}
return getData(numPage).then(function(result) {
return result;
})
.catch(function(error) {
console.error(error);
})
}
(async () => {
const googledata = await google.fetchGoogle();
// console.log(googledata);
const apidata = await getAllData();
const newdata = [];
apidata.map(item => { //map trough the items
const match = googledata.find(function(element) {
return item.name === element[0]; //compare two value_names
});
if (match && item.value != match[1]) {
item.value = match[1]; // if they don't match substitute them
}
newdata.push(item);
});
newdata.forEach(element => {
const options = {
method: 'PUT',
headers: {
'content-type': 'application/json',
'Authorization': 'Token '
},
data: {
"content_value": element.value
},
url: url + element.name + '.json'
};
axios(options).then(response => {
return response;
}).catch(err =>{
console.log('error');
console.log(err);
})
});
})();