lyg
2024-07-21 ae0679525fac309fc6dece684cde728acc62b71a
src/book-list-download2.mjs
@@ -53,6 +53,19 @@
  }
}
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) => {
@@ -256,6 +269,26 @@
  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();
// 图书数量
@@ -263,7 +296,7 @@
// chrome驱动
/** @type {WebDriver} */
let driver;
function main() {
function startTask() {
  initLogger();
  getBook()
    .catch(e => {
@@ -283,6 +316,7 @@
  fs.mkdirSync('D:\\book-list-crawler-cache', { recursive: true });
}
function main() {
// 多进程执行
if (isMainThread) {
  console.log(`线程数:${config.threadSize}`);
@@ -299,7 +333,7 @@
      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++;
@@ -316,5 +350,9 @@
    process.exit(0);
  });
} else {
  main();
    startTask();
}
}
// importFromExcel();
main();