🧲 Added parse-torrent for metadata

This commit is contained in:
2024-02-29 08:37:24 -06:00
parent df07dff1db
commit 54974db2de
5 changed files with 366 additions and 88 deletions

View File

@@ -1,6 +1,8 @@
"use strict";
import { Context, Service, ServiceBroker, ServiceSchema, Errors } from "moleculer";
import { qBittorrentClient } from "@robertklep/qbittorrent";
import parseTorrent from "parse-torrent";
import { readFileSync, writeFileSync } from "fs";
export default class QBittorrentService extends Service {
// @ts-ignore
@@ -32,6 +34,7 @@ export default class QBittorrentService extends Service {
`${username}`,
`${password}`,
);
console.log(this.meta);
},
},
getClientInfo: {
@@ -45,6 +48,46 @@ export default class QBittorrentService extends Service {
};
},
},
addTorrent: {
rest: "POST /addTorrent",
handler: async (
ctx: Context<{
torrentToDownload: any;
comicObjectId: string;
}>,
) => {
try {
const { torrentToDownload } = ctx.params;
console.log(torrentToDownload);
const response = await fetch(torrentToDownload, {
method: "GET",
});
const buffer = await response.arrayBuffer();
writeFileSync(`mithrandir.torrent`, Buffer.from(buffer));
const result = await this.meta.torrents.add({
torrents: {
buffer: readFileSync("mithrandir.torrent"),
},
// start this torrent in a paused state (see Torrent type for options)
paused: true,
});
return {
result,
metadata: parseTorrent(readFileSync("mithrandir.torrent")),
};
} catch (err) {
console.error(err);
}
},
},
getTorrents: {
rest: "POST /getTorrents",
handler: async (ctx: Context<{}>) => {
return await this.meta.torrents.info();
},
},
},
methods: {},
});