Make listing discovery unit-aware

This commit is contained in:
2026-03-27 23:11:10 -05:00
parent 301986fb25
commit f8c998d579
9 changed files with 259 additions and 59 deletions

View File

@@ -7,24 +7,11 @@ import {
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/`;
}
import {
buildZillowAddressSlug,
parseAddressIdentity,
scoreAddressCandidate,
} from "./real-estate-address.js";
function normalizeListingUrl(url) {
try {
@@ -53,20 +40,31 @@ async function collectListingUrl(page) {
if (!href) continue;
const absolute = toAbsolute(href);
if (!absolute) continue;
candidates.push(absolute);
const text = (anchor.textContent || "").replace(/\s+/g, " ").trim();
const aria = anchor.getAttribute("aria-label") || "";
const title = anchor.getAttribute("title") || "";
const parentText = (anchor.parentElement?.textContent || "").replace(/\s+/g, " ").trim();
candidates.push({
url: absolute,
text,
aria,
title,
parentText,
});
}
const unique = [];
for (const candidate of candidates) {
if (!unique.includes(candidate)) unique.push(candidate);
if (!unique.some((item) => item.url === candidate.url)) unique.push(candidate);
}
return unique[0] || null;
return unique;
});
}
async function main() {
const address = parseAddress(process.argv[2]);
const searchUrl = buildSearchUrl(address);
const address = String(process.argv[2] || "").trim();
const identity = parseAddressIdentity(address);
const searchUrl = `https://www.zillow.com/homes/${encodeURIComponent(buildZillowAddressSlug(address))}_rb/`;
const { context, page } = await createPageSession({ headless: process.env.HEADLESS !== "false" });
try {
@@ -77,15 +75,33 @@ async function main() {
let listingUrl = null;
if (page.url().includes("/homedetails/")) {
listingUrl = normalizeListingUrl(page.url());
attempts.push("Zillow search URL resolved directly to a property page.");
const directScore = scoreAddressCandidate(
identity,
`${page.url()} ${(await page.title()) || ""}`
);
if (directScore.matched) {
listingUrl = normalizeListingUrl(page.url());
attempts.push("Zillow search URL resolved directly to a matching property page.");
} else {
attempts.push("Zillow redirected to a property page, but it did not match the requested address closely enough.");
}
} else {
const discovered = await collectListingUrl(page);
if (discovered) {
listingUrl = normalizeListingUrl(discovered);
attempts.push("Zillow search results exposed a homedetails link.");
const scored = discovered
.map((candidate) => ({
...candidate,
match: scoreAddressCandidate(
identity,
`${candidate.url} ${candidate.text} ${candidate.aria} ${candidate.title} ${candidate.parentText}`
)
}))
.sort((a, b) => b.match.score - a.match.score);
if (scored[0]?.match.matched) {
listingUrl = normalizeListingUrl(scored[0].url);
attempts.push(`Zillow search results exposed a matching homedetails link with score ${scored[0].match.score}.`);
} else {
attempts.push("Zillow discovery did not expose a homedetails link for this address.");
attempts.push("Zillow discovery did not expose a confident homedetails match for this address.");
}
}