Files
stef-openclaw-skills/skills/web-automation/scripts/zillow-photo-data.js

87 lines
2.3 KiB
JavaScript

export function extractZillowStructuredPhotoCandidatesFromNextDataScript(scriptText) {
if (typeof scriptText !== "string" || !scriptText.trim()) {
return [];
}
let nextData;
try {
nextData = JSON.parse(scriptText);
} catch {
return [];
}
const cacheText = nextData?.props?.pageProps?.componentProps?.gdpClientCache;
if (typeof cacheText !== "string" || !cacheText.trim()) {
return [];
}
let cache;
try {
cache = JSON.parse(cacheText);
} catch {
return [];
}
const out = [];
for (const entry of Object.values(cache)) {
const photos = entry?.property?.responsivePhotos;
if (!Array.isArray(photos)) continue;
for (const photo of photos) {
if (typeof photo?.url === "string" && photo.url) {
out.push({ url: photo.url });
continue;
}
const mixedSources = photo?.mixedSources;
if (!mixedSources || typeof mixedSources !== "object") continue;
let best = null;
for (const variants of Object.values(mixedSources)) {
if (!Array.isArray(variants)) continue;
for (const variant of variants) {
if (typeof variant?.url !== "string" || !variant.url) continue;
const width = Number(variant.width || 0);
if (!best || width > best.width) {
best = { url: variant.url, width };
}
}
}
if (best) {
out.push(best);
}
}
}
return out;
}
const DEFAULT_MINIMUM_TRUSTED_STRUCTURED_PHOTO_COUNT = 12;
export function shouldUseStructuredZillowPhotos(candidates, options = {}) {
const count = Array.isArray(candidates) ? candidates.length : 0;
const normalizedOptions =
typeof options === "number"
? { expectedPhotoCount: options }
: options && typeof options === "object"
? options
: {};
const expectedPhotoCount = Number(normalizedOptions.expectedPhotoCount || 0);
const fallbackPhotoCount = Number(normalizedOptions.fallbackPhotoCount || 0);
const minimumTrustCount = Number(
normalizedOptions.minimumTrustCount || DEFAULT_MINIMUM_TRUSTED_STRUCTURED_PHOTO_COUNT
);
if (Number.isFinite(expectedPhotoCount) && expectedPhotoCount > 0) {
return count >= expectedPhotoCount;
}
if (Number.isFinite(fallbackPhotoCount) && fallbackPhotoCount > 0) {
return count >= fallbackPhotoCount;
}
return count >= minimumTrustCount;
}