| | |
| | | } |
| | | } |
| | | |
| | | function addBooks(books) { |
| | | db.run("begin transaction"); |
| | | for (const book of books) { |
| | | db.run("INSERT INTO t_books (Title, Author, Year, Publisher, ISBN) VALUES (?,?,?,?,?)", |
| | | [book.title, book.author, book.year, book.publisher, book.isbn], (err) => { |
| | | if (!err) { |
| | | downloadCnt++; |
| | | } |
| | | }); |
| | | } |
| | | db.run("commit"); |
| | | } |
| | | |
| | | function addBook(book) { |
| | | db.run("INSERT INTO t_books (Title, Author, Year, Publisher, ISBN) VALUES (?,?,?,?,?)", |
| | | [book.title, book.author, book.year, book.publisher, book.isbn], (err) => { |
| | |
| | | return Math.random() * (max - min) + min; |
| | | } |
| | | |
| | | function importFromExcel() { |
| | | initDb(); |
| | | const file = './76w.xlsx'; |
| | | const workSheets = xlsx.parse(file); |
| | | const sheet = workSheets[0]; |
| | | sheet.data.shift(); |
| | | const books = []; |
| | | sheet.data.forEach((row) => { |
| | | const title = row[0]; |
| | | const author = row[1] |
| | | const year = row[2]; |
| | | const publisher = row[3]; |
| | | const isbn = row[4].split(',').sort((a, b) => b.length - a.length)[0]; |
| | | |
| | | books.push({ title, author, year, publisher, isbn }); |
| | | }); |
| | | addBooks(books); |
| | | closeDb(); |
| | | } |
| | | |
| | | // 开始时间 |
| | | const startTime = Date.now(); |
| | | // 图书数量 |
| | |
| | | // chrome驱动 |
| | | /** @type {WebDriver} */ |
| | | let driver; |
| | | function main() { |
| | | function startTask() { |
| | | initLogger(); |
| | | getBook() |
| | | .catch(e => { |
| | |
| | | fs.mkdirSync('D:\\book-list-crawler-cache', { recursive: true }); |
| | | } |
| | | |
| | | function main() { |
| | | // 多进程执行 |
| | | if (isMainThread) { |
| | | console.log(`线程数:${config.threadSize}`); |
| | |
| | | else if (message.type === 'getBookName') { |
| | | const bookName = bookNames.shift(); |
| | | if (bookName) |
| | | console.log(bookName, `剩于:${bookNames.length}`); |
| | | console.log(bookName, `剩于:${bookNames.length},已获取${downloadCnt}本`); |
| | | worker.postMessage({ type: "bookName", data: bookName, threadId: message.threadId }); |
| | | } else if (message.type === 'finish') { |
| | | finishCnt++; |
| | |
| | | process.exit(0); |
| | | }); |
| | | } else { |
| | | main(); |
| | | startTask(); |
| | | } |
| | | } |
| | | |
| | | // importFromExcel(); |
| | | main(); |