-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
267 lines (253 loc) · 7.66 KB
/
solution.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const request = require('request');
const Promise = require('bluebird');
const cheerio = require('cheerio');
const fs = require('fs');
const async = require("async");
//environment
const API_URL = 'https://www.bankmega.com/promolainnya.php';
const AJAX_URL = 'https://www.bankmega.com/ajax.promolainnya.php';
const WEB_URL = 'https://bankmega.com';
//data
let categories = [];
let subcategories = [];
let subcategoryData = {};
let categoryData = {};
let jsonData = {};
const fetchData = async() => {
try {
await request({
method: 'GET',
url: API_URL
}, (err, res, body) => {
if (!err) {
const $ = cheerio.load(body);
console.log('magic is starting');
getCategory($);
clickCategory(categories);
setTimeout(() => {
getDetailInfo(categoryData);
}, 3000);
}
});
} catch (err) {
console.error(err);
}
}
const setUrl = (category, subcat, $) => {
try {
$('#promolain').children().map((index, item) => {
if ($(item).children().attr('href').includes('promo_detail')) {
if (subcat) {
if (!(category in categoryData)) categoryData[category] = {};
if (!(subcat in categoryData[category])) categoryData[category][subcat] = [];
if (!(subcat in subcategoryData)) subcategoryData[subcat] = [];
if ($(item).children().attr('href').includes('https://www.bankmega.com/')) {
categoryData[category][subcat].push($(item).children().attr('href'));
subcategoryData[subcat].push($(item).children().attr('href'));
} else {
categoryData[category][subcat].push('https://www.bankmega.com/' + $(item).children().attr('href'));
subcategoryData[subcat].push('https://www.bankmega.com/' + $(item).children().attr('href'));
}
} else {
if (!(category in categoryData)) categoryData[category] = [];
if ($(item).children().attr('href').includes('https://www.bankmega.com/')) {
categoryData[category].push($(item).children().attr('href'));
} else {
categoryData[category].push('https://www.bankmega.com/' + $(item).children().attr('href'));
}
}
}
})
} catch (err) {
console.error(err);
}
}
const getCategory = ($) => {
try {
$('#contentpromolain2').filter((i, el) => {
let data = $(el).children().first().children().next().first().children();
data.map((index, item) => {
if (index > 0) {
categories.push($(item).attr("id"));
}
});
});
} catch (err) {
console.error(err);
}
}
const clickCategory = (data) => {
data.map(async(item, index) => {
try {
let product = index + 1
await request({
method: 'GET',
url: AJAX_URL + `?product=${product}`
}, (err, res, body) => {
if (!err) {
let $ = cheerio.load(body);
let isSubcat = false;
if ($('div').find('#subcatpromo').length > 0) {
let subcat = $('div').find('#subcatpromo').children();
subcat.map((index, item) => {
subcategories.push($(item).children().attr('title'));
})
isSubcat = true;
}
let category = $('div').find('.menupromoinside').text();
getDataPerPage(category, product, isSubcat, $);
}
});
} catch (err) {
console.error(err);
}
});
}
const getDataPerPage = async(category, product, isSubcat = false, $) => {
if (isSubcat) {
subcategories.map(async(item, index) => {
try {
let subcat = index + 1;
await request({
method: 'GET',
url: AJAX_URL + `?product=${product}&subcat=${subcat}`
}, (err, res, body) => {
if (!err) {
let $$ = cheerio.load(body);
let total_page = $$('.tablepaging').children().children().children().last().children().attr('title').split(" ").slice(-1)[0];
const mapLoop = async() => {
for (let i = 1; i <= total_page; i++) {
try {
await request({
method: 'GET',
url: AJAX_URL + `?product=${product}&subcat=${subcat}&page=${i}`
}, (err, res, body) => {
if (!err) {
let $$$ = cheerio.load(body);
setUrl(category, item, $$$);
}
});
} catch (err) {
console.error(err);
}
}
}
mapLoop();
}
});
} catch (err) {
console.error(err);
}
});
} else {
let total_page = $('.tablepaging').children().children().children().last().children().attr('title').split(" ").slice(-1)[0];
for (let i = 1; i <= total_page; i++) {
try {
await request({
method: 'GET',
url: AJAX_URL + `?product=${product}&page=${i}`
}, (err, res, body) => {
if (!err) {
let $$ = cheerio.load(body);
setUrl(category, null, $$);
}
});
} catch (err) {
console.error(err);
}
}
}
}
//get detail data
const getDetailInfo = (urls) => {
async.forEach(urls, async(val, next) => {
try {
if (val.map) {
await Promise.all(val.map(requestDetailInfo));
} else {
async.forEach(val, async(_val, next) => {
try {
await Promise.all(_val.map(requestDetailInfo));
} catch (err) {
console.error(err);
}
})
}
setJson(jsonData);
} catch (err) {
console.error(err);
}
});
}
const requestDetailInfo = async(url) => {
try {
await new Promise((resolve, reject) => {
let req = request(url, (err, res, body) => {
if (!err) {
let $ = cheerio.load(body);
let category = getParentName(categoryData, url);
let subcategory = ''
if (category === 'Kartu Kredit') {
subcategory = getParentName(subcategoryData, url);
}
resolve(getDetail(category, subcategory, $))
}
});
});
process.stdout.write(".");
} catch (err) {
console.error(err)
}
};
const getDetail = (category, subcategory, $) => {
$('#contentpromolain2').filter((i, el) => {
let element = $(el);
let title = '',
area = '',
period = '',
image = '';
if (element.find('.titleinside').length > 0) {
title = element.find('.titleinside').children().text();
}
if (element.find('.area').length > 0) {
area = element.find('.area').children().text();
}
if (element.find('.periode').length > 0) {
period = element.find('.periode').find('b').text();
}
if (element.find('.keteranganinside').length > 0) {
image = element.find('.keteranganinside').find('img').attr('src');
}
let data = {
"title": title,
"area": area,
"period": period,
"imageurl": WEB_URL + image
}
if (subcategory !== '') {
if (!(category in jsonData)) jsonData[category] = {};
if (!(subcategory in jsonData[category])) jsonData[category][subcategory] = [];
jsonData[category][subcategory].push(data);
} else {
if (!(category in jsonData)) jsonData[category] = [];
jsonData[category].push(data);
}
})
};
//function
const setJson = async(data) => {
await fs.writeFile('solution.json', JSON.stringify(data, null, 2), (err) => {
if (err) {
console.log('error', err.message);
}
})
};
const getParentName = (object, value) => {
try {
return Object.keys(object).find(key => object[key].includes(value));
} catch (err) {
return 'Kartu Kredit';
}
};
//start
fetchData();