(Proper) Initial commit
This commit is contained in:
parent
48c0059860
commit
32796e4026
19 changed files with 6094 additions and 97 deletions
41
backend/services/booklet.ts
Normal file
41
backend/services/booklet.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
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;
|
||||
}
|
Reference in a new issue