-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprueba.js
40 lines (36 loc) · 1.51 KB
/
prueba.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
const got = require('got');
let accessToken = ''; /* TODO: Fill it */
let category = 'MLA431427' /* TODO: Fill it */
let installments = true /* TODO: Fill it */
let itemNew = true /* TODO: Fill it */
(async () => {
let offset = 0;
let loop = true;
let url = `https://api.mercadolibre.com/sites/MLA/search?category=${category}${installments ? '&installments=yes' : ''}${itemNew ? '&ITEM_CONDITION=2230284' : ''}&access_token=${accessToken}&offset=${offset}`;
let itemsOrderedBySoldQuantity = [];
while(loop) {
try {
console.log(offset);
let call = await got(url);
let json = JSON.parse(call.body);
// Lets check if response is ok to continue loop
if (!json.paging || !json.paging.total || offset > json.paging.total) {
loop = false;
break;
}
json.results.forEach(item => {
if (item.sold_quantity && item.id) {
itemsOrderedBySoldQuantity.push({sold_quantity: item.sold_quantity, id: item.id, permalink: item.permalink});
}
});
offset += 50;
url = `https://api.mercadolibre.com/sites/MLA/search?category=${category}${installments ? '&installments=yes' : ''}${itemNew ? '&ITEM_CONDITION=2230284' : ''}&access_token=${accessToken}&offset=${offset}`;
} catch (error) {
console.log(error);
console.log(error.response.body);
loop = false;
}
}
itemsOrderedBySoldQuantity.sort((a,b) => b.sold_quantity - a.sold_quantity);
console.log(itemsOrderedBySoldQuantity.map((item) => item.permalink));
})();