feat(lucida): Download multiple urls
This commit is contained in:
		
							parent
							
								
									b0a44f32d0
								
							
						
					
					
						commit
						c85737a57d
					
				
					 1 changed files with 23 additions and 17 deletions
				
			
		|  | @ -1,6 +1,6 @@ | |||
| import {firefox, Page, BrowserContext, Download} from 'playwright'; | ||||
| import {firefox, Page, BrowserContext, Download, Browser} from 'playwright'; | ||||
| 
 | ||||
| async function lucida(album: URL, timeout: number, context: BrowserContext): Promise<void> { | ||||
| async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise<void> { | ||||
|     const page: Page = await context.newPage(); | ||||
| 
 | ||||
|     await page.goto('/'); | ||||
|  | @ -20,38 +20,44 @@ async function lucida(album: URL, timeout: number, context: BrowserContext): Pro | |||
| 
 | ||||
|     // Parse info
 | ||||
|     const albumName: string = (await page.locator('h1[class="svelte-6pt9ji"]').last().innerText()).trim(); | ||||
|     const trackCount: string = (await page.locator('h3[class="svelte-6pt9ji"]').first().innerText()).trim(); | ||||
|     const trackCount: number = parseInt((await page.locator('h3[class="svelte-6pt9ji"]').first().innerText()).trim().split(' ')[0]); | ||||
| 
 | ||||
|     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(); | ||||
| 
 | ||||
|     console.log(`Downloading ${albumName} (${trackCount}) from ${album.href}...`); | ||||
| 
 | ||||
| 
 | ||||
|     page.on('download', download => download.path().then(console.log)); | ||||
|     const downloadPromise: Promise<Download> = page.waitForEvent('download', { timeout: timeout }); | ||||
|     const download: Download = await downloadPromise; | ||||
| 
 | ||||
|     // Save the download to the Downloads folder
 | ||||
|     const download: Download = await page.waitForEvent('download', { timeout: baseTimeout * trackCount }); | ||||
|     // TODO Set path (configurable)
 | ||||
|     await download.saveAs('/home/tdpeuter/Downloads/' + download.suggestedFilename()); | ||||
|     await download.saveAs('/home/tdpeuter/Downloads/lucida/' + download.suggestedFilename()); | ||||
| 
 | ||||
|     await page.close(); | ||||
| } | ||||
| 
 | ||||
| (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 testAlbums: 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 = await firefox.launch({ headless: true }); // Set headless: true to run without UI
 | ||||
|     const context = await browser.newContext({ | ||||
|     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' | ||||
|     }); | ||||
| 
 | ||||
|     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); | ||||
|     for (const album of testAlbums) { | ||||
|         await lucida(new URL(album), timeout, context); | ||||
|     } | ||||
| 
 | ||||
|     // Close the browser
 | ||||
|     await browser.close(); | ||||
|  |  | |||
		Reference in a new issue