From 7a115ad2b20b5ee0813f76a0484c44a1f4e71ec0 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Sun, 29 Dec 2024 17:29:36 +0100 Subject: [PATCH] feat(lucida): Download single url --- package.json | 2 +- src/lucida.ts | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index d99ab4b..59f0133 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "src/download.ts", "scripts": { "build": "tsc", - "start": "node dist/download.js", + "start": "node dist/lucida.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { diff --git a/src/lucida.ts b/src/lucida.ts index 735f01b..0676b24 100644 --- a/src/lucida.ts +++ b/src/lucida.ts @@ -1,6 +1,6 @@ -import { firefox, Page, BrowserContext } from 'playwright'; +import {firefox, Page, BrowserContext, Download} from 'playwright'; -async function lucida(album: URL, context: BrowserContext): Promise { +async function lucida(album: URL, timeout: number, context: BrowserContext): Promise { const page: Page = await context.newPage(); await page.goto('/'); @@ -18,19 +18,33 @@ async function lucida(album: URL, context: BrowserContext): Promise { // Check 'Hide my download from recently downloaded' checkbox await page.check('input[id="hide-from-ticker"]'); + // Start download + await page.getByText('download full album').click(); + + page.on('download', download => download.path().then(console.log)); + const downloadPromise: Promise = page.waitForEvent('download', { timeout: timeout }); + const download: Download = await downloadPromise; + + // Save the download to the Downloads folder + // TODO Set path (configurable) + await download.saveAs('/home/tdpeuter/Downloads/' + download.suggestedFilename()); + await page.close(); } (async () => { // Launch a new Firefox browser instance - const browser = await firefox.launch({ headless: false }); // Set headless: true to run without UI + // TODO Allow to pass headless option as argument + const browser = await firefox.launch({ headless: true }); // Set headless: true to run without UI const context = await browser.newContext({ acceptDownloads: true, // Enable download handling baseURL: 'https://lucida.to' }); - const album = new URL('https://www.qobuz.com/us-en/album/liquid-spirit-gregory-porter/0060253743200'); - await lucida(album, context); + const album: URL = new URL('https://www.qobuz.com/us-en/album/you-me-disclosure-eliza-doolittle/q5q4tg0cupgwa'); + // const album: URL = new URL('https://www.qobuz.com/us-en/album/led-zeppelin-iv-hd-remastered-edition-led-zeppelin/0603497898503'); + const timeout: number = 240000; + await lucida(album, timeout, context); // Close the browser await browser.close();