fix(booklet): Close pages when no goodie

This commit is contained in:
Tibo De Peuter 2025-01-01 19:22:39 +01:00
parent fe523c91fe
commit 9bcba4caaa
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2

View file

@ -21,7 +21,7 @@ export async function lucida(album: URL, baseTimeout: number, context: BrowserCo
const page: Page = await openAlbum(album, context); const page: Page = await openAlbum(album, context);
// Check 'Hide my download from recently downloaded' checkbox // Check 'Hide my download from recently downloaded' checkbox
await page.locator('input[id="hide-from-recent"]').first().setChecked(true); await page.locator('input[id="hide-from-ticker"]').first().setChecked(true);
// Parse info // Parse info
const albumName: string = (await page.locator('h1[class="svelte-6pt9ji"]').last().innerText()).trim(); const albumName: string = (await page.locator('h1[class="svelte-6pt9ji"]').last().innerText()).trim();
@ -81,13 +81,16 @@ async function booklet(album: URL, context: BrowserContext): Promise<File | null
// Find link with goodies // Find link with goodies
const link: Locator = page.locator('a').filter({hasText: '/goodies/'}); const link: Locator = page.locator('a').filter({hasText: '/goodies/'});
const linkCount: number = await link.count(); const linkCount: number = await link.count();
if (linkCount > 0) {
console.log(await link.innerHTML()); if (0 <= linkCount) {
} else {
console.log('No goodies link found'); console.log('No goodies link found');
await page.close();
return null; return null;
} }
console.log(`Found goodies: ${await link.innerHTML()}`)
let result: File | null = null;
try { try {
// Go to the goodies link // Go to the goodies link
await link.dispatchEvent('click'); await link.dispatchEvent('click');
@ -97,13 +100,13 @@ async function booklet(album: URL, context: BrowserContext): Promise<File | null
const filename: string = download.suggestedFilename(); const filename: string = download.suggestedFilename();
await download.saveAs('/home/tdpeuter/Downloads/lucida/' + filename); await download.saveAs('/home/tdpeuter/Downloads/lucida/' + filename);
console.log(`Downloaded booklet ${filename}`); result = new File([await download.path()], filename);
await page.close(); console.log(`Downloaded booklet ${filename}`);
return new File([await download.path()], filename);
} catch (e) { } catch (e) {
console.log('Could not download booklet:', e); console.log('Could not download booklet:', e);
await page.close();
return null;
} }
await page.close();
return result;
} }