-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapartments.js
281 lines (265 loc) · 9.09 KB
/
apartments.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Apartments.com listings
(async () => {
const website = window.location.href;
const isApartments = website.includes("apartments.com");
const isZillow = website.includes("zillow.com");
const isForRent = website.includes("forrent.com");
const isRent =
website.includes("rent.com") || website.includes("apartmentguide.com");
if (!(isApartments || isZillow || isForRent || isRent)) {
console.error(
`The website "${website}" is not supported. Please open an issue on GitHub.`
);
}
function onPage(pageNumber) {
if (isApartments) {
const pageLink = document.querySelector(`a[data-page="${pageNumber}"]`);
if (pageLink) {
return pageLink.classList?.contains("active") ?? false;
} else {
// No pagination
return pageNumber === 1;
}
} else if (isZillow) {
return (
document.querySelector(
`a[Title="Page ${pageNumber}, current page"]`
) !== null
);
} else if (isForRent) {
return (
document.querySelector('a[aria-current="true"]')?.textContent ===
`page ${pageNumber} `
);
} else if (isRent) {
// Doesn't have page buttons
return pageNumber === 1;
}
}
function getButtonForPage(pageNumber) {
if (isApartments) {
return (
document.querySelector(`a[data-page="${pageNumber}"]`) ??
document.querySelector("a.next ")
);
} else if (isZillow) {
const button =
document.querySelector(`a[Title="Page ${pageNumber}"]`) ??
document.querySelector('a[Title="Next page"]');
if (button?.hasAttribute("disabled")) {
return null;
} else {
return button;
}
} else if (isForRent) {
const button =
Array.from(document.querySelectorAll('a[aria-current="false"]')).find(
(pageButton) => pageButton?.textContent === `page ${pageNumber} `
) ?? document.querySelector('a[aria-label="next page"]');
if (
button.hasAttribute("aria-disabled") &&
button.getAttribute("aria-disabled") === "true"
) {
return null;
} else {
return button;
}
} else if (isRent) {
return document.querySelector('a[data-tid="pagination-next"]');
}
}
function isLoading() {
if (isApartments) {
return (
window.getComputedStyle(
document.querySelector("div#placardLoadingOverlay")
).display !== "none"
);
} else if (isZillow) {
return document.querySelector("div.list-loading-message-cover") !== null;
} else if (isForRent) {
return document.querySelector("div.loading-container") !== null;
} else if (isRent) {
return (
document
.querySelector('div[data-tid="property-count"]')
?.textContent?.includes("Loading Properties") ?? false
);
}
}
function getPageHash() {
if (isApartments) {
return document.querySelector(
"li.mortar-wrapper article.placard span.title"
)?.textContent;
} else if (isZillow) {
return document.querySelector("article.list-card address")?.textContent;
} else if (isForRent) {
return document.querySelector(
"article.listing-card h2.property-title > a"
)?.textContent;
} else if (isRent) {
return document.querySelector(
'div.listing-card a[data-tid="property-title"]'
)?.textContent;
}
}
async function getListings() {
if (isApartments) {
return Array.from(
document.querySelectorAll("li.mortar-wrapper article.placard")
).map((propertyInfo) => {
const titleElement =
propertyInfo.querySelector("span.title") ??
propertyInfo.querySelector("div.property-title");
if (titleElement == null) {
console.log(propertyInfo);
}
const name = titleElement.textContent;
const addressElements = Array.from(
propertyInfo.querySelectorAll(".property-address")
);
let address = addressElements
.map((x) => x.getAttribute("title"))
.join(" ");
// Some listings have part of the address in the title
const regex =
/^(?<city>[A-Z][a-z]+), (?<state>[A-Z]{2}) (?<zipcode>\d{5}(?:-\d{4})?)$/;
if (regex.test(address)) {
address = name + " " + address;
}
const url = propertyInfo.querySelector("a.property-link").href;
// some listings do not show prices
let priceRange =
(
propertyInfo.querySelector(".property-pricing") ??
propertyInfo.querySelector(".property-rents") ??
propertyInfo.querySelector(".price-range")
)?.textContent ?? "N/A";
return {
name,
address,
url,
priceRange,
};
});
} else if (isZillow) {
// Zillow only loads address info once you scroll into view of a listing
do {
const listings = Array.from(
document.querySelectorAll("article.list-card")
);
for (let i = 0; i < listings.length; i++) {
listings[i].scrollIntoView();
await new Promise((r) => setTimeout(r, 50));
}
} while (
Array.from(document.querySelectorAll("article.list-card"))
.map((listing) => listing.querySelector("address"))
.some((address) => address === null)
);
return Array.from(document.querySelectorAll("article.list-card")).map(
(listing) => {
const addressContent = listing
.querySelector("address")
?.textContent?.split("|");
let name = addressContent[0];
const address = addressContent[addressContent.length - 1];
if (name === address) {
name =
listing
.querySelector("li.list-card-statusText")
?.textContent?.replace("- ", "") ?? name;
}
const url = listing.querySelector("a.list-card-link").href;
const priceRange =
listing.querySelector("div.list-card-price")?.textContent ?? "N/A";
return {
name,
address,
url,
priceRange,
};
}
);
} else if (isForRent) {
const titleSelector = "h2.property-title > a";
return Array.from(document.querySelectorAll("article.listing-card")).map(
(listing) => ({
name: listing.querySelector(titleSelector)?.textContent,
address: listing.querySelector("address")?.textContent,
url: listing.querySelector(titleSelector)?.href,
priceRange:
listing
.querySelector("p.copy-row span.border-left")
?.textContent?.replace("–", "-") ?? "N/A",
})
);
} else if (isRent) {
const titleSelector = 'a[data-tid="property-title"]';
return Array.from(document.querySelectorAll("div.listing-card")).map(
(listing) => ({
name: listing.querySelector(titleSelector)?.textContent,
address:
listing.querySelector('a[data-tid="listing-info-address"]')
?.textContent ??
(listing.querySelector(titleSelector)?.textContent ?? "") +
(listing.querySelector('a[data-id="listing-info-address"]') ??
""),
url: listing.querySelector(titleSelector)?.href,
priceRange:
listing
.querySelector('span[data-tid="price"]')
?.textContent?.replace("–", "-") ?? "N/A",
})
);
}
}
console.log("Please wait while I click through the pages...");
let tsv = "name\taddress\tURL\tprice-range\n";
let prevPageHash = undefined;
let pageNumber = 1;
for (;;) {
// load next page or finish search
if (!onPage(pageNumber)) {
const button = getButtonForPage(pageNumber);
if (button !== null) {
button.click();
} else {
console.log("Done!");
break;
}
}
let pageHash = undefined;
do {
console.log(
`Waiting for page ${pageNumber} to load (debug info: ${isLoading()} | ${prevPageHash} === ${pageHash})`
);
await new Promise((r) => setTimeout(r, 1000));
pageHash = getPageHash();
} while (isLoading() || prevPageHash === pageHash);
prevPageHash = pageHash;
console.log(`Processing ${pageNumber}`);
pageNumber++;
// add listings on page to TSV
tsv += (await getListings())
.map(
({ name, address, url, priceRange }) =>
`${name.trim()}\t${address.trim()}\t${url.trim()}\t${priceRange.trim()}\n`
)
.reduce((acc, curr) => acc + curr, "");
}
const tsvDataUri = "data:text/tsv;base64," + btoa(unescape(encodeURIComponent(tsv)));
const link = document.createElement("a");
link.href = tsvDataUri;
link.download = "apartments.tsv";
link.click();
console.log(
"You should now see a pop-up to download a TSV file with the listings from your search"
);
})().catch((e) => {
console.log(
`There was an error while pulling listings: ${e.message} \nThere may be an issue with your browser or this site has changed. Please create an issue on GitHub.`
);
console.log(e.stack);
});