This repository has been archived on 2025-08-08. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
lucida-queue/backend/services/booklet.ts

41 lines
1.4 KiB
TypeScript

import {BrowserContext, Download, firefox, Locator} from "playwright";
import path from "node:path";
export async function fetchBooklet(url: string, downloadPath: string, context: BrowserContext | null): Promise<string | null> {
const browser = await firefox.launch({
headless: true
});
context = await browser.newContext({
acceptDownloads: true
});
const page = await context.newPage();
const bookletPage: string = 'http://audiofil.hostronavt.ru/booklet.php?name=' + encodeURIComponent(url);
await page.goto(bookletPage);
// Find link with goodies
const link: Locator = page.locator('a').filter({hasText: '/goodies/'});
const linkCount: number = await link.count();
if (0 <= linkCount) {
await page.close();
return null;
}
console.log(`Booklet: Found goodies: ${await link.innerHTML()}`);
let filename: string | null = null;
try {
const downloadPromise: Promise<Download> = page.waitForEvent('download');
await link.dispatchEvent('click');
const download: Download = await downloadPromise;
filename = download.suggestedFilename();
await download.saveAs(path.join(downloadPath, filename));
} catch (err) {
console.log('Booklet: Could not download booklet:', err instanceof Error ? err.message : 'Unknown error');
filename = null;
}
await page.close();
return filename;
}