Qbittorrent endpoints #3

Merged
rishighan merged 13 commits from qbittorrent-endpoints into main 2024-03-31 01:38:18 +00:00
Showing only changes of commit 20fe2327b4 - Show all commits

View File

@@ -14,6 +14,14 @@ export default class QBittorrentService extends Service {
hooks: {}, hooks: {},
settings: {}, settings: {},
actions: { actions: {
fetchQbittorrentCredentials: {
rest: "GET /fetchQbittorrentCredentials",
handler: async (ctx: Context<{}>) => {
return await this.broker.call("settings.getSettings", {
settingsKey: "bittorrent",
});
},
},
connect: { connect: {
rest: "POST /connect", rest: "POST /connect",
handler: async ( handler: async (
@@ -23,7 +31,7 @@ export default class QBittorrentService extends Service {
hostname: string; hostname: string;
port: string; port: string;
protocol: string; protocol: string;
name: string; name?: string;
}>, }>,
) => { ) => {
const { username, password, hostname, port, protocol } = ctx.params; const { username, password, hostname, port, protocol } = ctx.params;
@@ -39,10 +47,43 @@ export default class QBittorrentService extends Service {
} }
}, },
}, },
loginWithStoredCredentials: {
rest: "POST /loginWithStoredCredentials",
handler: async (ctx: Context<{}>) => {
try {
const result: any = await this.broker.call(
"qbittorrent.fetchQbittorrentCredentials",
{},
);
if (result !== undefined) {
const {
client: {
host: { username, password, hostname, port, protocol },
},
} = result;
return await this.broker.call("qbittorrent.connect", {
username,
password,
hostname,
port,
protocol,
});
}
} catch (err) {
return {
error: err,
message:
"Qbittorrent credentials not found, please configure them in Settings.",
};
}
},
},
getClientInfo: { getClientInfo: {
rest: "GET /getClientInfo", rest: "GET /getClientInfo",
handler: async (ctx: Context<{}>) => { handler: async (ctx: Context<{}>) => {
console.log(this.meta.app); console.log(this.meta.app);
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
return { return {
buildInfo: await this.meta.app.buildInfo(), buildInfo: await this.meta.app.buildInfo(),
version: await this.meta.app.version(), version: await this.meta.app.version(),
@@ -59,6 +100,7 @@ export default class QBittorrentService extends Service {
}>, }>,
) => { ) => {
try { try {
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
const { torrentToDownload, comicObjectId } = ctx.params; const { torrentToDownload, comicObjectId } = ctx.params;
console.log(torrentToDownload); console.log(torrentToDownload);
const response = await fetch(torrentToDownload, { const response = await fetch(torrentToDownload, {
@@ -95,11 +137,15 @@ export default class QBittorrentService extends Service {
}, },
getTorrents: { getTorrents: {
rest: "POST /getTorrents", rest: "POST /getTorrents",
handler: async (ctx: Context<{}>) => await this.meta.torrents.info(), handler: async (ctx: Context<{}>) => {
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
return await this.meta.torrents.info();
},
}, },
getTorrentDetails: { getTorrentDetails: {
rest: "POST /getTorrentDetails", rest: "POST /getTorrentDetails",
handler: async (ctx: Context<{ infoHashes: [string] }>) => { handler: async (ctx: Context<{ infoHashes: [string] }>) => {
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
const infoHashes = Object.values(ctx.params); const infoHashes = Object.values(ctx.params);
const torrentDetails = infoHashes.map(async (infoHash) => { const torrentDetails = infoHashes.map(async (infoHash) => {
return await this.meta.torrents.properties(infoHash); return await this.meta.torrents.properties(infoHash);
@@ -110,13 +156,7 @@ export default class QBittorrentService extends Service {
checkForDeletedTorrents: { checkForDeletedTorrents: {
rest: "GET /checkForDeletedTorrents", rest: "GET /checkForDeletedTorrents",
handler: async (ctx: Context<{ infoHashes: [string] }>) => { handler: async (ctx: Context<{ infoHashes: [string] }>) => {
await this.broker.call("qbittorrent.connect", { await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
hostname: "localhost",
protocol: "http",
port: "8080",
username: "admin",
password: "password",
});
const torrents: any = await this.broker.call("qbittorrent.getTorrents", {}); const torrents: any = await this.broker.call("qbittorrent.getTorrents", {});
const deletedTorrents = this.detectDeletedTorrents( const deletedTorrents = this.detectDeletedTorrents(
torrents.map((torrent: any) => torrent.hash), torrents.map((torrent: any) => torrent.hash),