| | |
| | | import axios from "axios"; |
| | | import * as fs from "fs"; |
| | | import path from "path"; |
| | | import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'; |
| | | import { Worker, isMainThread, parentPort, workerData, threadId } from 'worker_threads'; |
| | | import { HttpsProxyAgent } from "https-proxy-agent"; |
| | | import { resolve } from "path"; |
| | | |
| | |
| | | if (!fs.existsSync('./logs')) { |
| | | fs.mkdirSync('./logs', { recursive: true }); |
| | | } |
| | | logFile = fs.createWriteStream(`./logs/logs-${config.startRow}-${config.endRow}.log`, { flags: 'a', encoding: 'utf8' }); |
| | | logFile = fs.createWriteStream(`./logs/logs-${config.startRow}-${config.endRow}-thread${threadId}.log`, { flags: 'a', encoding: 'utf8' }); |
| | | console.log = function (...text) { |
| | | text = `${new Date().toLocaleString()} ${text.join(' ') ?? ''}`; |
| | | _log(text); |
| | |
| | | return alreadyDownloadedBooks.includes(id); |
| | | } |
| | | |
| | | function nextBook() { |
| | | return new Promise(resolve => { |
| | | const cb = (message) => { |
| | | if (message.type === 'book') { |
| | | resolve(message.data); |
| | | parentPort.removeListener('message', cb); |
| | | } |
| | | }; |
| | | parentPort.on('message', cb); |
| | | parentPort.postMessage({ type: 'get-book', threadId }); |
| | | |
| | | }); |
| | | } |
| | | |
| | | async function downloadBooks(books) { |
| | | driver = await createDriver(); |
| | | for (const book of books) { |
| | | |
| | | for (; ;) { |
| | | const book = await nextBook(); |
| | | if (!book) { |
| | | break; |
| | | } |
| | | books.push(book); |
| | | if (config.endOfTime && Date.now() - startTime > 1000 * 60 * config.endOfTime) { |
| | | // 定时退出 |
| | | break; |
| | |
| | | |
| | | function main() { |
| | | initLogger(); |
| | | const books = getBooksFromExcel(config.startRow, config.endRow); |
| | | const books = []; |
| | | downloadBooks(books) |
| | | .then(() => { |
| | | console.log(`全部完成,共下载${bookCount}本,成功下载${successCount}本,跳过${skipCount}本,失败${bookCount - skipCount - successCount}本,耗时: ${msFormat(Date.now() - startTime)}。`); |
| | |
| | | if (isMainThread) { |
| | | initLogger(); |
| | | const alreadyDownloadedBooks = getAlreadyDownloadedBooks(); |
| | | console.log(`线程数:${config.threadSize}, 开始行:${config.startRow}, 结束行:${config.endRow}`); |
| | | let startRow = config.startRow; |
| | | let endRow = config.endRow; |
| | | const { startRow, endRow, threadSize } = config; |
| | | console.log(`线程数:${threadSize}, 开始行:${startRow}, 结束行:${endRow}`); |
| | | let finishCnt = 0; |
| | | const finishBooks = []; |
| | | const threadSize = config.threadSize; |
| | | const thBookSize = (endRow - startRow) / threadSize |
| | | const thBookSize = (endRow - startRow) / threadSize; |
| | | const books = getBooksFromExcel(startRow, endRow); |
| | | |
| | | for (let sr = startRow; sr < endRow; sr += thBookSize) { |
| | | let er = sr + thBookSize; |
| | | if (er > endRow) { |
| | |
| | | if (message.type === 'books') { |
| | | finishBooks.push(...message.data); |
| | | finishCnt++; |
| | | if (finishCnt >= config.threadSize) { |
| | | if (finishCnt >= threadSize) { |
| | | saveBooks(finishBooks); |
| | | } |
| | | } else if (message.type === 'get-book') { |
| | | worker.postMessage({ type: "book", data: books.shift() }); |
| | | } |
| | | }); |
| | | } |
| | | } else { |
| | | config.startRow = workerData.startRow; |
| | | config.endRow = workerData.endRow; |
| | | alreadyDownloadedBooks = workerData.alreadyDownloadedBooks; |
| | | main(); |
| | | } |