Add Zillow and HAR photo extractors
This commit is contained in:
59
skills/web-automation/scripts/zillow-photo-data.js
Normal file
59
skills/web-automation/scripts/zillow-photo-data.js
Normal file
@@ -0,0 +1,59 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user