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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import xlsx from "node-xlsx";
import * as fs from 'fs';
import * as path from 'path';
import * as qbtApi from 'qbittorrent-api-v2';
const ignoreFolderMap = {};
const downloadedFileMap = new Map();
const fileMap = {};
async function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}
function loadIgnoreFolderMap() {
  fs.readFileSync('ignore-folder.txt', 'utf8').replace(/\r/g, '').split('\n').forEach((line) => {
    ignoreFolderMap[line.trim()] = true;
  });
}
function loadFileMap() {
  const ids = fs.readFileSync('8月1和2书单-nofiction.txt', 'utf8').replace(/\r/g, '').split('\n');
  for (const id of ids) {
    const md5 = id.split('.')[0];
    fileMap[md5] = id;
  }
}
 
function loadDownloadedFileMap() {
  const files = fs.readFileSync('8月1和2书单-nofiction-downloaded.txt', 'utf-8').replace(/\r/g, '').split('\n');
  for (const file of files) {
    downloadedFileMap.set(file.split('.')[0], file);
  }
}
 
function copyFiles() {
  const ws = fs.createWriteStream('8月1和2书单-nofiction-downloaded.txt', { flags: 'a', encoding: 'utf8' });
  const fileMap = new Map();
  fs.readFileSync('8月1和2书单-nofiction.txt', "utf8").replace(/\r/g, '').split("\n").forEach(id => fileMap.set(id.split('.')[0], id));
  const dir = 'C:\\Users\\lyg\\Downloads\\down';
  const dst = "D:\\test111";
  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)) {
        try {
          fs.copyFileSync(path.join(dir, folder, fileId), path.join(dst, fileMap.get(fileId)));
          fs.unlinkSync(path.join(dir, folder, fileId));
          ws.write(fileMap.get(fileId) + "\n");
        } catch (e) { }
      }
    });
  });
  ws.close();
}
 
async function main() {
  // loadIgnoreFolderMap();
  loadFileMap();
 
  copyFiles();
  await sleep(2000);
  loadDownloadedFileMap();
  const qbt = await qbtApi.connect('http://localhost:8081', 'admin', 'adminadmin');
 
  const ignoreTors = [];
  const tors = [];
  const torrents = await qbt.torrents();
  for (const torrent of torrents) {
    // if (torrent.state === 'pausedUP') { continue; }
    const contents = await qbt.files(torrent.hash);
    tors.push(torrent.hash);
    const fileIds = [];
    const ignoreFileIds = [];
    for (const content of contents) {
      const contentName = content.name.split("/")[1];
      if (!fileMap[contentName] || ignoreFolderMap[torrent.name] || downloadedFileMap.has(contentName)) {
        ignoreFileIds.push(content.index);
      } else {
        fileIds.push(content.index);
      }
    }
    if (ignoreFileIds.length) {
      let retry = true;
      while (retry) {
        await sleep(100);
        await qbt.setFilePriority(torrent.hash, ignoreFileIds.join('|'), "0").then(() => retry = false).catch(() => { });
      }
    }
    if (fileIds.length) {
      let retry = true;
      while (retry) {
        await sleep(100);
        await qbt.setFilePriority(torrent.hash, fileIds.join('|'), "1").then(() => retry = false).catch(() => { });
      }
    } else {
      ignoreTors.push(torrent.hash);
    }
  }
  if (ignoreTors.length) {
    await qbt.pauseTorrents(ignoreTors.join('|'));
  }
  if (tors.length) {
    await qbt.resumeTorrents(tors.join('|'));
  }
 
  setTimeout(() => {
 
  }, 1000 * 60 ** 60 * 5);
}
 
main();
 
function fixFileName(){
  loadFileMap();
  const dir = '';
  fs.readdirSync(dir).forEach(file => {
    if (fileMap[file]) {
      const filename = fileMap[file];
      fs.renameSync(path.join(dir, file), path.join(dir, filename));
    }
  });
}