119 lines
4.0 KiB
JavaScript
119 lines
4.0 KiB
JavaScript
import axios from "axios"
|
||
import fs from "fs"
|
||
import FormData from "form-data"
|
||
|
||
|
||
|
||
class BtDonwold {
|
||
constructor({ ip, port }) {
|
||
this.ip = ip
|
||
this.port = port
|
||
this.sessionId = ""
|
||
axios.defaults.baseURL = `http://${this.ip}:${this.port}`
|
||
this.baseURL = `http://${this.ip}:${this.port}`
|
||
// this.getTokne()
|
||
}
|
||
/**
|
||
* 失效函数
|
||
*/
|
||
async getTokne() {
|
||
axios.post(`/transmission/rpc`, { "method": "torrent-get", "arguments": { "fields": ["id", "error", "errorString", "eta", "isFinished", "isStalled", "leftUntilDone", "metadataPercentComplete", "peersConnected", "peersGettingFromUs", "peersSendingToUs", "percentDone", "queuePosition", "rateDownload", "rateUpload", "recheckProgress", "seedRatioMode", "seedRatioLimit", "sizeWhenDone", "status", "trackers", "downloadDir", "uploadedEver", "uploadRatio", "webseedsSendingToUs"], "ids": "recently-active" } }, {
|
||
headers: {
|
||
'X-Transmission-Session-Id': this.sessionId
|
||
}
|
||
}).then((res) => {
|
||
console.log(res.data, "成功")
|
||
}).catch((err) => {
|
||
if (err.response.status == 409) {
|
||
// console.log(err.response.headers['x-transmission-session-id'])
|
||
this.sessionId = err.response.headers['x-transmission-session-id']
|
||
this.getTokne()
|
||
}
|
||
})
|
||
|
||
}
|
||
/**
|
||
* 添加下载任务
|
||
* @param {string} file 文件地址
|
||
*/
|
||
addTask(file) {
|
||
const form = new FormData()
|
||
form.append('savepath', 'd:/bt');
|
||
form.append("autoTMM", "false")
|
||
form.append("rename", "")
|
||
form.append("category", "")
|
||
// true 开始
|
||
form.append("startTorrentHidden", "")
|
||
form.append("skip_checking", "")
|
||
form.append("contentLayout", "Original")
|
||
form.append("sequentialDownload", "")
|
||
form.append("dlLimitHidden", "0")
|
||
form.append("upLimitHidden", "0")
|
||
form.append("fileselect[]", fs.createReadStream(file))
|
||
form.submit(`${this.baseURL}/api/v2/torrents/add`, function (err, res) {
|
||
// res – response object (http.IncomingMessage) //
|
||
})
|
||
|
||
}
|
||
/**
|
||
* 将base64字符串转化为文件
|
||
* @param {string} base64str 要转换的base64字符串
|
||
* @param {string} file 保存到的文件路径
|
||
*/
|
||
base64_decode(base64str, file) {
|
||
// create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded
|
||
var bitmap = new Buffer.from(base64str, 'base64');
|
||
// write buffer to file
|
||
fs.writeFileSync(file, bitmap);
|
||
console.log('******** File created from base64 encoded string ********');
|
||
}
|
||
/**
|
||
* 将文件转化为base64
|
||
* @param {string} file 文件地址
|
||
* @returns 文件转化后的base64字符串
|
||
*/
|
||
base64_encode(file) {
|
||
// read binary data
|
||
var bitmap = fs.readFileSync(file);
|
||
// convert binary data to base64 encoded string
|
||
return new Buffer.from(bitmap).toString('base64');
|
||
}
|
||
/**
|
||
* 获取下载列表
|
||
*/
|
||
async getList() {
|
||
let { data: { torrents } } = await axios.get("/api/v2/sync/maindata")
|
||
// console.log(data.torrents)
|
||
// torrents
|
||
let list = []
|
||
for (let i in torrents) {
|
||
let info = {
|
||
name: torrents[i].name,
|
||
amount_left: (torrents[i].amount_left / 1024 / 1024).toFixed(2),
|
||
fileid: i
|
||
}
|
||
|
||
list.push(info)
|
||
}
|
||
console.log(list)
|
||
|
||
}
|
||
/**
|
||
* 根据hashes删除任务
|
||
* @param {string} hashes 删除任务的hashes
|
||
*/
|
||
async delete(hashes) {
|
||
try{
|
||
const form = new FormData()
|
||
form.append("hashes",hashes)
|
||
form.append("deleteFiles","false")
|
||
form.submit(`${this.baseURL}/api/v2/torrents/delete`)
|
||
}catch (err){
|
||
console.log("删除失败", err)
|
||
}
|
||
console.log("删除成功")
|
||
|
||
}
|
||
}
|
||
|
||
export default BtDonwold |