diff --git a/src/lucida.ts b/src/lucida.ts index fc5928d..118714b 100644 --- a/src/lucida.ts +++ b/src/lucida.ts @@ -25,27 +25,30 @@ export async function lucida(album: URL, baseTimeout: number, context: BrowserCo console.log(`Downloading ${albumName} (${trackCount} tracks) from ${album.href}...`); console.log(`Setting timeout to ${baseTimeout * trackCount} ms...`); - // Start download - await page.getByText('download full album').click(); - try { - // Save the download to the Downloads folder const download: Download = await page.waitForEvent('download', { timeout: baseTimeout * trackCount }); + // Start download + await page.getByText('download full album').click(); + // Save the download to the Downloads folder // TODO Set path (configurable) await download.saveAs('/home/tdpeuter/Downloads/lucida/' + download.suggestedFilename()); + + // Check if the album has a booklet + const bookPath: File | null = await booklet(album, context); + if (bookPath !== null) { + console.log(`Downloaded booklet ${bookPath.name}`); + // TODO Add booklet to ZIP + } + + await page.close(); + return true; } catch { await page.close(); return 'Download timed out'; } - - // TODO Check if booklet is available. - - await page.close(); - - return true; } -async function booklet(album: URL, context: BrowserContext): Promise { +async function booklet(album: URL, context: BrowserContext): Promise { const page: Page = await context.newPage(); const bookletURL: URL = new URL('http://audiofil.hostronavt.ru/booklet.php?name=' + encodeURIComponent(album.href)); @@ -58,38 +61,25 @@ async function booklet(album: URL, context: BrowserContext): Promise { console.log(await link.innerHTML()); } else { console.log('No goodies link found'); + return null; } - // TODO Download the booklet. + try { + // Go to the goodies link + await link.dispatchEvent('click'); - await page.close(); + // Download the booklet + const download: Download = await page.waitForEvent('download'); + const filename: string = download.suggestedFilename(); + await download.saveAs('/home/tdpeuter/Downloads/lucida/' + filename); + + console.log(`Downloaded booklet ${filename}`); + + await page.close(); + return new File([await download.path()], filename); + } catch (e) { + console.log('Could not download booklet:', e); + await page.close(); + return null; + } } - -(async () => { - const timeout: number = 240000; - const albums: string[] = [ - 'https://www.qobuz.com/us-en/album/you-me-disclosure-eliza-doolittle/q5q4tg0cupgwa', - 'https://www.qobuz.com/us-en/album/shes-gone-dance-on-disclosure/xrylx7j5794ab', - 'https://www.qobuz.com/us-en/album/tondo-disclosure-eko-roosevelt/tlb5v175fbyxb' - ]; - const withBooklet: string[] = [ - 'https://www.qobuz.com/us-en/album/moon-safari-air/0724384497859' - ]; - - // Launch a new Firefox browser instance - // TODO Allow to pass headless option as argument - const browser: Browser = await firefox.launch({ headless: false }); // Set headless: true to run without UI - const context: BrowserContext = await browser.newContext({ - acceptDownloads: true, // Enable download handling - baseURL: 'https://lucida.to' - }); - - // for (const album of testAlbums) { - // await lucida(new URL(album), timeout, context); - // } - - await booklet(new URL(withBooklet[0]), context); - - // Close the browser - await browser.close(); -})();