fix: make property-assessor safer for whatsapp runs
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
dismissCommonOverlays,
|
||||
fail,
|
||||
gotoListing,
|
||||
runWithOperationTimeout,
|
||||
sleep,
|
||||
} from "./real-estate-photo-common.js";
|
||||
import {
|
||||
@@ -68,63 +69,69 @@ export async function discoverZillowListing(rawAddress) {
|
||||
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" });
|
||||
const closeContext = async () => {
|
||||
await context.close().catch(() => {});
|
||||
};
|
||||
|
||||
try {
|
||||
const attempts = [`Opened Zillow address search URL: ${searchUrl}`];
|
||||
await gotoListing(page, searchUrl, 2500);
|
||||
await dismissCommonOverlays(page);
|
||||
await sleep(1500);
|
||||
return await runWithOperationTimeout(
|
||||
"Zillow discovery",
|
||||
async () => {
|
||||
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/")) {
|
||||
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);
|
||||
const scored = discovered
|
||||
.map((candidate) => ({
|
||||
...candidate,
|
||||
match: scoreAddressCandidate(
|
||||
let listingUrl = null;
|
||||
if (page.url().includes("/homedetails/")) {
|
||||
const directScore = scoreAddressCandidate(
|
||||
identity,
|
||||
`${candidate.url} ${candidate.text} ${candidate.aria} ${candidate.title} ${candidate.parentText}`
|
||||
)
|
||||
}))
|
||||
.sort((a, b) => b.match.score - a.match.score);
|
||||
`${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);
|
||||
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 confident homedetails match for this address.");
|
||||
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 confident homedetails match for this address.");
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
source: "zillow",
|
||||
address,
|
||||
searchUrl,
|
||||
finalUrl: page.url(),
|
||||
title: await page.title(),
|
||||
listingUrl,
|
||||
attempts,
|
||||
};
|
||||
},
|
||||
{
|
||||
onTimeout: closeContext
|
||||
}
|
||||
}
|
||||
|
||||
const result = {
|
||||
source: "zillow",
|
||||
address,
|
||||
searchUrl,
|
||||
finalUrl: page.url(),
|
||||
title: await page.title(),
|
||||
listingUrl,
|
||||
attempts,
|
||||
};
|
||||
await context.close();
|
||||
return result;
|
||||
);
|
||||
} catch (error) {
|
||||
try {
|
||||
await context.close();
|
||||
} catch {
|
||||
// Ignore close errors after the primary failure.
|
||||
}
|
||||
throw new Error(`Zillow discovery failed: ${error instanceof Error ? error.message : String(error)}`);
|
||||
} finally {
|
||||
await closeContext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user