diff --git a/nzz.js b/nzz.js index 52d1c5a..730647a 100755 --- a/nzz.js +++ b/nzz.js @@ -48,7 +48,7 @@ function sleep(ms) { * @param {fs.PathLike} outDir Final destination directory. * @param {Date} date Date of the issue. */ -async function moveDownload(tmpDir, outDir, date) { +function moveDownload(tmpDir, outDir, date) { return new Promise(resolve => { let needsOffset = false; @@ -60,20 +60,16 @@ async function moveDownload(tmpDir, outDir, date) { let srcFile = path.join(tmpDir, `Gesamtausgabe_NZZ_-_Neue_Zürcher_Zeitung_${date.isoDate()}.pdf`); + if (needsOffset) { + date = date.addDays(1); + } - fs.watchFile(srcFile, () => { - if (fs.existsSync(srcFile)) { - fs.unwatchFile(srcFile); - if (needsOffset) { - date = date.addDays(1); - } + if (!fs.existsSync(srcFile)) { + setTimeout(() => moveDownload(tmpDir, outDir, date), 5000); + } - let destFile = path.join(outDir, `${date.isoDate()}.pdf`); - fs.copyFileSync(srcFile, destFile); - - resolve(); - } - }); + let destFile = path.join(outDir, `${date.isoDate()}.pdf`); + fs.copyFileSync(srcFile, destFile); }); } @@ -93,6 +89,7 @@ async function enterDate(driver, date) { let endDate = await driver.wait(until.elementLocated(By.css('input.fup-s-date-end')), WAIT_TIMEOUT, TIMEOUT_MSG, RETRY_DELAY); await endDate.clear(); await endDate.sendKeys(dateString + Key.ENTER); + await sleep(500); } /** @@ -156,9 +153,11 @@ async function findIssues(driver, from, to, tmpDir, outDir) { try { let articles = await driver.wait(until.elementsLocated(By.css('.fup-archive-result-item-article-title')), SEARCH_WAIT_TIMEOUT, SEARCH_TIMEOUT_MSG, RETRY_DELAY); await articles[0].click(); + await sleep(500) await download(driver); console.log(`downloading ${from.isoDate()}...`); + await sleep(500) // do this in the background moveDownload(tmpDir, outDir, from); diff --git a/package-lock.json b/package-lock.json index 05a13d2..1d3c30e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nzz-downloader", - "version": "0.1.2", + "version": "0.1.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fdc3d79..b311fa7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nzz-downloader", - "version": "0.1.2", + "version": "0.1.3", "description": "", "main": "nzz.js", "author": "Sebastian Hugentobler",