-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
167 lines (153 loc) · 5.21 KB
/
Copy pathscript.js
File metadata and controls
167 lines (153 loc) · 5.21 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const fetch = require('node-fetch');
const fs = require('fs').promises;
let wpPages = 1;
let wpPagesTotal = 1;
let customPostTypesRoute = [];
let url = `https://www.kuhar.ba/wp-json/wp/v2/posts?per_page=5`;
let recepies_url = `https://www.kuhar.ba/wp-json/wp/v2/recipes?per_page=5`;
let settings = {
method: "Get"
};
const defaultPostTypes = ['page', 'post', 'attachment', 'wp_block', 'revision'];
const postTypesRoute = `https://www.kuhar.ba/wp-json/wp/v2/types`;
fetch(url, settings)
.then(res => {
if (res) {
fetchPrepare(res);
}
return res;
});
fetch(recepies_url, settings)
.then(res => {
if (res) {
fetchRecepiesPrepare(res);
}
return res;
});
const fetchPrepare = async (res, customType = 'posts') => {
let array = Array.from({ length: res.headers.get('x-wp-totalpages')}, (_, k) => k)
.filter(x=>x!=0);
console.log(array.length + '=' + customType);
let detail = "";
asyncForEach(array, async(data, index, array) => {
index == 0? index++: index;
detailUrl = `https://www.kuhar.ba/wp-json/wp/v2/${customType}?per_page=5&page=${index}`;
await fetchPostsPerPage(index, detailUrl,customType).then((data) => {
return new Promise((resolve)=> setTimeout(resolve, 1000));
}).then(()=>{console.log('next' + index);});
});
};
const fetchRecepiesPrepare = async (res, customType = 'recipes') => {
let array = Array.from({ length: res.headers.get('x-wp-totalpages')}, (_, k) => k)
.filter(x=>x!=0);
console.log(array.length + '=' + customType);
let detail = "";
asyncForEach(array, async(data, index, array) => {
index == 0? index++: index;
detailUrl = `https://www.kuhar.ba/wp-json/wp/v2/${customType}?per_page=5&page=${index}`;
await fetchPostsPerPage(index, detailUrl,customType).then((data) => {
return new Promise((resolve)=> setTimeout(resolve, 1000));
}).then(()=>{console.log('next page = ' + index);});
});
};
const fetchPostInfoDetaily = (url, id, parent) => {
if (url === 'author') {
url = 'users';
}
else if (url === 'featured_media') {
url = 'media';
}
let detailInfoUrl = `https://www.kuhar.ba/wp-json/wp/v2/${url}/${id}`;
let key = 'name';
let detail = '';
key = url!=='media'? 'name': 'source_url';
return fetch(detailInfoUrl, settings)
.then(res=>res.json())
.then(json => {
//Get key name of object or whole object
if (parent) {
return json;
}
else {
return json[key];
}
});
};
const fetchCategoryParents = async (currentCategory,categories) => {
let dataTemp = {};
if (!categories.includes(currentCategory.name)) {
categories.push({
name: currentCategory.name,
slug: currentCategory.slug
});
}
if (currentCategory.parent > 0) {
await fetchPostInfoDetaily('categories', currentCategory.parent, 'yes')
.then(async(data) => {
dataTemp = data;
if (!categories.includes(data.name)) {
categories.push({
name: data.name,
slug: data.slug
});
}
if (dataTemp.parent > 0) {
return fetchCategoryParents(dataTemp, categories);
} else {
return Promise.resolve(categories);
}
});
}
return Promise.resolve(categories);
};
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const fetchPostsPerPage = async (page, url, controller) => {
let resultPagination = 0;
if (page > 20) {
resultPagination = Math.floor(page / 20);
}
return fetch(url, settings)
.then(res => res.json())
.then(async (json) => {
let approvedProperties = ['id','date','date_gmt','slug','status','type','link','title',
'content','excerpt', 'author','featured_media','categories','tags','recipe_difficulty',
'recipe_meal_course', 'recipe_serving', 'recipe_cooking_time', 'recipe_preparation_time', 'recipe_instructions',
'recipe_ingredients', 'recipe_categories'];
await asyncForEach(json, async item => {
let detailInfo = '';
for (let key in item) {
if (!approvedProperties.includes(key)) {
delete item[key];
}
if (key == 'content' || key == 'excerpt' || key == 'title') {
let rendered = item[key].rendered;
delete item[key].rendered;
item[key] = rendered;
}
else if (key == 'author' || key == 'featured_media') {
await fetchPostInfoDetaily(key, item[key])
.then(data=> {
item[key] = data;
})
}
else if (key == 'categories') {
await fetchPostInfoDetaily(key, item[key], 'yes')
.then(async data=> {
let categories = await fetchCategoryParents(data, new Array())
.then((categoriesWithParent) => {
item[key] = categoriesWithParent;
});
});
}
}
});
let jsonToBeFixed = JSON.stringify(json);
jsonToBeFixed = jsonToBeFixed.replace('][', ',');
await fs.appendFile(`${controller}_result_${resultPagination}.json`, jsonToBeFixed);
return Promise.resolve();
});
};