69 lines
1.7 KiB
JavaScript
69 lines
1.7 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;
|
|
}
|
|
|
|
export function shouldUseStructuredZillowPhotos(candidates, expectedPhotoCount) {
|
|
const count = Array.isArray(candidates) ? candidates.length : 0;
|
|
if (!Number.isFinite(expectedPhotoCount) || expectedPhotoCount <= 0) {
|
|
return false;
|
|
}
|
|
|
|
return count >= expectedPhotoCount;
|
|
}
|