fix: make property-assessor safer for whatsapp runs
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
gotoListing,
|
||||
normalizeImageCandidates,
|
||||
parseTarget,
|
||||
runWithOperationTimeout,
|
||||
scrollUntilSettled,
|
||||
sleep,
|
||||
waitForPhotoExperience,
|
||||
@@ -34,51 +35,56 @@ async function getAnnouncedPhotoCount(page) {
|
||||
export async function extractHarPhotos(rawUrl) {
|
||||
const requestedUrl = parseTarget(rawUrl);
|
||||
const { context, page } = await createPageSession({ headless: process.env.HEADLESS !== "false" });
|
||||
const closeContext = async () => {
|
||||
await context.close().catch(() => {});
|
||||
};
|
||||
|
||||
try {
|
||||
await gotoListing(page, requestedUrl);
|
||||
await dismissCommonOverlays(page);
|
||||
return await runWithOperationTimeout(
|
||||
"HAR photo extraction",
|
||||
async () => {
|
||||
await gotoListing(page, requestedUrl);
|
||||
await dismissCommonOverlays(page);
|
||||
|
||||
const expectedPhotoCount = await getAnnouncedPhotoCount(page);
|
||||
const beforeUrl = page.url();
|
||||
const clickedLabel = await clickPhotoEntryPoint(page, HAR_LABELS);
|
||||
await waitForPhotoExperience(page, beforeUrl);
|
||||
await scrollUntilSettled(page);
|
||||
await sleep(1200);
|
||||
const expectedPhotoCount = await getAnnouncedPhotoCount(page);
|
||||
const beforeUrl = page.url();
|
||||
const clickedLabel = await clickPhotoEntryPoint(page, HAR_LABELS);
|
||||
await waitForPhotoExperience(page, beforeUrl);
|
||||
await scrollUntilSettled(page);
|
||||
await sleep(1200);
|
||||
|
||||
const candidates = await collectRenderedImageCandidates(page);
|
||||
const photos = normalizeImageCandidates(candidates, {
|
||||
hostIncludes: ["pics.harstatic.com", "photos.harstatic.com"],
|
||||
minWidth: 240,
|
||||
minHeight: 180,
|
||||
});
|
||||
const candidates = await collectRenderedImageCandidates(page);
|
||||
const photos = normalizeImageCandidates(candidates, {
|
||||
hostIncludes: ["pics.harstatic.com", "photos.harstatic.com"],
|
||||
minWidth: 240,
|
||||
minHeight: 180,
|
||||
});
|
||||
|
||||
if (!photos.length) {
|
||||
fail("HAR photo extraction failed.", "No large image URLs were found after opening the HAR all-photos view.");
|
||||
}
|
||||
if (!photos.length) {
|
||||
fail("HAR photo extraction failed.", "No large image URLs were found after opening the HAR all-photos view.");
|
||||
}
|
||||
|
||||
const result = {
|
||||
source: "har",
|
||||
requestedUrl,
|
||||
finalUrl: page.url(),
|
||||
title: await page.title(),
|
||||
clickedLabel,
|
||||
expectedPhotoCount,
|
||||
complete: expectedPhotoCount ? photos.length >= expectedPhotoCount : true,
|
||||
photoCount: photos.length,
|
||||
imageUrls: photos.map((photo) => photo.url),
|
||||
notes: ["Opened HAR all-photos flow and extracted large rendered image URLs from the photo page."],
|
||||
};
|
||||
|
||||
await context.close();
|
||||
return result;
|
||||
return {
|
||||
source: "har",
|
||||
requestedUrl,
|
||||
finalUrl: page.url(),
|
||||
title: await page.title(),
|
||||
clickedLabel,
|
||||
expectedPhotoCount,
|
||||
complete: expectedPhotoCount ? photos.length >= expectedPhotoCount : true,
|
||||
photoCount: photos.length,
|
||||
imageUrls: photos.map((photo) => photo.url),
|
||||
notes: ["Opened HAR all-photos flow and extracted large rendered image URLs from the photo page."],
|
||||
};
|
||||
},
|
||||
{
|
||||
onTimeout: closeContext
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
try {
|
||||
await context.close();
|
||||
} catch {
|
||||
// Ignore close errors after the primary failure.
|
||||
}
|
||||
throw new Error(error instanceof Error ? error.message : String(error));
|
||||
} finally {
|
||||
await closeContext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user