-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (143 loc) · 4.3 KB
/
index.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
const fetch = require("node-fetch");
const express = require("express");
const path = require("path");
const port = process.env.PORT || 8085;
const app = express();
const numberOfItemsOnOnePage = 10;
const maximumOfRequests = 20;
const currency = "CZK";
let saunaLinks = "";
let numberOfRequests = 0;
app.use(express.static(__dirname, { dotfiles: "allow" }));
app.engine("html", require("ejs").renderFile);
app.get("/", async function(req, res) {
saunaLinks = "";
numberOfRequests = 0;
if (req.query) {
console.log(req.query.query);
await fetchResultPage(req.query.query);
res.render(__dirname + "/public/index.html", { links: saunaLinks });
}
});
app.listen(port);
const editableParams = {
// query: "Helsinki, Finland",
// checkin: "2020-04-01",
// checkout: "2020-04-17",
price_min: 0,
price_max: 10000,
adults: 6
// infants: 0,
// superhost: "true",
};
const headers = {
accept: "application/json",
"accept-encoding": "br, gzip, deflate",
"content-type": "application/json",
"x-airbnb-api-key": "915pw2pnf4h1aiguhph5gc5b2",
"x-airbnb-currency": currency,
"x-airbnb-locale": "en",
"x-airbnb-carrier-country": "us",
"accept-language": "en-us"
};
const getParams = (itemsOffset = 0) => ({
...editableParams,
_limit: numberOfItemsOnOnePage,
toddlers: "0",
is_guided_search: "true",
version: "1.4.8",
section_offset: "0",
items_offset: itemsOffset,
screen_size: "small",
source: "explore_tabs",
items_per_grid: "500",
_format: "for_explore_search_native",
metadata_only: "false",
"refinement_paths[]": "/homes",
timezone: "Europe/Prague",
satori_version: "1.1.0"
});
const filterAsync = (array, filter) =>
Promise.all(array.map(entry => filter(entry))).then(bits =>
array.filter(entry => bits.shift())
);
const fetchHome = id => {
if (numberOfRequests > maximumOfRequests) return;
const request = fetch(
`https://api.airbnb.com/v2/pdp_listing_details/${id}?_format=for_native`,
{
method: "get",
headers: headers
}
)
.then(body =>
body.json().catch(e => {
// console.log(`Not a JSON response (ID: ${id})`);
})
)
.then(body => {
if (body) {
const descriptionObject = body.pdp_listing_detail.sectioned_description;
const photo = body.pdp_listing_detail.photos[0].large;
const searchedInFields = [
"space",
"summary",
"notes",
"name",
"access"
];
const isSaunaAvailable =
searchedInFields
.map(field => descriptionObject[field])
.join(" ")
.indexOf("saun") !== -1;
if (isSaunaAvailable) {
const params = getParams();
const homeUrl = `https://www.airbnb.cz/rooms/${id}`;
const homeUrlWithDateInterval =
params.checkin && params.checkout
? `${homeUrl}?check_in=${params.checkin}&check_out=${params.checkout}`
: homeUrl;
saunaLinks = `${saunaLinks}<a href="${homeUrlWithDateInterval}" target="_blank"><div class="item" style="background-image: url('${photo}')"></div></a>`;
console.log(`- ${homeUrlWithDateInterval}`);
}
}
})
.catch(e => {
console.log(e);
});
numberOfRequests++;
return request;
};
const fetchResultPage = async (query = null, offset = 0) => {
if (numberOfRequests > maximumOfRequests || query === null) return;
const params = getParams(offset);
console.log(`Query: ${query}`);
const paramString =
Object.keys(params)
.map(key => `${key}=${encodeURIComponent(params[key])}`)
.join("&") + `&query=${query}`;
console.log(`https://api.airbnb.com/v2/explore_tabs?${paramString}`);
const request = await fetch(
`https://api.airbnb.com/v2/explore_tabs?${paramString}`,
{
method: "get",
headers: headers
}
);
try {
const body = await request.json();
if (body) {
const ids = body.explore_tabs[0].home_tab_metadata.remarketing_ids;
const homePromise = await filterAsync(ids, fetchHome);
if (body.explore_tabs[0].pagination_metadata.has_next_page) {
await fetchResultPage(query, offset + numberOfItemsOnOnePage);
}
}
} catch (err) {
console.log("error");
}
numberOfRequests++;
};
console.log(`Saunas\n============================`);
fetchResultPage();