lyg
2024-08-01 ce8cb9c851fa66c7c2902ceb57e369d3cecf1a28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as fs from 'fs';
 
const books = [];
const bookMap = {};
 
function main() {
  // 获取所有日志文件
  const logFiles = fs.readdirSync('./logs', { withFileTypes: true });
  // 遍历日志文件
  for (const file of logFiles) {
    // 读取日志文件
    const log = fs.readFileSync(`./logs/${file.name}`, 'utf8');
    // 解析日志文件
    const bookLogs = log.split('开始下载');
    for (const bookLog of bookLogs) {
      const book = {};
      const lines = bookLog.split('\n');
      // 遍历日志行
      for (const line of lines) {
        // 解析日志行
        let reg, group;
        reg = /^: (\d+) .*/g;
        group = reg.exec(line);
        if (group) {
          const bookId = group[1];
          book.bookId = bookId;
          if (!bookMap[bookId]) {
            bookMap[bookId] = book;
            books.push(book);
          }
          continue;
        }
        reg = /.* 打开详情: (.*)$/g;
        group = reg.exec(line);
        if (group) {
          const detailUrl = group[1];
          book.detailUrl = detailUrl;
          continue;
        }
        reg = /.* 下载文件: (.*)$/g;
        group = reg.exec(line);
        if (group) {
          const downloadUrl = group[1];
          book.downloadUrl = downloadUrl;
          continue;
        }
        reg = /.* 下载完成: (\d+) /g;
        group = reg.exec(line);
        if (group) {
          const bookId = group[1];
          if (book.bookId === bookId) {
            book.download = true;
          }
          continue;
        }
        reg = /.*没有搜索结果.*/g;
        if (reg.test(line)) {
          book.notFound = true;
          continue;
        }
      }
 
    }
  }
}
 
try {
  main();
} catch (e) {
  console.error(e);
} finally {
  const failedBooks = books.filter(book => !book.download && book.downloadUrl)
    .map(book => book.bookId + " " + (book.downloadUrl ?? '')).join('\n');
  fs.writeFileSync('./failed-books.txt', failedBooks);
 
  const failedBookUrls = books.filter(book => !book.download && book.downloadUrl)
    .map(book => book.downloadUrl).join('\n');
  fs.writeFileSync('./failed-book-urls.txt', failedBookUrls);
 
  const notFoundBooks = books.filter(book => book.notFound).map(book => book.bookId).join('\n');
  fs.writeFileSync('./not-found-books.txt', notFoundBooks);
 
  const noFileBooks = books.filter(book => !book.download && !book.downloadUrl).map(book => book.bookId).join('\n');
  fs.writeFileSync('./no-file-books.txt', noFileBooks);
 
  const successBooks = books.filter(book => book.download).map(book => book.bookId).join('\n');
  fs.writeFileSync('./success-books.txt', successBooks);
}