🔧 getBundles endpoint WIP

This commit is contained in:
2024-10-21 18:04:16 -04:00
parent 55e0ce6d36
commit 68c2dacff4
3 changed files with 414 additions and 557 deletions

695
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -33,13 +33,7 @@ SOFTWARE.
"use strict"; "use strict";
import { isNil } from "lodash"; import { isNil } from "lodash";
import { import { Context, Service, ServiceBroker, ServiceSchema, Errors } from "moleculer";
Context,
Service,
ServiceBroker,
ServiceSchema,
Errors,
} from "moleculer";
import { DbMixin } from "../mixins/db.mixin"; import { DbMixin } from "../mixins/db.mixin";
import Comic from "../models/comic.model"; import Comic from "../models/comic.model";
import { walkFolder, getSizeOfDirectory } from "../utils/file.utils"; import { walkFolder, getSizeOfDirectory } from "../utils/file.utils";
@@ -133,8 +127,7 @@ export default class ImportService extends Service {
}); });
// Determine source where the comic was added from // Determine source where the comic was added from
// and gather identifying information about it // and gather identifying information about it
const sourceName = const sourceName = referenceComicObject[0].acquisition.source.name;
referenceComicObject[0].acquisition.source.name;
const { sourcedMetadata } = referenceComicObject[0]; const { sourcedMetadata } = referenceComicObject[0];
const filePath = `${COMICS_DIRECTORY}/${ctx.params.bundle.data.name}`; const filePath = `${COMICS_DIRECTORY}/${ctx.params.bundle.data.name}`;
@@ -178,14 +171,8 @@ export default class ImportService extends Service {
// 1.1 Filter on .cb* extensions // 1.1 Filter on .cb* extensions
.pipe( .pipe(
through2.obj(function (item, enc, next) { through2.obj(function (item, enc, next) {
let fileExtension = path.extname( let fileExtension = path.extname(item.path);
item.path if ([".cbz", ".cbr", ".cb7"].includes(fileExtension)) {
);
if (
[".cbz", ".cbr", ".cb7"].includes(
fileExtension
)
) {
this.push(item); this.push(item);
} }
next(); next();
@@ -194,10 +181,7 @@ export default class ImportService extends Service {
// 1.2 Pipe filtered results to the next step // 1.2 Pipe filtered results to the next step
// Enqueue the job in the queue // Enqueue the job in the queue
.on("data", async (item) => { .on("data", async (item) => {
console.info( console.info("Found a file at path: %s", item.path);
"Found a file at path: %s",
item.path
);
let comicExists = await Comic.exists({ let comicExists = await Comic.exists({
"rawFileDetails.name": `${path.basename( "rawFileDetails.name": `${path.basename(
item.path, item.path,
@@ -206,14 +190,8 @@ export default class ImportService extends Service {
}); });
if (!comicExists) { if (!comicExists) {
// 2.1 Reset the job counters in Redis // 2.1 Reset the job counters in Redis
await pubClient.set( await pubClient.set("completedJobCount", 0);
"completedJobCount", await pubClient.set("failedJobCount", 0);
0
);
await pubClient.set(
"failedJobCount",
0
);
// 2.2 Send the extraction job to the queue // 2.2 Send the extraction job to the queue
this.broker.call("jobqueue.enqueue", { this.broker.call("jobqueue.enqueue", {
fileObject: { fileObject: {
@@ -225,9 +203,7 @@ export default class ImportService extends Service {
action: "enqueue.async", action: "enqueue.async",
}); });
} else { } else {
console.log( console.log("Comic already exists in the library.");
"Comic already exists in the library."
);
} }
}) })
.on("end", () => { .on("end", () => {
@@ -272,19 +248,13 @@ export default class ImportService extends Service {
}> }>
) { ) {
try { try {
console.log( console.log(JSON.stringify(ctx.params.payload, null, 4));
JSON.stringify(ctx.params.payload, null, 4)
);
const { payload } = ctx.params; const { payload } = ctx.params;
const { wanted } = payload; const { wanted } = payload;
console.log("Saving to Mongo..."); console.log("Saving to Mongo...");
if ( if (!wanted || !wanted.volume || !wanted.volume.id) {
!wanted ||
!wanted.volume ||
!wanted.volume.id
) {
console.log( console.log(
"No valid identifier for upsert. Attempting to create a new document with minimal data..." "No valid identifier for upsert. Attempting to create a new document with minimal data..."
); );
@@ -329,11 +299,7 @@ export default class ImportService extends Service {
new: true, new: true,
}; };
const result = await Comic.findOneAndUpdate( const result = await Comic.findOneAndUpdate(condition, update, options);
condition,
update,
options
);
console.log( console.log(
"Operation completed. Document updated or inserted:", "Operation completed. Document updated or inserted:",
result result
@@ -346,10 +312,7 @@ export default class ImportService extends Service {
}; };
} catch (error) { } catch (error) {
console.log(error); console.log(error);
throw new Errors.MoleculerError( throw new Errors.MoleculerError("Operation failed.", 500);
"Operation failed.",
500
);
} }
}, },
}, },
@@ -389,9 +352,7 @@ export default class ImportService extends Service {
) { ) {
// 1. Find mongo object by id // 1. Find mongo object by id
// 2. Import payload into sourcedMetadata.comicvine // 2. Import payload into sourcedMetadata.comicvine
const comicObjectId = new ObjectId( const comicObjectId = new ObjectId(ctx.params.comicObjectId);
ctx.params.comicObjectId
);
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let volumeDetails = {}; let volumeDetails = {};
@@ -400,18 +361,15 @@ export default class ImportService extends Service {
const volumeDetails = await this.broker.call( const volumeDetails = await this.broker.call(
"comicvine.getVolumes", "comicvine.getVolumes",
{ {
volumeURI: volumeURI: matchedResult.volume.api_detail_url,
matchedResult.volume.api_detail_url,
} }
); );
matchedResult.volumeInformation = matchedResult.volumeInformation = volumeDetails.results;
volumeDetails.results;
Comic.findByIdAndUpdate( Comic.findByIdAndUpdate(
comicObjectId, comicObjectId,
{ {
$set: { $set: {
"sourcedMetadata.comicvine": "sourcedMetadata.comicvine": matchedResult,
matchedResult,
}, },
}, },
{ new: true }, { new: true },
@@ -442,9 +400,7 @@ export default class ImportService extends Service {
}> }>
) { ) {
console.log(JSON.stringify(ctx.params, null, 2)); console.log(JSON.stringify(ctx.params, null, 2));
const comicObjectId = new ObjectId( const comicObjectId = new ObjectId(ctx.params.comicObjectId);
ctx.params.comicObjectId
);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Comic.findByIdAndUpdate( Comic.findByIdAndUpdate(
@@ -482,13 +438,8 @@ export default class ImportService extends Service {
announce: [String]; announce: [String];
}> }>
) => { ) => {
const { const { name, torrentToDownload, comicObjectId, announce, infoHash } =
name, ctx.params;
torrentToDownload,
comicObjectId,
announce,
infoHash,
} = ctx.params;
console.log(JSON.stringify(ctx.params, null, 4)); console.log(JSON.stringify(ctx.params, null, 4));
try { try {
return await Comic.findByIdAndUpdate( return await Comic.findByIdAndUpdate(
@@ -552,7 +503,6 @@ export default class ImportService extends Service {
async handler(ctx: Context<{ id: string }>) { async handler(ctx: Context<{ id: string }>) {
console.log(ctx.params.id); console.log(ctx.params.id);
return await Comic.findById(new ObjectId(ctx.params.id)); return await Comic.findById(new ObjectId(ctx.params.id));
}, },
}, },
getComicBooksByIds: { getComicBooksByIds: {
@@ -560,9 +510,7 @@ export default class ImportService extends Service {
params: { ids: "array" }, params: { ids: "array" },
handler: async (ctx: Context<{ ids: [string] }>) => { handler: async (ctx: Context<{ ids: [string] }>) => {
console.log(ctx.params.ids); console.log(ctx.params.ids);
const queryIds = ctx.params.ids.map( const queryIds = ctx.params.ids.map((id) => new ObjectId(id));
(id) => new ObjectId(id)
);
return await Comic.find({ return await Comic.find({
_id: { _id: {
$in: queryIds, $in: queryIds,
@@ -578,8 +526,7 @@ export default class ImportService extends Service {
const volumes = await Comic.aggregate([ const volumes = await Comic.aggregate([
{ {
$project: { $project: {
volumeInfo: volumeInfo: "$sourcedMetadata.comicvine.volumeInformation",
"$sourcedMetadata.comicvine.volumeInformation",
}, },
}, },
{ {
@@ -625,52 +572,46 @@ export default class ImportService extends Service {
const { queryObjects } = ctx.params; const { queryObjects } = ctx.params;
// construct the query for ElasticSearch // construct the query for ElasticSearch
let elasticSearchQuery = {}; let elasticSearchQuery = {};
const elasticSearchQueries = queryObjects.map( const elasticSearchQueries = queryObjects.map((queryObject) => {
(queryObject) => { console.log("Volume: ", queryObject.volumeName);
console.log("Volume: ", queryObject.volumeName); console.log("Issue: ", queryObject.issueName);
console.log("Issue: ", queryObject.issueName); if (queryObject.issueName === null) {
if (queryObject.issueName === null) { queryObject.issueName = "";
queryObject.issueName = "";
}
if (queryObject.volumeName === null) {
queryObject.volumeName = "";
}
elasticSearchQuery = {
bool: {
must: [
{
match_phrase: {
"rawFileDetails.name":
queryObject.volumeName,
},
},
{
term: {
"inferredMetadata.issue.number":
parseInt(
queryObject.issueNumber,
10
),
},
},
],
},
};
return [
{
index: "comics",
search_type: "dfs_query_then_fetch",
},
{
query: elasticSearchQuery,
},
];
} }
); if (queryObject.volumeName === null) {
console.log( queryObject.volumeName = "";
JSON.stringify(elasticSearchQueries, null, 2) }
); elasticSearchQuery = {
bool: {
must: [
{
match_phrase: {
"rawFileDetails.name": queryObject.volumeName,
},
},
{
term: {
"inferredMetadata.issue.number": parseInt(
queryObject.issueNumber,
10
),
},
},
],
},
};
return [
{
index: "comics",
search_type: "dfs_query_then_fetch",
},
{
query: elasticSearchQuery,
},
];
});
console.log(JSON.stringify(elasticSearchQueries, null, 2));
return await ctx.broker.call("search.searchComic", { return await ctx.broker.call("search.searchComic", {
elasticSearchQueries, elasticSearchQueries,
@@ -683,10 +624,11 @@ export default class ImportService extends Service {
rest: "GET /libraryStatistics", rest: "GET /libraryStatistics",
params: {}, params: {},
handler: async (ctx: Context<{}>) => { handler: async (ctx: Context<{}>) => {
const comicDirectorySize = await getSizeOfDirectory( const comicDirectorySize = await getSizeOfDirectory(COMICS_DIRECTORY, [
COMICS_DIRECTORY, ".cbz",
[".cbz", ".cbr", ".cb7"] ".cbr",
); ".cb7",
]);
const totalCount = await Comic.countDocuments({}); const totalCount = await Comic.countDocuments({});
const statistics = await Comic.aggregate([ const statistics = await Comic.aggregate([
{ {
@@ -695,11 +637,7 @@ export default class ImportService extends Service {
{ {
$match: { $match: {
"rawFileDetails.extension": { "rawFileDetails.extension": {
$in: [ $in: [".cbr", ".cbz", ".cb7"],
".cbr",
".cbz",
".cb7",
],
}, },
}, },
}, },
@@ -713,10 +651,9 @@ export default class ImportService extends Service {
issues: [ issues: [
{ {
$match: { $match: {
"sourcedMetadata.comicvine.volumeInformation": "sourcedMetadata.comicvine.volumeInformation": {
{ $gt: {},
$gt: {}, },
},
}, },
}, },
{ {
@@ -770,6 +707,39 @@ export default class ImportService extends Service {
}; };
}, },
}, },
getBundles: {
rest: "POST /getBundles",
params: {},
handler: async (
ctx: Context<{
comicObjectId: string;
config: any;
}>
) => {
// 1. Get the comic object Id
console.log("ala re lala");
console.log(ctx.params);
const { config } = ctx.params;
const comicObject = await Comic.findById(
new ObjectId(ctx.params.comicObjectId)
);
console.log(JSON.stringify(comicObject, null, 4));
if (comicObject) {
const foo = comicObject.acquisition.directconnect.downloads.map(
async (bundle) => {
// make the call to get the bundles from AirDC++ using the bundleId
return await this.broker.call("socket.getBundles", {
bundleId: bundle.id,
config,
});
}
);
console.log(foo);
}
return [];
},
},
flushDB: { flushDB: {
rest: "POST /flushDB", rest: "POST /flushDB",
@@ -779,23 +749,16 @@ export default class ImportService extends Service {
.drop() .drop()
.then(async (data) => { .then(async (data) => {
console.info(data); console.info(data);
const coversFolderDeleteResult = const coversFolderDeleteResult = fsExtra.emptyDirSync(
fsExtra.emptyDirSync( path.resolve(`${USERDATA_DIRECTORY}/covers`)
path.resolve( );
`${USERDATA_DIRECTORY}/covers` const expandedFolderDeleteResult = fsExtra.emptyDirSync(
) path.resolve(`${USERDATA_DIRECTORY}/expanded`)
); );
const expandedFolderDeleteResult = const eSIndicesDeleteResult = await ctx.broker.call(
fsExtra.emptyDirSync( "search.deleteElasticSearchIndices",
path.resolve( {}
`${USERDATA_DIRECTORY}/expanded` );
)
);
const eSIndicesDeleteResult =
await ctx.broker.call(
"search.deleteElasticSearchIndices",
{}
);
return { return {
data, data,
coversFolderDeleteResult, coversFolderDeleteResult,

View File

@@ -105,14 +105,15 @@ export default class SocketService extends Service {
// ); // );
}, },
// AirDCPP Socket actions // AirDCPP Socket actions
// the way this method is called has some unnecessary indirection
getBundles: { getBundles: {
async handler( rest: "GET /getBundles",
ctx: Context<{ handler: async (ctx: Context<{ config: any; bundleId: string }>) => {
comicObjectId: string; const { config, bundleId } = ctx.params;
}> console.log(config);
) { const ADCPPSocket = new AirDCPPSocket(config);
// get bundles bitch
// 1. Get the comic object Id return await ADCPPSocket.get(`queue/bundles/${bundleId}`);
}, },
}, },
search: { search: {