-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (64 loc) · 2.04 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
const {
Builder, By,
} = require('selenium-webdriver');
const {
getBedroomListingUrl,
readData,
writeData,
} = require('./utils');
const getListingDetails = async (root) => {
const priceEl = await root.findElement(By.css('p.listing-result__price'));
const price = await priceEl.getText();
const anchorEl = await root.findElement(By.css('a.address.is-two-lines'));
const href = await anchorEl.getAttribute('href');
const address = await anchorEl.getAttribute('text');
return {
price,
href,
address,
};
};
const updateData = async (input) => {
const driver = await new Builder().forBrowser('chrome').build();
try {
let i = 1;
while (i <= 7) {
const url = getBedroomListingUrl(i);
i += 1;
await driver.get(url);
const listingRoots = await driver.findElements(By.css('div.listing-result__details.listing-result__right'));
const data = await Promise.all(listingRoots.map(getListingDetails));
input.domain = input.domain || {};
data.forEach(({
price,
href,
address,
}) => {
const exists = !!input.domain[href];
input.domain[href] = input.domain[href] || {};
input.domain[href].address = address;
input.domain[href].price = price;
if (!exists) {
input.domain[href].new = true;
}
});
}
} catch (e) {
console.log(e);
} finally {
await driver.quit();
}
};
const show = (data) => {
const result = Object.entries(data.domain)
.filter(([key, value]) => (typeof value.grade === 'undefined' || value.grade >= 6))
.reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {});
console.log(JSON.stringify(result, null, 4));
};
const main = async () => {
const data = readData();
await updateData(data);
writeData(data);
show(data);
};
main();