liyugang
3 天以前 8c24730e9a52dc2c8933e8d41d2f9651de48a231
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
89
90
91
92
93
94
95
96
97
98
import * as fs from "fs";
import * as path from "path";
import xlsx from "node-xlsx";
 
const srcBookMap = new Map();
const dstBooks = [];
 
function loadSrcBookMap() {
  const workSheets = xlsx.parse("libgen-fiction.xlsx");
  const sheet = workSheets[0];
  const books = sheet.data;
  for (const row of books) {
    const [id, isbn, title, author, md5, ext] = row;
    if (isbn.includes(',')) {
      const isbns = isbn.split(',');
      for (const _isbn of isbns) {
        srcBookMap.set(_isbn, { id, isbn: _isbn, title, author, md5, ext, file: `${md5}.${ext}` });
      }
    } else {
      srcBookMap.set(isbn, { id, isbn, title, author, md5, ext, file: `${md5}.${ext}` });
    }
  }
}
 
function loadDstBooks() {
  const file = "C:\\Users\\lyg\\Downloads\\【反馈客户】7月批次书单 - 已撞库.xlsx";
  const workSheets = xlsx.parse(file);
  const sheet = workSheets[0];
  const books = sheet.data;
  for (const row of books) {
    let [id, isbn, title, _, author,] = row;
    if (!isbn) { continue; }
    isbn = `${isbn}`.trim();
    if (isbn.includes(",")) {
      const isbns = isbn.split(',');
      for (const _isbn of isbn) {
        dstBooks.push({ id, isbn: _isbn, title, author });
      }
    } else {
      dstBooks.push({ id, isbn, title, author });
    }
  }
}
 
async function main() {
  loadSrcBookMap();
  loadDstBooks();
  let cnt = 0;
  const ids = [];
  for (const dstBook of dstBooks) {
    const srcBook = srcBookMap.get(dstBook.isbn);
    if (srcBook) {
      cnt++;
      ids.push(srcBook.id);
    }
  }
  console.log(cnt);
  fs.writeFileSync("ids1.txt", ids.join("\n"), "utf8");
}
 
// main();
 
function copyFiles() {
  const fileMap = {};
  fs.readFileSync("zlib-files-7.txt", "utf8").split("\n").forEach(file => {
    const [id, ext] = file.split('.');
    fileMap[id] = file;
  });
  const dir = 'D:\\zlib-books\\book';
  const dst = "";
  const failedFiles = [];
  fs.readdirSync(dir).forEach(folder => {
    fs.readdirSync(path.join(dir, folder)).forEach(fileId => {
      const fileName = fileMap[fileId];
      if (!fileName) { return; }
      try {
        fs.copyFileSync(path.join(dir, folder, fileId), path.join(dst, fileName));
      } catch (e) {
        failedFiles.push(path.join(folder, fileId));
      }
    });
  });
  fs.writeFileSync('failed-files.txt', failedFiles.join('\n'), 'utf8');
}
 
function copyFiles() {
  const fileMap = new Map();
  fs.readFileSync("ids1.txt", "utf8").replace(/\r/g, '').split("\n").forEach(id => fileMap.set(id, true));
  const dir = 'D:\\zlib-books\\book';
  const dst = "F:\\books";
  fs.readdirSync(dir).filter(folder => fs.statSync(path.join(dir, folder)).isDirectory()).forEach(folder => {
    fs.readdirSync(path.join(dir, folder)).forEach(fileId => {
      if (fileMap.has(fileId)) {
        fs.copyFileSync(path.join(dir, folder, fileId), path.join(dst, fileId));
      }
    });
  });
}