chore(lucida): Retry track if download fails
This commit is contained in:
parent
0e01c21995
commit
03c01bdfa0
1 changed files with 34 additions and 15 deletions
|
@ -25,27 +25,46 @@ export async function lucida(album: URL, baseTimeout: number, context: BrowserCo
|
||||||
console.log(`Downloading ${albumName} (${trackCount} tracks) from ${album.href}...`);
|
console.log(`Downloading ${albumName} (${trackCount} tracks) from ${album.href}...`);
|
||||||
console.log(`Setting timeout to ${baseTimeout * trackCount} ms...`);
|
console.log(`Setting timeout to ${baseTimeout * trackCount} ms...`);
|
||||||
|
|
||||||
try {
|
// Start download
|
||||||
const download: Download = await page.waitForEvent('download', { timeout: baseTimeout * trackCount });
|
await page.getByText('download full album').click();
|
||||||
// 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
|
let download: Download | null = null;
|
||||||
const bookPath: File | null = await booklet(album, context);
|
const timeout: number = baseTimeout * trackCount;
|
||||||
if (bookPath !== null) {
|
const start: number = Date.now();
|
||||||
console.log(`Downloaded booklet ${bookPath.name}`);
|
let retryCount: number = 0;
|
||||||
// TODO Add booklet to ZIP
|
|
||||||
|
// Retry file download if it fails
|
||||||
|
while (!download && (Date.now() - start) < timeout && retryCount < trackCount) {
|
||||||
|
try {
|
||||||
|
download = await page.waitForEvent('download');
|
||||||
|
} catch {
|
||||||
|
const retry: Locator = page.getByText('Retry');
|
||||||
|
if (await retry.count() !== 0 && await retry.isVisible() && await retry.isEnabled()) {
|
||||||
|
await page.getByText('Retry').click();
|
||||||
|
console.log('Retrying download...');
|
||||||
|
retryCount++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await page.close();
|
if (download === null) {
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
await page.close();
|
await page.close();
|
||||||
return 'Download timed out';
|
return 'Download timed out';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function booklet(album: URL, context: BrowserContext): Promise<File | null> {
|
async function booklet(album: URL, context: BrowserContext): Promise<File | null> {
|
||||||
|
|
Reference in a new issue