Skip to content

Commit 0e67cf8

Browse files
committed
Implement paging to results
1 parent dfa6352 commit 0e67cf8

2 files changed

Lines changed: 46 additions & 32 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ Open links in Terminal (Mac): `CMD + [double click]`
2929
- Petr Polášek
3030
- Jiří Ostatnický
3131

32-
Hackaton at STRV, (6.11.2019)
32+
Private Hackaton, River Garden II, (6.11.2019)

index.js

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ const headers = {
1010
"x-airbnb-carrier-country": "us",
1111
"accept-language": "en-us"
1212
};
13-
const params = {
14-
query: "Norway, Lyngen",
15-
adults: "6",
13+
14+
const numberOfItemsOnOnePage = 50;
15+
const maximumRequests = 60;
16+
let numberOfRequests = 0;
17+
18+
const getParams = (itemsOffset = 0) => ({
19+
query: "Sweden, Stockholm",
20+
adults: "8",
1621
toddlers: "0",
1722
infants: "0",
1823
is_guided_search: "true",
1924
version: "1.4.8",
2025
section_offset: "0",
21-
items_offset: "0",
26+
items_offset: itemsOffset,
2227
screen_size: "small",
2328
source: "explore_tabs",
2429
items_per_grid: "500",
@@ -27,12 +32,15 @@ const params = {
2732
"refinement_paths[]": "/homes",
2833
timezone: "Europe/Prague",
2934
satori_version: "1.1.0"
30-
};
35+
});
36+
37+
const filterAsync = (array, filter) =>
38+
Promise.all(array.map(entry => filter(entry))).then(bits =>
39+
array.filter(entry => bits.shift())
40+
);
3141

32-
const paramString = Object.keys(params)
33-
.map(key => `${key}=${encodeURIComponent(params[key])}`)
34-
.join("&");
3542
const fetchHome = id => {
43+
if (numberOfRequests > maximumRequests) return;
3644
const request = fetch(
3745
`https://api.airbnb.com/v2/pdp_listing_details/${id}?_format=for_native`,
3846
{
@@ -70,30 +78,36 @@ const fetchHome = id => {
7078
.catch(e => {
7179
console.log(e);
7280
});
81+
numberOfRequests++;
7382
return request;
7483
};
7584

76-
const filterAsync = (array, filter) =>
77-
Promise.all(array.map(entry => filter(entry))).then(bits =>
78-
array.filter(entry => bits.shift())
79-
);
80-
81-
fetch(`https://api.airbnb.com/v2/explore_tabs?${paramString}`, {
82-
method: "get",
83-
headers: headers
84-
})
85-
.then(body => body.json().catch(e => null))
86-
.then(body => {
87-
if (body) {
88-
const ids = body.explore_tabs[0].home_tab_metadata.remarketing_ids;
89-
console.log(
90-
`Saunas ${params.query} (search in ${
91-
ids.length
92-
} results)\n============================`
93-
);
94-
const saunasUrls = filterAsync(ids, fetchHome);
95-
}
85+
const fetchResultPage = (offset = 0) => {
86+
if (numberOfRequests > maximumRequests) return;
87+
const params = getParams(offset);
88+
const paramString = Object.keys(params)
89+
.map(key => `${key}=${encodeURIComponent(params[key])}`)
90+
.join("&");
91+
// console.log(`https://api.airbnb.com/v2/explore_tabs?${paramString}`);
92+
fetch(`https://api.airbnb.com/v2/explore_tabs?${paramString}`, {
93+
method: "get",
94+
headers: headers
9695
})
97-
.catch(e => {
98-
console.log(e);
99-
});
96+
.then(body => body.json().catch(e => null))
97+
.then(body => {
98+
if (body) {
99+
const ids = body.explore_tabs[0].home_tab_metadata.remarketing_ids;
100+
101+
const saunasUrls = filterAsync(ids, fetchHome);
102+
if (body.explore_tabs[0].pagination_metadata.has_next_page) {
103+
fetchResultPage(offset + numberOfItemsOnOnePage);
104+
}
105+
}
106+
})
107+
.catch(e => {
108+
console.log(e);
109+
});
110+
numberOfRequests++;
111+
};
112+
console.log(`Saunas ${getParams().query}\n============================`);
113+
fetchResultPage();

0 commit comments

Comments
 (0)