Compare commits
2 Commits
graphql-re
...
prowlarr-i
| Author | SHA1 | Date | |
|---|---|---|---|
| 011293376c | |||
| 9c10347d38 |
19
.github/workflows/docker-image.yml
vendored
19
.github/workflows/docker-image.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
name: Docker Image CI
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ main ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ main ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@master
|
|
||||||
- name: Publish to Registry
|
|
||||||
uses: elgohr/Publish-Docker-Github-Action@v5
|
|
||||||
with:
|
|
||||||
name: frishi/threetwo-acquisition-service
|
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -62,5 +62,3 @@ jspm_packages/
|
|||||||
|
|
||||||
# Don't track transpiled files
|
# Don't track transpiled files
|
||||||
dist/
|
dist/
|
||||||
.DS_Store
|
|
||||||
*.torrent
|
|
||||||
|
|||||||
29
Dockerfile
29
Dockerfile
@@ -1,29 +0,0 @@
|
|||||||
FROM node:22.1.0
|
|
||||||
LABEL maintainer="Rishi Ghan <rishi.ghan@gmail.com>"
|
|
||||||
|
|
||||||
# Set working directory
|
|
||||||
WORKDIR /acquisition-service
|
|
||||||
|
|
||||||
# Copy package files first for efficient caching
|
|
||||||
COPY package.json package-lock.json ./
|
|
||||||
|
|
||||||
# Install all dependencies (including devDependencies)
|
|
||||||
RUN npm install
|
|
||||||
|
|
||||||
# Copy necessary config files
|
|
||||||
COPY moleculer.config.ts tsconfig.json tsconfig.build.json ./
|
|
||||||
|
|
||||||
# Copy the rest of the source code
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# Build the application
|
|
||||||
RUN npm run build
|
|
||||||
|
|
||||||
# Now remove devDependencies to keep the final image small
|
|
||||||
RUN npm prune --omit=dev
|
|
||||||
|
|
||||||
# Expose the port
|
|
||||||
EXPOSE 3080
|
|
||||||
|
|
||||||
# Start the application
|
|
||||||
CMD ["npm", "start"]
|
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
"use strict";
|
import type { BrokerOptions, MetricRegistry, ServiceBroker } from "moleculer";
|
||||||
import {
|
import { Errors } from "moleculer";
|
||||||
BrokerOptions,
|
|
||||||
Errors,
|
|
||||||
MetricRegistry,
|
|
||||||
ServiceBroker,
|
|
||||||
} from "moleculer";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Moleculer ServiceBroker configuration file
|
* Moleculer ServiceBroker configuration file
|
||||||
@@ -35,7 +30,7 @@ const brokerConfig: BrokerOptions = {
|
|||||||
// Namespace of nodes to segment your nodes on the same network.
|
// Namespace of nodes to segment your nodes on the same network.
|
||||||
namespace: "",
|
namespace: "",
|
||||||
// Unique node identifier. Must be unique in a namespace.
|
// Unique node identifier. Must be unique in a namespace.
|
||||||
nodeID: "threetwo-acquisition-service",
|
nodeID: "threetwo-acquistion-service",
|
||||||
// Custom metadata store. Store here what you want. Accessing: `this.broker.metadata`
|
// Custom metadata store. Store here what you want. Accessing: `this.broker.metadata`
|
||||||
metadata: {},
|
metadata: {},
|
||||||
|
|
||||||
@@ -47,7 +42,7 @@ const brokerConfig: BrokerOptions = {
|
|||||||
// Using colors on the output
|
// Using colors on the output
|
||||||
colors: true,
|
colors: true,
|
||||||
// Print module names with different colors (like docker-compose for containers)
|
// Print module names with different colors (like docker-compose for containers)
|
||||||
moduleColors: true,
|
moduleColors: false,
|
||||||
// Line formatter. It can be "json", "short", "simple", "full", a `Function` or a template string like "{timestamp} {level} {nodeID}/{mod}: {msg}"
|
// Line formatter. It can be "json", "short", "simple", "full", a `Function` or a template string like "{timestamp} {level} {nodeID}/{mod}: {msg}"
|
||||||
formatter: "full",
|
formatter: "full",
|
||||||
// Custom object printer. If not defined, it uses the `util.inspect` method.
|
// Custom object printer. If not defined, it uses the `util.inspect` method.
|
||||||
@@ -64,11 +59,11 @@ const brokerConfig: BrokerOptions = {
|
|||||||
// More info: https://moleculer.services/docs/0.14/networking.html
|
// More info: https://moleculer.services/docs/0.14/networking.html
|
||||||
// Note: During the development, you don't need to define it because all services will be loaded locally.
|
// Note: During the development, you don't need to define it because all services will be loaded locally.
|
||||||
// In production you can set it via `TRANSPORTER=nats://localhost:4222` environment variable.
|
// In production you can set it via `TRANSPORTER=nats://localhost:4222` environment variable.
|
||||||
transporter: process.env.REDIS_URI || "redis://localhost:6379", // "NATS"
|
transporter: process.env.REDIS_URI || "redis://localhost:6379",
|
||||||
|
|
||||||
// Define a cacher.
|
// Define a cacher.
|
||||||
// More info: https://moleculer.services/docs/0.14/caching.html
|
// More info: https://moleculer.services/docs/0.14/caching.html
|
||||||
cacher: "Memory",
|
cacher: "Redis",
|
||||||
|
|
||||||
// Define a serializer.
|
// Define a serializer.
|
||||||
// Available values: "JSON", "Avro", "ProtoBuf", "MsgPack", "Notepack", "Thrift".
|
// Available values: "JSON", "Avro", "ProtoBuf", "MsgPack", "Notepack", "Thrift".
|
||||||
@@ -91,7 +86,8 @@ const brokerConfig: BrokerOptions = {
|
|||||||
// Backoff factor for delay. 2 means exponential backoff.
|
// Backoff factor for delay. 2 means exponential backoff.
|
||||||
factor: 2,
|
factor: 2,
|
||||||
// A function to check failed requests.
|
// A function to check failed requests.
|
||||||
check: (err: Errors.MoleculerError) => err && !!err.retryable,
|
check: (err: Error) =>
|
||||||
|
err && err instanceof Errors.MoleculerRetryableError && !!err.retryable,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Limit of calling level. If it reaches the limit, broker will throw an MaxCallLevelError error. (Infinite loop protection)
|
// Limit of calling level. If it reaches the limit, broker will throw an MaxCallLevelError error. (Infinite loop protection)
|
||||||
@@ -138,7 +134,7 @@ const brokerConfig: BrokerOptions = {
|
|||||||
// Number of milliseconds to switch from open to half-open state
|
// Number of milliseconds to switch from open to half-open state
|
||||||
halfOpenTime: 10 * 1000,
|
halfOpenTime: 10 * 1000,
|
||||||
// A function to check failed requests.
|
// A function to check failed requests.
|
||||||
check: (err: Errors.MoleculerError) => err && err.code >= 500,
|
check: (err: Error) => err && err instanceof Errors.MoleculerError && err.code >= 500,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Settings of bulkhead feature. More info: https://moleculer.services/docs/0.14/fault-tolerance.html#Bulkhead
|
// Settings of bulkhead feature. More info: https://moleculer.services/docs/0.14/fault-tolerance.html#Bulkhead
|
||||||
@@ -199,13 +195,16 @@ const brokerConfig: BrokerOptions = {
|
|||||||
middlewares: [],
|
middlewares: [],
|
||||||
|
|
||||||
// Register custom REPL commands.
|
// Register custom REPL commands.
|
||||||
/*
|
replCommands: null,
|
||||||
|
|
||||||
// Called after broker created.
|
// Called after broker created.
|
||||||
created : (broker: ServiceBroker): void => {},
|
// created(broker: ServiceBroker): void {},
|
||||||
|
|
||||||
// Called after broker started.
|
// Called after broker started.
|
||||||
started: async (broker: ServiceBroker): Promise<void> => {},
|
// async started(broker: ServiceBroker): Promise<void> {},
|
||||||
stopped: async (broker: ServiceBroker): Promise<void> => {},
|
|
||||||
*/
|
// Called after broker stopped.
|
||||||
|
// async stopped(broker: ServiceBroker): Promise<void> {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export = brokerConfig;
|
export = brokerConfig;
|
||||||
|
|||||||
658
package-lock.json
generated
658
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
21
package.json
21
package.json
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "threetwo-acquisition-service",
|
"name": "threetwo-acquisition-service",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "My Moleculer-based microservices project",
|
"description": "",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc --project tsconfig.build.json",
|
"build": "tsc --project tsconfig.build.json",
|
||||||
"dev": "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --config moleculer.config.ts --hot --repl services/**/*.service.ts",
|
"dev": "ts-node ./node_modules/moleculer/bin/moleculer-runner.js --config moleculer.config.ts --hot --repl services/**/*.service.ts",
|
||||||
@@ -19,14 +19,11 @@
|
|||||||
"microservices",
|
"microservices",
|
||||||
"moleculer"
|
"moleculer"
|
||||||
],
|
],
|
||||||
"author": "",
|
"author": "Rishi Ghan",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@jest/globals": "^29.3.1",
|
"@jest/globals": "^29.3.1",
|
||||||
"@types/jest": "^29.2.3",
|
"@types/jest": "^29.2.3",
|
||||||
"@types/lodash": "^4.17.4",
|
|
||||||
"@types/node": "^18.11.9",
|
"@types/node": "^18.11.9",
|
||||||
"@types/parse-torrent": "^5.8.7",
|
|
||||||
"@types/qs": "^6.9.18",
|
|
||||||
"@typescript-eslint/eslint-plugin": "^5.44.0",
|
"@typescript-eslint/eslint-plugin": "^5.44.0",
|
||||||
"@typescript-eslint/parser": "^5.44.0",
|
"@typescript-eslint/parser": "^5.44.0",
|
||||||
"concurrently": "^7.6.0",
|
"concurrently": "^7.6.0",
|
||||||
@@ -40,24 +37,18 @@
|
|||||||
"jest": "^29.3.1",
|
"jest": "^29.3.1",
|
||||||
"moleculer-repl": "^0.7.3",
|
"moleculer-repl": "^0.7.3",
|
||||||
"prettier": "^2.8.0",
|
"prettier": "^2.8.0",
|
||||||
"qbittorrent-api-v2": "^1.2.2",
|
|
||||||
"ts-jest": "^29.0.3",
|
"ts-jest": "^29.0.3",
|
||||||
"ts-node": "^10.9.1",
|
"ts-node": "^10.9.1",
|
||||||
"typescript": "^4.9.3"
|
"typescript": "^4.9.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@robertklep/qbittorrent": "^1.0.1",
|
"@robertklep/qbittorrent": "^1.0.1",
|
||||||
|
"axios": "^1.5.0",
|
||||||
"ioredis": "^5.0.0",
|
"ioredis": "^5.0.0",
|
||||||
"kafkajs": "^2.2.4",
|
"moleculer": "^0.14.27",
|
||||||
"axios": "^1.7.9",
|
"moleculer-web": "^0.10.5"
|
||||||
"lodash": "^4.17.21",
|
|
||||||
"moleculer": "^0.14.34",
|
|
||||||
"moleculer-web": "^0.10.7",
|
|
||||||
"parse-torrent": "^9.1.5",
|
|
||||||
"socket.io-client": "^4.7.5",
|
|
||||||
"string-similarity-alg": "^1.3.2"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 16.x.x"
|
"node": ">= 20.x.x"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,43 @@
|
|||||||
import fs from "fs";
|
import { IncomingMessage } from "http";
|
||||||
import { Service, ServiceBroker } from "moleculer";
|
import { Service, ServiceBroker, Context } from "moleculer";
|
||||||
import ApiGateway from "moleculer-web";
|
import ApiGateway from "moleculer-web";
|
||||||
|
|
||||||
export default class ApiService extends Service {
|
export default class ApiService extends Service {
|
||||||
constructor(broker: ServiceBroker) {
|
public constructor(broker: ServiceBroker) {
|
||||||
super(broker);
|
super(broker);
|
||||||
|
// @ts-ignore
|
||||||
this.parseServiceSchema({
|
this.parseServiceSchema({
|
||||||
name: "api",
|
name: "api",
|
||||||
mixins: [ApiGateway],
|
mixins: [ApiGateway],
|
||||||
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
|
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
|
||||||
settings: {
|
settings: {
|
||||||
port: process.env.PORT || 3060,
|
port: process.env.PORT || 3060,
|
||||||
|
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: "/api",
|
path: "/api",
|
||||||
whitelist: ["**"],
|
whitelist: ["**"],
|
||||||
|
use: [],
|
||||||
|
mergeParams: true,
|
||||||
cors: {
|
cors: {
|
||||||
origin: "*",
|
origin: "*",
|
||||||
methods: ["GET", "OPTIONS", "POST", "PUT", "DELETE"],
|
methods: [
|
||||||
|
"GET",
|
||||||
|
"OPTIONS",
|
||||||
|
"POST",
|
||||||
|
"PUT",
|
||||||
|
"DELETE",
|
||||||
|
],
|
||||||
allowedHeaders: ["*"],
|
allowedHeaders: ["*"],
|
||||||
exposedHeaders: [],
|
exposedHeaders: [],
|
||||||
credentials: false,
|
credentials: false,
|
||||||
maxAge: 3600,
|
maxAge: 3600,
|
||||||
},
|
},
|
||||||
use: [],
|
|
||||||
mergeParams: true,
|
|
||||||
authentication: false,
|
authentication: false,
|
||||||
authorization: false,
|
authorization: false,
|
||||||
autoAliases: true,
|
autoAliases: true,
|
||||||
|
|
||||||
aliases: {},
|
aliases: {},
|
||||||
callingOptions: {},
|
callingOptions: {},
|
||||||
|
|
||||||
@@ -42,27 +52,90 @@ export default class ApiService extends Service {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
mappingPolicy: "all", // Available values: "all", "restrict"
|
mappingPolicy: "all", // Available values: "all", "restrict"
|
||||||
|
|
||||||
|
// Enable/disable logging
|
||||||
logging: true,
|
logging: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
|
||||||
path: "/logs",
|
|
||||||
use: [ApiGateway.serveStatic("logs")],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
|
// Do not log client side errors (does not log an error response when the error.code is 400<=X<500)
|
||||||
log4XXResponses: false,
|
log4XXResponses: false,
|
||||||
logRequestParams: true,
|
// Logging the request parameters. Set to any log level to enable it. E.g. "info"
|
||||||
logResponseData: true,
|
logRequestParams: null,
|
||||||
|
logResponseData: null,
|
||||||
assets: {
|
assets: {
|
||||||
folder: "public",
|
folder: "public",
|
||||||
// Options to `server-static` module
|
|
||||||
options: {},
|
options: {},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
events: {},
|
|
||||||
|
|
||||||
methods: {},
|
methods: {
|
||||||
started(): any {},
|
/**
|
||||||
|
* Authenticate the request. It checks the `Authorization` token value in the request header.
|
||||||
|
* Check the token value & resolve the user by the token.
|
||||||
|
* The resolved user will be available in `ctx.meta.user`
|
||||||
|
*
|
||||||
|
* PLEASE NOTE, IT'S JUST AN EXAMPLE IMPLEMENTATION. DO NOT USE IN PRODUCTION!
|
||||||
|
*
|
||||||
|
* @param {Context} ctx
|
||||||
|
* @param {any} route
|
||||||
|
* @param {IncomingMessage} req
|
||||||
|
* @returns {Promise}
|
||||||
|
|
||||||
|
async authenticate (ctx: Context, route: any, req: IncomingMessage): Promise < any > => {
|
||||||
|
// Read the token from header
|
||||||
|
const auth = req.headers.authorization;
|
||||||
|
|
||||||
|
if (auth && auth.startsWith("Bearer")) {
|
||||||
|
const token = auth.slice(7);
|
||||||
|
|
||||||
|
// Check the token. Tip: call a service which verify the token. E.g. `accounts.resolveToken`
|
||||||
|
if (token === "123456") {
|
||||||
|
// Returns the resolved user. It will be set to the `ctx.meta.user`
|
||||||
|
return {
|
||||||
|
id: 1,
|
||||||
|
name: "John Doe",
|
||||||
|
};
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Invalid token
|
||||||
|
throw new ApiGateway.Errors.UnAuthorizedError(ApiGateway.Errors.ERR_INVALID_TOKEN, {
|
||||||
|
error: "Invalid Token",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// No token. Throw an error or do nothing if anonymous access is allowed.
|
||||||
|
// Throw new E.UnAuthorizedError(E.ERR_NO_TOKEN);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Authorize the request. Check that the authenticated user has right to access the resource.
|
||||||
|
*
|
||||||
|
* PLEASE NOTE, IT'S JUST AN EXAMPLE IMPLEMENTATION. DO NOT USE IN PRODUCTION!
|
||||||
|
*
|
||||||
|
* @param {Context} ctx
|
||||||
|
* @param {Object} route
|
||||||
|
* @param {IncomingMessage} req
|
||||||
|
* @returns {Promise}
|
||||||
|
|
||||||
|
async authorize (ctx: Context < any, {
|
||||||
|
user: string;
|
||||||
|
} > , route: Record<string, undefined>, req: IncomingMessage): Promise < any > => {
|
||||||
|
// Get the authenticated user.
|
||||||
|
const user = ctx.meta.user;
|
||||||
|
|
||||||
|
// It check the `auth` property in action schema.
|
||||||
|
// @ts-ignore
|
||||||
|
if (req.$action.auth === "required" && !user) {
|
||||||
|
throw new ApiGateway.Errors.UnAuthorizedError("NO_RIGHTS", {
|
||||||
|
error: "Unauthorized",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,114 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
import { Kafka } from "kafkajs";
|
|
||||||
import type { Context, ServiceBroker, ServiceSchema } from "moleculer";
|
|
||||||
import { Errors, Service } from "moleculer";
|
|
||||||
|
|
||||||
interface Comic {
|
|
||||||
wanted: {
|
|
||||||
markEntireVolumeWanted?: boolean;
|
|
||||||
issues?: any[];
|
|
||||||
volume: {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class AutoDownloadService extends Service {
|
|
||||||
private kafkaProducer: any;
|
|
||||||
|
|
||||||
private readonly BATCH_SIZE = 100; // Adjust based on your system capacity
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
constructor(
|
|
||||||
public broker: ServiceBroker,
|
|
||||||
schema: ServiceSchema<{}> = { name: "autodownload" },
|
|
||||||
) {
|
|
||||||
super(broker);
|
|
||||||
this.parseServiceSchema({
|
|
||||||
name: "autodownload",
|
|
||||||
actions: {
|
|
||||||
searchWantedComics: {
|
|
||||||
rest: "POST /searchWantedComics",
|
|
||||||
handler: async (ctx: Context<{}>) => {
|
|
||||||
try {
|
|
||||||
/* eslint-disable no-await-in-loop */
|
|
||||||
let page = 1;
|
|
||||||
const limit = this.BATCH_SIZE;
|
|
||||||
let comics: Comic[];
|
|
||||||
do {
|
|
||||||
comics = await this.broker.call(
|
|
||||||
"library.getComicsMarkedAsWanted",
|
|
||||||
{ page, limit },
|
|
||||||
);
|
|
||||||
// Log debugging info
|
|
||||||
this.logger.info(
|
|
||||||
"Received comics from getComicsMarkedAsWanted:",
|
|
||||||
JSON.stringify(comics, null, 2),
|
|
||||||
);
|
|
||||||
if (!Array.isArray(comics)) {
|
|
||||||
this.logger.error(
|
|
||||||
"Invalid response structure",
|
|
||||||
JSON.stringify(comics, null, 2),
|
|
||||||
);
|
|
||||||
throw new Errors.MoleculerError(
|
|
||||||
"Invalid response structure from getComicsMarkedAsWanted",
|
|
||||||
500,
|
|
||||||
"INVALID_RESPONSE_STRUCTURE",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
this.logger.info(
|
|
||||||
`Fetched ${comics.length} comics from page ${page}`,
|
|
||||||
);
|
|
||||||
for (const comic of comics) {
|
|
||||||
await this.produceJobToKafka(comic);
|
|
||||||
}
|
|
||||||
page += 1;
|
|
||||||
} while (comics.length === limit);
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
message: "Jobs enqueued for background processing.",
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error("Error in searchWantedComics:", error);
|
|
||||||
throw new Errors.MoleculerError(
|
|
||||||
"Failed to search wanted comics.",
|
|
||||||
500,
|
|
||||||
"SEARCH_WANTED_COMICS_ERROR",
|
|
||||||
{ error },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
produceJobToKafka: async (comic: Comic) => {
|
|
||||||
const job = { comic };
|
|
||||||
try {
|
|
||||||
await this.kafkaProducer.send({
|
|
||||||
topic: "comic-search-jobs",
|
|
||||||
messages: [{ value: JSON.stringify(job) }],
|
|
||||||
});
|
|
||||||
this.logger.info("Produced job to Kafka:", job);
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error("Error producing job to Kafka:", error);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async started() {
|
|
||||||
const kafka = new Kafka({
|
|
||||||
clientId: "comic-search-service",
|
|
||||||
brokers: [process.env.KAFKA_BROKER_URI],
|
|
||||||
});
|
|
||||||
this.kafkaProducer = kafka.producer();
|
|
||||||
await this.kafkaProducer.connect();
|
|
||||||
this.logger.info("Kafka producer connected successfully.");
|
|
||||||
},
|
|
||||||
async stopped() {
|
|
||||||
await this.kafkaProducer.disconnect();
|
|
||||||
this.logger.info("Kafka producer disconnected successfully.");
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,304 +0,0 @@
|
|||||||
import type { EachMessagePayload } from "kafkajs";
|
|
||||||
import { Kafka, logLevel } from "kafkajs";
|
|
||||||
import { isNil, isUndefined } from "lodash";
|
|
||||||
import type { ServiceBroker, ServiceSchema } from "moleculer";
|
|
||||||
import { Service } from "moleculer";
|
|
||||||
import io from "socket.io-client";
|
|
||||||
import stringSimilarity from "string-similarity-alg";
|
|
||||||
|
|
||||||
interface SearchResult {
|
|
||||||
groupedResult: { entityId: number; payload: any };
|
|
||||||
updatedResult: { entityId: number; payload: any };
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class ComicProcessorService extends Service {
|
|
||||||
private kafkaConsumer: any;
|
|
||||||
private socketIOInstance: any;
|
|
||||||
private kafkaProducer: any;
|
|
||||||
private prowlarrResultsMap: Map<string, any> = new Map();
|
|
||||||
private airDCPPSearchResults: Map<number, any[]> = new Map();
|
|
||||||
private issuesToSearch: any = [];
|
|
||||||
|
|
||||||
// @ts-ignore: schema parameter is required by Service constructor
|
|
||||||
constructor(
|
|
||||||
public broker: ServiceBroker,
|
|
||||||
schema: ServiceSchema<object> = { name: "comicProcessor" },
|
|
||||||
) {
|
|
||||||
super(broker, schema);
|
|
||||||
this.parseServiceSchema({
|
|
||||||
name: "comicProcessor",
|
|
||||||
methods: {
|
|
||||||
parseStringDate: (dateString: string) => {
|
|
||||||
const date = new Date(dateString);
|
|
||||||
return {
|
|
||||||
year: date.getFullYear(),
|
|
||||||
month: date.getMonth() + 1,
|
|
||||||
day: date.getDate(),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
rankSearchResults: async (results: Map<number, any[]>, query: string) => {
|
|
||||||
// Find the highest-ranked response based on similarity to the search string
|
|
||||||
let highestRankedResult = null;
|
|
||||||
let highestSimilarity = -1;
|
|
||||||
|
|
||||||
results.forEach((resultArray) => {
|
|
||||||
resultArray.forEach((result) => {
|
|
||||||
const similarity = stringSimilarity("jaro-winkler").compare(
|
|
||||||
result.name,
|
|
||||||
query,
|
|
||||||
);
|
|
||||||
if (similarity > highestSimilarity) {
|
|
||||||
highestSimilarity = similarity;
|
|
||||||
highestRankedResult = { ...result, similarity };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return highestRankedResult;
|
|
||||||
},
|
|
||||||
processJob: async (job: any) => {
|
|
||||||
try {
|
|
||||||
this.logger.info("Processing job:", JSON.stringify(job, null, 2));
|
|
||||||
// Get the hub to search on
|
|
||||||
const settings: any = await this.broker.call("settings.getSettings", {
|
|
||||||
settingsKey: "directConnect",
|
|
||||||
});
|
|
||||||
const hubs = settings.client.hubs.map((hub: any) => hub.value);
|
|
||||||
|
|
||||||
const { comic } = job;
|
|
||||||
const { volume, issues, markEntireVolumeWanted } = comic.wanted;
|
|
||||||
|
|
||||||
// If entire volume is marked as wanted, get their details from CV
|
|
||||||
if (markEntireVolumeWanted) {
|
|
||||||
this.issuesToSearch = await this.broker.call(
|
|
||||||
"comicvine.getIssuesForVolume",
|
|
||||||
{ volumeId: volume.id },
|
|
||||||
);
|
|
||||||
this.logger.info(
|
|
||||||
`The entire volume with id: ${volume.id} was marked as wanted.`,
|
|
||||||
);
|
|
||||||
this.logger.info(`Fetched issues for ${volume.id}:`);
|
|
||||||
this.logger.info(`${this.issuesToSearch.length} issues to search`);
|
|
||||||
} else {
|
|
||||||
// Or proceed with `issues` from the wanted object.
|
|
||||||
this.issuesToSearch = issues;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const issue of this.issuesToSearch) {
|
|
||||||
// Query builder for DC++
|
|
||||||
// 1. issue number
|
|
||||||
const inferredIssueNumber =
|
|
||||||
issue.issueNumber || issue.issue_number || "";
|
|
||||||
// 2. year
|
|
||||||
const { year } = this.parseStringDate(issue.coverDate);
|
|
||||||
const inferredYear = year || issue.year || "";
|
|
||||||
|
|
||||||
// 3. Orchestrate the query
|
|
||||||
const dcppSearchQuery = {
|
|
||||||
query: {
|
|
||||||
pattern: `${volume.name
|
|
||||||
.replace(/[^\w\s]/g, "")
|
|
||||||
.replace(/\s+/g, " ")
|
|
||||||
.trim()}`,
|
|
||||||
extensions: ["cbz", "cbr", "cb7"],
|
|
||||||
},
|
|
||||||
hub_urls: hubs,
|
|
||||||
priority: 5,
|
|
||||||
};
|
|
||||||
this.logger.info(
|
|
||||||
"DC++ search query:",
|
|
||||||
JSON.stringify(dcppSearchQuery, null, 4),
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.broker.call("socket.search", {
|
|
||||||
query: dcppSearchQuery,
|
|
||||||
config: {
|
|
||||||
hostname: "192.168.1.119:5600",
|
|
||||||
protocol: "http",
|
|
||||||
username: "admin",
|
|
||||||
password: "password",
|
|
||||||
},
|
|
||||||
namespace: "/automated",
|
|
||||||
});
|
|
||||||
|
|
||||||
// const prowlarrResults = await this.broker.call("prowlarr.search", {
|
|
||||||
// prowlarrQuery: {
|
|
||||||
// port: "9696",
|
|
||||||
// apiKey: "c4f42e265fb044dc81f7e88bd41c3367",
|
|
||||||
// offset: 0,
|
|
||||||
// categories: [7030],
|
|
||||||
// query: `${volume.name} ${issue.issueNumber} ${year}`,
|
|
||||||
// host: "localhost",
|
|
||||||
// limit: 100,
|
|
||||||
// type: "search",
|
|
||||||
// indexerIds: [2],
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// this.logger.info(
|
|
||||||
// "Prowlarr search results:",
|
|
||||||
// JSON.stringify(prowlarrResults, null, 4),
|
|
||||||
// );
|
|
||||||
|
|
||||||
// Store prowlarr results in map using unique key
|
|
||||||
// const key = `${volume.name}-${issue.issueNumber}-${year}`;
|
|
||||||
// this.prowlarrResultsMap.set(key, prowlarrResults);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error("Error processing job:", error);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
produceResultsToKafka: async (query: string, result: any[]): Promise<void> => {
|
|
||||||
try {
|
|
||||||
/*
|
|
||||||
Match and rank
|
|
||||||
*/
|
|
||||||
const finalResult = await this.rankSearchResults(
|
|
||||||
this.airDCPPSearchResults,
|
|
||||||
query,
|
|
||||||
);
|
|
||||||
/*
|
|
||||||
Kafka messages need to be in a format that can be serialized to JSON,
|
|
||||||
and a Map is not directly serializable in a way that retains its structure,
|
|
||||||
hence why we use Object.fromEntries
|
|
||||||
*/
|
|
||||||
await this.kafkaProducer.send({
|
|
||||||
topic: "comic-search-results",
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
value: JSON.stringify(finalResult),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
this.logger.info(`Produced results to Kafka.`);
|
|
||||||
|
|
||||||
// socket event for UI
|
|
||||||
await this.broker.call("socket.broadcast", {
|
|
||||||
namespace: "/",
|
|
||||||
event: "searchResultsAvailable",
|
|
||||||
args: [
|
|
||||||
{
|
|
||||||
query,
|
|
||||||
finalResult,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error("Error producing results to Kafka:", error);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async started() {
|
|
||||||
const kafka = new Kafka({
|
|
||||||
clientId: "comic-processor-service",
|
|
||||||
brokers: [process.env.KAFKA_BROKER_URI],
|
|
||||||
logLevel: logLevel.INFO,
|
|
||||||
});
|
|
||||||
this.kafkaConsumer = kafka.consumer({ groupId: "comic-processor-group" });
|
|
||||||
this.kafkaProducer = kafka.producer();
|
|
||||||
|
|
||||||
this.kafkaConsumer.on("consumer.crash", (event: any) => {
|
|
||||||
this.logger.error("Consumer crash:", event);
|
|
||||||
});
|
|
||||||
this.kafkaConsumer.on("consumer.connect", () => {
|
|
||||||
this.logger.info("Consumer connected");
|
|
||||||
});
|
|
||||||
this.kafkaConsumer.on("consumer.disconnect", () => {
|
|
||||||
this.logger.info("Consumer disconnected");
|
|
||||||
});
|
|
||||||
this.kafkaConsumer.on("consumer.network.request_timeout", () => {
|
|
||||||
this.logger.warn("Consumer network request timeout");
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.kafkaConsumer.connect();
|
|
||||||
await this.kafkaProducer.connect();
|
|
||||||
|
|
||||||
await this.kafkaConsumer.subscribe({
|
|
||||||
topic: "comic-search-jobs",
|
|
||||||
fromBeginning: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.kafkaConsumer.run({
|
|
||||||
eachMessage: async ({ topic, partition, message }: EachMessagePayload) => {
|
|
||||||
if (message.value) {
|
|
||||||
const job = JSON.parse(message.value.toString());
|
|
||||||
await this.processJob(job);
|
|
||||||
} else {
|
|
||||||
this.logger.warn("Received message with null value");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
this.socketIOInstance = io("ws://localhost:3001/automated", {
|
|
||||||
transports: ["websocket"],
|
|
||||||
withCredentials: true,
|
|
||||||
});
|
|
||||||
this.socketIOInstance.on("connect", () => {
|
|
||||||
this.logger.info("Socket.IO connected successfully.");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle searchResultAdded event
|
|
||||||
this.socketIOInstance.on("searchResultAdded", (result: SearchResult) => {
|
|
||||||
const {
|
|
||||||
groupedResult: { entityId, payload },
|
|
||||||
} = result;
|
|
||||||
|
|
||||||
this.logger.info(
|
|
||||||
`AirDC++ Search result added for entityId: ${entityId} - ${payload?.name}`,
|
|
||||||
);
|
|
||||||
if (!this.airDCPPSearchResults.has(entityId)) {
|
|
||||||
this.airDCPPSearchResults.set(entityId, []);
|
|
||||||
}
|
|
||||||
if (!isNil(payload)) {
|
|
||||||
this.airDCPPSearchResults.get(entityId).push(payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"Updated airDCPPSearchResults:",
|
|
||||||
JSON.stringify(Array.from(this.airDCPPSearchResults.entries()), null, 4),
|
|
||||||
);
|
|
||||||
console.log(JSON.stringify(payload, null, 4));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle searchResultUpdated event
|
|
||||||
this.socketIOInstance.on("searchResultUpdated", (result: SearchResult) => {
|
|
||||||
const {
|
|
||||||
updatedResult: { entityId, payload },
|
|
||||||
} = result;
|
|
||||||
const resultsForInstance = this.airDCPPSearchResults.get(entityId);
|
|
||||||
|
|
||||||
if (resultsForInstance) {
|
|
||||||
const toReplaceIndex = resultsForInstance.findIndex((element: any) => {
|
|
||||||
this.logger.info("search result updated!");
|
|
||||||
this.logger.info(JSON.stringify(element, null, 4));
|
|
||||||
return element.id === payload.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (toReplaceIndex !== -1) {
|
|
||||||
// Replace the existing result with the updated result
|
|
||||||
resultsForInstance[toReplaceIndex] = payload;
|
|
||||||
// Optionally, update the map with the modified array
|
|
||||||
this.airDCPPSearchResults.set(entityId, resultsForInstance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle searchComplete event
|
|
||||||
this.socketIOInstance.on("searchesSent", async (data: any) => {
|
|
||||||
this.logger.info(
|
|
||||||
`Search complete for query: "${data.searchInfo.query.pattern}"`,
|
|
||||||
);
|
|
||||||
await this.produceResultsToKafka(data.searchInfo.query.pattern);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
async stopped() {
|
|
||||||
await this.kafkaConsumer.disconnect();
|
|
||||||
await this.kafkaProducer.disconnect();
|
|
||||||
|
|
||||||
if (this.socketIOInstance) {
|
|
||||||
this.socketIOInstance.close();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,123 +1,39 @@
|
|||||||
|
"use strict";
|
||||||
import { Context, Service, ServiceBroker, ServiceSchema, Errors } from "moleculer";
|
import { Context, Service, ServiceBroker, ServiceSchema, Errors } from "moleculer";
|
||||||
import axios from "axios";
|
import { qBittorrentClient } from "@robertklep/qbittorrent";
|
||||||
|
const { MoleculerError } = require("moleculer").Errors;
|
||||||
|
|
||||||
export default class ProwlarrService extends Service {
|
export default class QBittorrentService extends Service {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor(
|
public constructor(
|
||||||
public broker: ServiceBroker,
|
public broker: ServiceBroker,
|
||||||
schema: ServiceSchema<{}> = { name: "prowlarr" },
|
schema: ServiceSchema<{}> = { name: "qbittorrent" },
|
||||||
) {
|
) {
|
||||||
super(broker);
|
super(broker);
|
||||||
this.parseServiceSchema({
|
this.parseServiceSchema({
|
||||||
name: "prowlarr",
|
name: "qbittorrent",
|
||||||
mixins: [],
|
mixins: [],
|
||||||
hooks: {},
|
hooks: {},
|
||||||
actions: {
|
actions: {
|
||||||
connect: {
|
|
||||||
rest: "POST /connect",
|
getList: {
|
||||||
handler: async (
|
rest: "GET /getTorrents",
|
||||||
ctx: Context<{
|
|
||||||
host: string;
|
|
||||||
port: string;
|
|
||||||
apiKey: string;
|
|
||||||
}>,
|
|
||||||
) => {
|
|
||||||
const { host, port, apiKey } = ctx.params;
|
|
||||||
const result = await axios.request({
|
|
||||||
url: `http://${host}:${port}/api`,
|
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
"X-Api-Key": apiKey,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
console.log(result.data);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
getIndexers: {
|
|
||||||
rest: "GET /indexers",
|
|
||||||
handler: async (
|
|
||||||
ctx: Context<{ host: string; port: string; apiKey: string }>,
|
|
||||||
) => {
|
|
||||||
const { host, port, apiKey } = ctx.params;
|
|
||||||
const result = await axios.request({
|
|
||||||
url: `http://${host}:${port}/api/v1/indexer`,
|
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
"X-Api-Key": apiKey,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return result.data;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
search: {
|
|
||||||
rest: "GET /search",
|
|
||||||
handler: async (
|
|
||||||
ctx: Context<{
|
|
||||||
prowlarrQuery: {
|
|
||||||
host: string;
|
|
||||||
port: string;
|
|
||||||
apiKey: string;
|
|
||||||
query: string;
|
|
||||||
type: string;
|
|
||||||
indexerIds: [number];
|
|
||||||
categories: [number];
|
|
||||||
limit: number;
|
|
||||||
offset: number;
|
|
||||||
};
|
|
||||||
}>,
|
|
||||||
) => {
|
|
||||||
const {
|
|
||||||
prowlarrQuery: {
|
|
||||||
indexerIds,
|
|
||||||
categories,
|
|
||||||
host,
|
|
||||||
port,
|
|
||||||
apiKey,
|
|
||||||
query,
|
|
||||||
type,
|
|
||||||
limit,
|
|
||||||
offset,
|
|
||||||
},
|
|
||||||
} = ctx.params;
|
|
||||||
const indexer = indexerIds[0] ? indexerIds.length === 1 : indexerIds;
|
|
||||||
const category = categories[0] ? categories.length === 1 : categories;
|
|
||||||
const result = await axios({
|
|
||||||
url: `http://${host}:${port}/api/v1/search`,
|
|
||||||
method: "GET",
|
|
||||||
params: {
|
|
||||||
query,
|
|
||||||
type,
|
|
||||||
indexer,
|
|
||||||
category,
|
|
||||||
limit,
|
|
||||||
offset,
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
Accept: "application/json",
|
|
||||||
"X-Api-Key": `${apiKey}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return result.data;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ping: {
|
|
||||||
rest: "GET /ping",
|
|
||||||
handler: async (ctx: Context<{}>) => {
|
handler: async (ctx: Context<{}>) => {
|
||||||
const foo = await axios.request({
|
return await this.torrentClient.torrents.info()
|
||||||
url: "http://192.168.1.183:9696/ping",
|
}
|
||||||
method: "GET",
|
}
|
||||||
headers: {
|
}, methods: {},
|
||||||
Accept: "application/json",
|
async started(): Promise<any> {
|
||||||
"X-Api-Key": "163ef9a683874f65b53c7be87354b38b",
|
try {
|
||||||
},
|
this.torrentClient = new qBittorrentClient("http://192.168.1.183:8089", "admin", "adminadmin");
|
||||||
});
|
|
||||||
console.log(foo.data);
|
} catch (err) {
|
||||||
return true;
|
throw new MoleculerError(err, 500, "QBITTORRENT_CONNECTION_ERROR", {
|
||||||
},
|
data: err,
|
||||||
},
|
});
|
||||||
},
|
}
|
||||||
methods: {},
|
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,237 +1,45 @@
|
|||||||
import { readFileSync, writeFileSync } from "fs";
|
"use strict";
|
||||||
import { qBittorrentClient } from "@robertklep/qbittorrent";
|
import { Context, Service, ServiceBroker, ServiceSchema, Errors } from "moleculer";
|
||||||
import type { Context, ServiceBroker, ServiceSchema } from "moleculer";
|
const { MoleculerError } = require("moleculer").Errors;
|
||||||
import { Errors, Service } from "moleculer";
|
import axios from "axios";
|
||||||
import parseTorrent from "parse-torrent";
|
|
||||||
|
|
||||||
export default class QBittorrentService extends Service {
|
export default class ProwlarrService extends Service {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor(public broker: ServiceBroker, schema: ServiceSchema<{}> = { name: "qbittorrent" }) {
|
public constructor(
|
||||||
|
public broker: ServiceBroker,
|
||||||
|
schema: ServiceSchema<{}> = { name: "prowlarr" },
|
||||||
|
) {
|
||||||
super(broker);
|
super(broker);
|
||||||
this.parseServiceSchema({
|
this.parseServiceSchema({
|
||||||
name: "qbittorrent",
|
name: "prowlarr",
|
||||||
mixins: [],
|
mixins: [],
|
||||||
hooks: {},
|
hooks: {},
|
||||||
settings: {},
|
|
||||||
actions: {
|
actions: {
|
||||||
fetchQbittorrentCredentials: {
|
testConnection: {
|
||||||
rest: "GET /fetchQbittorrentCredentials",
|
rest: "GET /testConnection",
|
||||||
handler: async (ctx: Context<{}>) => {
|
|
||||||
return await 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;
|
|
||||||
}>,
|
|
||||||
) => {
|
|
||||||
const { username, password, hostname, port, protocol } = ctx.params;
|
|
||||||
|
|
||||||
this.meta = new qBittorrentClient(
|
|
||||||
`${protocol}://${hostname}:${port}`,
|
|
||||||
`${username}`,
|
|
||||||
`${password}`,
|
|
||||||
);
|
|
||||||
console.log(this.meta);
|
|
||||||
if (this.meta) {
|
|
||||||
return { success: true, message: "Logged in successfully" };
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
loginWithStoredCredentials: {
|
|
||||||
rest: "POST /loginWithStoredCredentials",
|
|
||||||
handler: async (ctx: Context<{}>) => {
|
handler: async (ctx: Context<{}>) => {
|
||||||
try {
|
try {
|
||||||
const result: any = await this.broker.call(
|
const result = await axios.request({
|
||||||
"qbittorrent.fetchQbittorrentCredentials",
|
url: `http://192.168.1.183:9696/api/v1/history`,
|
||||||
{},
|
method: `GET`,
|
||||||
);
|
headers: { Accept: "application/json" },
|
||||||
if (result !== undefined) {
|
params: {
|
||||||
const {
|
apikey: "163ef9a683874f65b53c7be87354b38b",
|
||||||
client: {
|
|
||||||
host: { username, password, hostname, port, protocol },
|
|
||||||
},
|
|
||||||
} = result;
|
|
||||||
|
|
||||||
const connection = await this.broker.call("qbittorrent.connect", {
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
hostname,
|
|
||||||
port,
|
|
||||||
protocol,
|
|
||||||
});
|
|
||||||
console.log("qbittorrent connection details:");
|
|
||||||
console.log(JSON.stringify(connection, null, 4));
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
return {
|
|
||||||
error: err,
|
|
||||||
message:
|
|
||||||
"Qbittorrent credentials not found, please configure them in Settings.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
getClientInfo: {
|
|
||||||
rest: "GET /getClientInfo",
|
|
||||||
handler: async (ctx: Context<{}>) => {
|
|
||||||
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
|
|
||||||
return {
|
|
||||||
buildInfo: await this.meta.app.buildInfo(),
|
|
||||||
version: await this.meta.app.version(),
|
|
||||||
webAPIVersion: await this.meta.app.webapiVersion(),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
|
||||||
addTorrent: {
|
|
||||||
rest: "POST /addTorrent",
|
|
||||||
handler: async (
|
|
||||||
ctx: Context<{
|
|
||||||
torrentToDownload: any;
|
|
||||||
comicObjectId: string;
|
|
||||||
}>,
|
|
||||||
) => {
|
|
||||||
try {
|
|
||||||
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
|
|
||||||
const { torrentToDownload, comicObjectId } = ctx.params;
|
|
||||||
console.log(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));
|
|
||||||
// Add the torrent to qbittorrent's queue, paused.
|
|
||||||
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,
|
|
||||||
});
|
|
||||||
const { name, infoHash, announce } = parseTorrent(
|
|
||||||
readFileSync("mithrandir.torrent"),
|
|
||||||
);
|
|
||||||
await this.broker.call("library.applyTorrentDownloadMetadata", {
|
|
||||||
name,
|
|
||||||
torrentToDownload,
|
|
||||||
comicObjectId,
|
|
||||||
announce,
|
|
||||||
infoHash,
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
result,
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
getTorrents: {
|
|
||||||
rest: "POST /getTorrents",
|
|
||||||
handler: async (ctx: Context<{}>) => {
|
|
||||||
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
|
|
||||||
return await this.meta.torrents.info();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
getTorrentProperties: {
|
|
||||||
rest: "POST /getTorrentProperties",
|
|
||||||
handler: async (ctx: Context<{ infoHashes: string[] }>) => {
|
|
||||||
try {
|
|
||||||
const { infoHashes } = ctx.params;
|
|
||||||
await this.broker.call("qbittorrent.loginWithStoredCredentials", {});
|
|
||||||
return await this.meta.torrents.info({
|
|
||||||
hashes: infoHashes,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.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
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
getTorrentRealTimeStats: {
|
|
||||||
rest: "POST /getTorrentRealTimeStats",
|
|
||||||
handler: async (
|
|
||||||
ctx: Context<{ infoHashes: { _id: string; infoHashes: string[] }[] }>,
|
|
||||||
) => {
|
|
||||||
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 = [];
|
|
||||||
|
|
||||||
infoHashes.forEach(({ _id, infoHashes }) => {
|
|
||||||
// Initialize an object to hold details for this _id
|
|
||||||
const details: any = [];
|
|
||||||
|
|
||||||
infoHashes.forEach((hash) => {
|
|
||||||
// Assuming 'data.torrents[hash]' retrieves the details for the hash
|
|
||||||
const torrent = data.torrents[hash];
|
|
||||||
if (torrent) {
|
|
||||||
details.push({
|
|
||||||
torrent,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// If you have details for this _id, add them to the main array
|
|
||||||
if (details.length > 0) {
|
|
||||||
torrentDetails.push({
|
|
||||||
_id,
|
|
||||||
details,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Update rid with the latest value if needed based on the response
|
console.log(result);
|
||||||
// Assuming `data.rid` contains the latest rid from the server
|
return result.data;
|
||||||
if (data.rid !== undefined) {
|
} catch(err) {
|
||||||
this.rid = data.rid;
|
console.log(err);
|
||||||
console.log(`rid is ${this.rid}`);
|
|
||||||
}
|
|
||||||
console.log(JSON.stringify(torrentDetails, null, 4));
|
|
||||||
return torrentDetails;
|
|
||||||
} catch (err) {
|
|
||||||
this.logger.error(err);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
determineDownloadApps: {
|
|
||||||
rest: "",
|
}, methods: {},
|
||||||
handler: async () => {
|
async started(): Promise<any> {
|
||||||
// 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
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
methods: {},
|
|
||||||
async started() {
|
|
||||||
console.log(`Initializing rid...`);
|
|
||||||
this.rid = 0;
|
|
||||||
console.log(`rid is ${this.rid}`);
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
tsconfig.eslint.json
Normal file
12
tsconfig.eslint.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"allowJs": true
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"./.*.cjs", // root commonjs files
|
||||||
|
"./.*.js", // root javascript config files
|
||||||
|
"**/*.js", // javascript files
|
||||||
|
"**/*.ts" // typescript files
|
||||||
|
]
|
||||||
|
}
|
||||||
117
tsconfig.json
117
tsconfig.json
@@ -1,18 +1,103 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||||
"esModuleInterop": true,
|
|
||||||
"noImplicitAny": true,
|
/* Projects */
|
||||||
"removeComments": true,
|
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
||||||
"preserveConstEnums": true,
|
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||||
"sourceMap": true,
|
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
||||||
"pretty": true,
|
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
||||||
"target": "es6",
|
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||||
"outDir": "dist"
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||||
},
|
|
||||||
"include": ["./**/*"],
|
/* Language and Environment */
|
||||||
"exclude": [
|
"target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||||
"node_modules/**/*",
|
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||||
"test"
|
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||||
]
|
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||||
|
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
||||||
|
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||||
|
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
||||||
|
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
||||||
|
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||||
|
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||||
|
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
||||||
|
|
||||||
|
/* Modules */
|
||||||
|
"module": "commonjs", /* Specify what module code is generated. */
|
||||||
|
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||||
|
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||||
|
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||||
|
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||||
|
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||||
|
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
||||||
|
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||||
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
|
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
||||||
|
// "resolveJsonModule": true, /* Enable importing .json files. */
|
||||||
|
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
||||||
|
|
||||||
|
/* JavaScript Support */
|
||||||
|
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
||||||
|
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
||||||
|
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
||||||
|
|
||||||
|
/* Emit */
|
||||||
|
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||||
|
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||||
|
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||||
|
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||||
|
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
||||||
|
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
||||||
|
// "removeComments": true, /* Disable emitting comments. */
|
||||||
|
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||||
|
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||||
|
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
||||||
|
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||||
|
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||||
|
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||||
|
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||||
|
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||||
|
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||||
|
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
||||||
|
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
||||||
|
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||||
|
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
||||||
|
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||||
|
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||||
|
|
||||||
|
/* Interop Constraints */
|
||||||
|
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||||
|
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||||
|
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||||
|
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||||
|
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
||||||
|
|
||||||
|
/* Type Checking */
|
||||||
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
|
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
||||||
|
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
||||||
|
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||||
|
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
||||||
|
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||||
|
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
||||||
|
"useUnknownInCatchVariables": false, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
||||||
|
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||||
|
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
||||||
|
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
||||||
|
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||||
|
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||||
|
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
||||||
|
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||||
|
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
||||||
|
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||||
|
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
||||||
|
|
||||||
|
/* Completeness */
|
||||||
|
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||||
|
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user