88 lines
3.2 KiB
TypeScript
88 lines
3.2 KiB
TypeScript
import {firefox, Page, BrowserContext, Download, Browser, Locator} from 'playwright';
|
|
|
|
export async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise<void> {
|
|
const page: Page = await context.newPage();
|
|
|
|
await page.goto('/');
|
|
|
|
// Fill in the album URL
|
|
await page.fill('input[id="download"]', album.href);
|
|
|
|
// Wait for the XHR request to complete
|
|
await Promise.all([
|
|
page.waitForResponse(res => res.url().includes('/api/load') && res.status() == 200),
|
|
page.click('input[id="go"]'),
|
|
page.waitForLoadState('domcontentloaded')
|
|
]);
|
|
|
|
// Check 'Hide my download from recently downloaded' checkbox
|
|
await page.check('input[id="hide-from-ticker"]');
|
|
|
|
// Parse info
|
|
const albumName: string = (await page.locator('h1[class="svelte-6pt9ji"]').last().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();
|
|
|
|
// 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/lucida/' + download.suggestedFilename());
|
|
|
|
// TODO Check if booklet is available.
|
|
|
|
await page.close();
|
|
}
|
|
|
|
async function booklet(album: URL, context: BrowserContext): Promise<void> {
|
|
const page: Page = await context.newPage();
|
|
|
|
const bookletURL: URL = new URL('http://audiofil.hostronavt.ru/booklet.php?name=' + encodeURIComponent(album.href));
|
|
await page.goto(bookletURL.href);
|
|
|
|
// Find link with goodies
|
|
const link: Locator = page.locator('a').filter({hasText: '/goodies/'});
|
|
const linkCount: number = await link.count();
|
|
if (linkCount > 0) {
|
|
console.log(await link.innerHTML());
|
|
} else {
|
|
console.log('No goodies link found');
|
|
}
|
|
|
|
// TODO Download the booklet.
|
|
|
|
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 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();
|
|
})();
|