119 lines
2.9 KiB
JavaScript
119 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import {
|
|
createPageSession,
|
|
dismissCommonOverlays,
|
|
fail,
|
|
gotoListing,
|
|
sleep,
|
|
} from "./real-estate-photo-common.js";
|
|
|
|
function parseAddress(rawAddress) {
|
|
const address = String(rawAddress || "").trim();
|
|
if (!address) {
|
|
fail("Missing address.");
|
|
}
|
|
return address;
|
|
}
|
|
|
|
function buildSearchUrl(address) {
|
|
const slug = address
|
|
.replace(/,/g, "")
|
|
.replace(/#/g, "")
|
|
.trim()
|
|
.split(/\s+/)
|
|
.join("-");
|
|
return `https://www.zillow.com/homes/${encodeURIComponent(slug)}_rb/`;
|
|
}
|
|
|
|
function normalizeListingUrl(url) {
|
|
try {
|
|
const parsed = new URL(url);
|
|
parsed.search = "";
|
|
parsed.hash = "";
|
|
return parsed.toString();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function collectListingUrl(page) {
|
|
return page.evaluate(() => {
|
|
const toAbsolute = (href) => {
|
|
try {
|
|
return new URL(href, location.href).toString();
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const candidates = [];
|
|
for (const anchor of document.querySelectorAll('a[href*="/homedetails/"]')) {
|
|
const href = anchor.getAttribute("href");
|
|
if (!href) continue;
|
|
const absolute = toAbsolute(href);
|
|
if (!absolute) continue;
|
|
candidates.push(absolute);
|
|
}
|
|
|
|
const unique = [];
|
|
for (const candidate of candidates) {
|
|
if (!unique.includes(candidate)) unique.push(candidate);
|
|
}
|
|
return unique[0] || null;
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
const address = parseAddress(process.argv[2]);
|
|
const searchUrl = buildSearchUrl(address);
|
|
const { context, page } = await createPageSession({ headless: process.env.HEADLESS !== "false" });
|
|
|
|
try {
|
|
const attempts = [`Opened Zillow address search URL: ${searchUrl}`];
|
|
await gotoListing(page, searchUrl, 2500);
|
|
await dismissCommonOverlays(page);
|
|
await sleep(1500);
|
|
|
|
let listingUrl = null;
|
|
if (page.url().includes("/homedetails/")) {
|
|
listingUrl = normalizeListingUrl(page.url());
|
|
attempts.push("Zillow search URL resolved directly to a property page.");
|
|
} else {
|
|
const discovered = await collectListingUrl(page);
|
|
if (discovered) {
|
|
listingUrl = normalizeListingUrl(discovered);
|
|
attempts.push("Zillow search results exposed a homedetails link.");
|
|
} else {
|
|
attempts.push("Zillow discovery did not expose a homedetails link for this address.");
|
|
}
|
|
}
|
|
|
|
process.stdout.write(
|
|
`${JSON.stringify(
|
|
{
|
|
source: "zillow",
|
|
address,
|
|
searchUrl,
|
|
finalUrl: page.url(),
|
|
title: await page.title(),
|
|
listingUrl,
|
|
attempts,
|
|
},
|
|
null,
|
|
2
|
|
)}\n`
|
|
);
|
|
await context.close();
|
|
} catch (error) {
|
|
try {
|
|
await context.close();
|
|
} catch {
|
|
// Ignore close errors after the primary failure.
|
|
}
|
|
fail("Zillow discovery failed.", error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
main();
|