liyugang
3 天以前 8c24730e9a52dc2c8933e8d41d2f9651de48a231
src/parse-isbn-log.mjs
@@ -3,50 +3,74 @@
const books = [];
function main() {
  // 获取所有日志文件
  const logFiles = fs.readdirSync('./book-isbn-logs', { withFileTypes: true });
  // 遍历日志文件
  for (const file of logFiles) {
    // 读取日志文件
    const log = fs.readFileSync(`./book-isbn-logs/${file.name}`, 'utf8');
    // 解析日志文件
    const bookLogs = log.split('开始下载');
    for (const bookLog of bookLogs) {
      const lines = bookLog.split('\n');
      // 遍历日志行
      for (const line of lines) {
        // 解析日志行
        let reg, group;
        reg = /.* ISBN: (.*)$/g;
        group = reg.exec(line);
        if (group) {
          const book = {};
          const text = group[1];
          const obj = JSON.parse(text);
          Object.assign(book, obj);
          books.push(book);
          continue;
        }
      }
function parseBooks() {
  try {
    // 获取所有日志文件
    const logFiles = fs.readdirSync('./book-isbn-logs', { withFileTypes: true });
    // 遍历日志文件
    for (const file of logFiles) {
      // 读取日志文件
      const log = fs.readFileSync(`./book-isbn-logs/${file.name}`, 'utf8');
      // 解析日志文件
      const bookLogs = log.split('开始下载');
      for (const bookLog of bookLogs) {
        const lines = bookLog.split('\n');
        // 遍历日志行
        for (const line of lines) {
          // 解析日志行
          let reg, group;
          reg = /.* ISBN: (.*)$/g;
          group = reg.exec(line);
          if (group) {
            const book = {};
            const text = group[1];
            const obj = JSON.parse(text);
            Object.assign(book, obj);
            books.push(book);
            continue;
          }
        }
      }
    }
  } catch (e) {
    console.error(e);
  } finally {
    const EXCEL_FILE = "fiction-noisbn.xlsx";
    const workSheets = xlsx.parse(EXCEL_FILE);
    const sheet = workSheets[0];
    const result = [books[0]];
    for (const book of books) {
      const row = sheet.data.find(row => row[0] == book.id);
      row[5] = book.isbn;
      result.push(row);
    }
    const buffer = xlsx.build([{ name: "Sheet1", data: result }]);
    fs.writeFileSync("archive-books.xlsx", buffer, (err) => { });
    console.log("保存完成: ", EXCEL_FILE);
  }
}
try {
  main();
} catch (e) {
  console.error(e);
} finally {
  const EXCEL_FILE = "fiction-noisbn.xlsx";
  const workSheets = xlsx.parse(EXCEL_FILE);
  const sheet = workSheets[0];
  for (const book of books) {
    const row=sheet.data.find(row => row[0] == book.id);
    row[5] = book.isbn;
function parseArchiveBooks() {
  const logText = fs.readFileSync(`./book-list-logs/book-list-logs.log`, 'utf8');
  const logs = logText.split('\n');
  for (const log of logs) {
    if (log.includes('|')) {
      const [id, title, author] = log.split(" | ");
      const book = { id, title, author };
      books.push(book);
    }
  }
  const buffer = xlsx.build([sheet]);
  fs.writeFileSync(EXCEL_FILE, buffer, (err) => { });
  console.log("保存完成: ", EXCEL_FILE);
}
  const result = [["id", "title", "author"]];
  for (const book of books) {
    result.push([book.id.substr(19), book.title, book.author]);
  }
  const buffer = xlsx.build([{ name: "Sheet1", data: result }]);
  fs.writeFileSync("archive-books.xlsx", buffer, (err) => { });
  console.log("保存完成:" + books.length);
}
parseArchiveBooks();
// parseBooks();