From ce8cb9c851fa66c7c2902ceb57e369d3cecf1a28 Mon Sep 17 00:00:00 2001
From: lyg <1543117173@qq.com>
Date: 星期四, 01 八月 2024 01:48:56 +0800
Subject: [PATCH] 复制bt下载的文件,bt任务控制

---
 src/qbt-task-creator.mjs |   45 ++++++++++++++++++++++
 src/libgen-file-copy.mjs |   59 +++++++++++++++++++++++++++++
 package.json             |    1 
 3 files changed, 105 insertions(+), 0 deletions(-)

diff --git a/package.json b/package.json
index 2dc2c80..5aa8177 100644
--- a/package.json
+++ b/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"
diff --git a/src/libgen-file-copy.mjs b/src/libgen-file-copy.mjs
new file mode 100644
index 0000000..9649fb7
--- /dev/null
+++ b/src/libgen-file-copy.mjs
@@ -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();
\ No newline at end of file
diff --git a/src/qbt-task-creator.mjs b/src/qbt-task-creator.mjs
new file mode 100644
index 0000000..4498a0e
--- /dev/null
+++ b/src/qbt-task-creator.mjs
@@ -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();
\ No newline at end of file

--
Gitblit v1.9.1