🔂 Recursive paginated call to fetch all volumes WIP

This commit is contained in:
2021-12-19 09:34:12 -08:00
parent 1c67e45a90
commit 269bdcb5ae
3 changed files with 423 additions and 29 deletions

View File

@@ -1,7 +1,7 @@
"use strict";
import { Service, ServiceBroker, Context } from "moleculer";
import axios from "axios";
import axios, { AxiosResponse } from "axios";
import { matchScorer } from "../utils/searchmatchscorer.utils";
const CV_BASE_URL = "https://comicvine.gamespot.com/api/";
console.log("KEYYYYYYYY", process.env.COMICVINE_API_KEY);
@@ -10,6 +10,7 @@ export default class ComicVineService extends Service {
super(broker);
this.parseServiceSchema({
name: "comicvine",
requestTimeout: 90000,
actions: {
fetchResource: {
rest: "/fetchresource",
@@ -40,7 +41,15 @@ export default class ComicVineService extends Service {
};
}>
): Promise<any> => {
const { format, sort, query, fieldList, limit, offset, resources } = ctx.params;
const {
format,
sort,
query,
fieldList,
limit,
offset,
resources,
} = ctx.params;
const response = await axios.request({
url:
CV_BASE_URL +
@@ -109,7 +118,9 @@ export default class ComicVineService extends Service {
) => {
const response = await axios.request({
url:
ctx.params.volumeURI + "?api_key=" + process.env.COMICVINE_API_KEY,
ctx.params.volumeURI +
"?api_key=" +
process.env.COMICVINE_API_KEY,
params: ctx.params.data,
headers: { Accept: "application/json" },
});
@@ -117,6 +128,71 @@ export default class ComicVineService extends Service {
return data;
},
},
volumeBasedSearch: {
rest: "POST /volumeBasedSearch",
params: {},
headers: { Accept: "application/json" },
bulkhead: {
enabled: true,
concurrency: 10,
maxQueueSize: 10,
},
handler: async (
ctx: Context<{
format: string;
sort: string;
query: string;
fieldList: string;
limit: number;
offset: number;
resources: string;
scorerConfiguration?: {
searchQuery: {
issue: object;
series: object;
};
rawFileDetails: object;
};
}>
) => {
const fo: any = [];
const foo = await this.fetchVolumesFromCV(
ctx.params,
fo
);
return foo;
},
},
},
methods: {
fetchVolumesFromCV: async (params, output: any[] = []) => {
let currentPage = parseInt(params.page, 10);
const response = await axios.request({
url:
CV_BASE_URL +
"search" +
"?api_key=" +
process.env.COMICVINE_API_KEY,
params,
headers: { Accept: "application/json" },
});
const { data } = response;
// 1. calculate total pages
const totalPages = Math.floor(
parseInt(data.number_of_total_results, 10) /
parseInt(params.limit, 10)
);
if (currentPage < totalPages) {
output.push(data.results);
currentPage += 1;
params.page = currentPage;
console.log(currentPage);
return await this.fetchVolumesFromCV(params, output);
} else {
return output;
}
},
},
});
}