lyg
2024-08-01 ce8cb9c851fa66c7c2902ceb57e369d3cecf1a28
复制bt下载的文件,bt任务控制
1个文件已修改
2个文件已添加
105 ■■■■■ 已修改文件
package.json 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/libgen-file-copy.mjs 59 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/qbt-task-creator.mjs 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
package.json
@@ -22,6 +22,7 @@
    "jssoup": "^0.0.15",
    "node-xlsx": "^0.24.0",
    "pdf-lib": "^1.17.1",
    "qbittorrent-api-v2": "^1.2.2",
    "selenium-webdriver": "^4.21.0",
    "sqlite3": "^5.1.7",
    "wordlist-js": "^2.0.0"
src/libgen-file-copy.mjs
New file
@@ -0,0 +1,59 @@
import xlsx from "node-xlsx";
import * as fs from 'fs';
const srcPath = "D:/books";
const dstPath = "E:/books";
function getBookFolders() {
  const folders = fs.readFileSync('已下载图书文件夹名称.txt', 'utf8').split('\n');
  const map = {};
  for (const folder of folders) {
    map[folder] = `${srcPath}/${folder}`;
  }
  return map;
}
function getBooksFromExcel() {
  const result = [];
  const excelFiles = [
    "清单第二批0723-已撞库.xlsx.result.xlsx",
    "【反馈客户】7月批次书单 - 已撞库.xlsx.result.xlsx"
  ];
  for (const excelFile of excelFiles) {
    const workSheets = xlsx.parse(excelFile);
    for (const sheet of workSheets) {
      const books = sheet.data;
      books.shift();
      for (const row of books) {
        const [id, title, author, isbn, libgenId, _, __, file] = row;
        result.push({ id, isbn, title, author, libgenId, file });
      }
    }
  }
  return result;
}
function main() {
  const bookFolders = getBookFolders();
  const books = getBooksFromExcel();
  let cnt = 0;
  const downloadedBooks = ['ID', 'Title', 'Author', 'ISBN', 'File'];
  for (const book of books) {
    const { id, isbn, title, author, libgenId, file } = book;
    const folderName = (Math.floor(parseInt(libgenId) / 1000) * 1000).toFixed(0);
    const folder = bookFolders[folderName];
    if (!folder) { continue; }
    const filePath = `${folder}/${file}`;
    try {
      const ext = file.split('.')[1];
      fs.cpSync(filePath, `${dstPath}/${libgenId}.${ext}`);
      downloadedBooks.push([id, title, author, isbn, file]);
      cnt++;
    } catch (e) {
      console.error(e);
    }
  }
  console.log(cnt);
}
main();
src/qbt-task-creator.mjs
New file
@@ -0,0 +1,45 @@
import xlsx from "node-xlsx";
import * as fs from 'fs';
import * as qbtApi from 'qbittorrent-api-v2';
const fileMap = {};
function loadFileMap() {
  const workSheets = xlsx.parse("libgen-撞库图书.xlsx");
  const sheet = workSheets[0];
  for (const row of sheet.data) {
    const [id, file] = row;
    const folderName = (Math.floor(parseInt(id) / 1000) * 1000).toFixed(0);
    if (!fileMap[folderName]) {
      fileMap[folderName] = {};
    }
    fileMap[folderName][file] = true;
  }
}
async function main() {
  loadFileMap();
  const qbt = await qbtApi.connect('http://localhost:8081', 'admin', 'adminadmin');
  const torrents = await qbt.torrents();
  for (const torrent of torrents) {
    if (torrent.state === 'pausedUP') { continue; }
    const contents = await qbt.files(torrent.hash);
    const torName = torrent.name;
    const files = fileMap[torName];
    if (!files) {
      continue;
    }
    const fileIds = [];
    for (const content of contents) {
      const contentName = content.name.split("/")[1];
      if (!files[contentName]) {
        fileIds.push(content.index);
      }
    }
    if (fileIds.length) {
      await qbt.setFilePriority(torrent.hash, fileIds.join('|'), "0");
    }
  }
}
main();