Fixed eslint errors
Some checks failed
Docker Image CI / build (push) Has been cancelled

This commit is contained in:
Rishi Ghan
2026-04-15 11:35:11 -04:00
parent f65a24da25
commit afead56a74
7 changed files with 322 additions and 182 deletions

View File

@@ -1,12 +1,67 @@
import { readFileSync, writeFileSync } from "fs";
import { qBittorrentClient } from "@robertklep/qbittorrent";
import type { Context, ServiceBroker, ServiceSchema } from "moleculer";
import { Errors, Service } from "moleculer";
import type { Context, ServiceBroker } from "moleculer";
import { Service } from "moleculer";
import parseTorrent from "parse-torrent";
interface QBittorrentCredentials {
client: {
host: {
username: string;
password: string;
hostname: string;
port: string;
protocol: string;
};
};
}
interface ConnectParams {
username: string;
password: string;
hostname: string;
port: string;
protocol: string;
name?: string;
}
interface AddTorrentParams {
torrentToDownload: string;
comicObjectId: string;
}
interface InfoHashesParams {
infoHashes: string[];
}
interface TorrentRealTimeStatsParams {
infoHashes: { _id: string; infoHashes: string[] }[];
}
interface TorrentDetail {
torrent: Record<string, unknown>;
}
interface TorrentDetailsGroup {
_id: string;
details: TorrentDetail[];
}
interface SyncMaindata {
torrents: Record<string, Record<string, unknown>>;
rid?: number;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type QBittorrentMeta = any;
export default class QBittorrentService extends Service {
// @ts-ignore
constructor(public broker: ServiceBroker, schema: ServiceSchema<{}> = { name: "qbittorrent" }) {
private meta!: QBittorrentMeta;
private rid = 0;
// @ts-ignore -- Moleculer requires this constructor signature for service instantiation
constructor(broker: ServiceBroker) {
super(broker);
this.parseServiceSchema({
name: "qbittorrent",
@@ -16,42 +71,35 @@ export default class QBittorrentService extends Service {
actions: {
fetchQbittorrentCredentials: {
rest: "GET /fetchQbittorrentCredentials",
handler: async (ctx: Context<{}>) => {
return await this.broker.call("settings.getSettings", {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handler: async (ctx: Context<Record<string, never>>) => this.broker.call("settings.getSettings", {
settingsKey: "bittorrent",
});
},
}),
},
connect: {
rest: "POST /connect",
handler: async (
ctx: Context<{
username: string;
password: string;
hostname: string;
port: string;
protocol: string;
name?: string;
}>,
) => {
handler: (ctx: Context<ConnectParams>) => {
const { username, password, hostname, port, protocol } = ctx.params;
// eslint-disable-next-line new-cap
this.meta = new qBittorrentClient(
`${protocol}://${hostname}:${port}`,
`${username}`,
`${password}`,
);
console.log(this.meta);
this.logger.info(this.meta);
if (this.meta) {
return { success: true, message: "Logged in successfully" };
}
return { success: false, message: "Failed to connect" };
},
},
loginWithStoredCredentials: {
rest: "POST /loginWithStoredCredentials",
handler: async (ctx: Context<{}>) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handler: async (ctx: Context<Record<string, never>>) => {
try {
const result: any = await this.broker.call(
const result: QBittorrentCredentials | undefined = await this.broker.call(
"qbittorrent.fetchQbittorrentCredentials",
{},
);
@@ -69,10 +117,14 @@ export default class QBittorrentService extends Service {
port,
protocol,
});
console.log("qbittorrent connection details:");
console.log(JSON.stringify(connection, null, 4));
this.logger.info("qbittorrent connection details:");
this.logger.info(JSON.stringify(connection, null, 4));
return connection;
}
return {
error: null,
message: "Qbittorrent credentials not found.",
};
} catch (err) {
return {
error: err,
@@ -85,7 +137,8 @@ export default class QBittorrentService extends Service {
getClientInfo: {
rest: "GET /getClientInfo",
handler: async (ctx: Context<{}>) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handler: async (ctx: Context<Record<string, never>>) => {
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
return {
buildInfo: await this.meta.app.buildInfo(),
@@ -96,22 +149,17 @@ export default class QBittorrentService extends Service {
},
addTorrent: {
rest: "POST /addTorrent",
handler: async (
ctx: Context<{
torrentToDownload: any;
comicObjectId: string;
}>,
) => {
handler: async (ctx: Context<AddTorrentParams>) => {
try {
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
const { torrentToDownload, comicObjectId } = ctx.params;
console.log(torrentToDownload);
this.logger.info(torrentToDownload);
const response = await fetch(torrentToDownload, {
method: "GET",
});
// Read the buffer to a file
const buffer = await response.arrayBuffer();
writeFileSync(`mithrandir.torrent`, Buffer.from(buffer));
writeFileSync(`mithrandir.torrent`, new Uint8Array(buffer));
// Add the torrent to qbittorrent's queue, paused.
const result = await this.meta.torrents.add({
torrents: {
@@ -134,28 +182,33 @@ export default class QBittorrentService extends Service {
result,
};
} catch (err) {
console.error(err);
this.logger.error(err);
return {
error: err,
message: "Failed to add torrent",
};
}
},
},
getTorrents: {
rest: "POST /getTorrents",
handler: async (ctx: Context<{}>) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handler: async (ctx: Context<Record<string, never>>) => {
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
return await this.meta.torrents.info();
return this.meta.torrents.info();
},
},
getTorrentProperties: {
rest: "POST /getTorrentProperties",
handler: async (ctx: Context<{ infoHashes: string[] }>) => {
handler: async (ctx: Context<InfoHashesParams>) => {
try {
const { infoHashes } = ctx.params;
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
return await this.meta.torrents.info({
return this.meta.torrents.info({
hashes: infoHashes,
});
} catch (err) {
console.error("An error occurred:", err);
this.logger.error("An error occurred:", err);
// Consider handling the error more gracefully here, possibly returning an error response
throw err; // or return a specific error object/message
}
@@ -163,23 +216,21 @@ export default class QBittorrentService extends Service {
},
getTorrentRealTimeStats: {
rest: "POST /getTorrentRealTimeStats",
handler: async (
ctx: Context<{ infoHashes: { _id: string; infoHashes: string[] }[] }>,
) => {
handler: async (ctx: Context<TorrentRealTimeStatsParams>) => {
const { infoHashes } = ctx.params;
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
try {
// Increment rid for each call
this.rid = typeof this.rid === "number" ? this.rid + 1 : 0;
const data = await this.meta.sync.maindata(this.rid);
const torrentDetails: any = [];
const data: SyncMaindata = await this.meta.sync.maindata(this.rid);
const torrentDetails: TorrentDetailsGroup[] = [];
infoHashes.forEach(({ _id, infoHashes }) => {
infoHashes.forEach(({ _id, infoHashes: hashes }) => {
// Initialize an object to hold details for this _id
const details: any = [];
const details: TorrentDetail[] = [];
infoHashes.forEach((hash) => {
hashes.forEach((hash) => {
// Assuming 'data.torrents[hash]' retrieves the details for the hash
const torrent = data.torrents[hash];
if (torrent) {
@@ -201,9 +252,9 @@ export default class QBittorrentService extends Service {
// Assuming `data.rid` contains the latest rid from the server
if (data.rid !== undefined) {
this.rid = data.rid;
console.log(`rid is ${this.rid}`);
this.logger.info(`rid is ${this.rid}`);
}
console.log(JSON.stringify(torrentDetails, null, 4));
this.logger.info(JSON.stringify(torrentDetails, null, 4));
return torrentDetails;
} catch (err) {
this.logger.error(err);
@@ -213,24 +264,23 @@ export default class QBittorrentService extends Service {
},
determineDownloadApps: {
rest: "",
handler: async () => {
// 1. Parse the incoming search query
// to make sure that it is well-formed
// At the very least, it should have name, year, number
// 2. Choose between download mediums based on user-preference?
// possible choices are: DC++, Torrent
// 3. Perform the search on those media with the aforementioned search query
// 4. Choose a subset of relevant search results,
// and score them
// 5. Download the highest-scoring, relevant result
},
// 1. Parse the incoming search query
// to make sure that it is well-formed
// At the very least, it should have name, year, number
// 2. Choose between download mediums based on user-preference?
// possible choices are: DC++, Torrent
// 3. Perform the search on those media with the aforementioned search query
// 4. Choose a subset of relevant search results,
// and score them
// 5. Download the highest-scoring, relevant result
handler: () => undefined,
},
},
methods: {},
async started() {
console.log(`Initializing rid...`);
started() {
this.logger.info(`Initializing rid...`);
this.rid = 0;
console.log(`rid is ${this.rid}`);
this.logger.info(`rid is ${this.rid}`);
},
});
}