🏗 Added the getIssuesForVolume endpoint

This commit is contained in:
2022-01-29 15:56:10 -08:00
parent 34266c9d48
commit 7273175958

View File

@@ -2,8 +2,8 @@
import { Service, ServiceBroker, Context } from "moleculer";
import axios from "axios";
import { isNil, isUndefined } from "lodash";
import { matchScorer, rankVolumes } from "../utils/searchmatchscorer.utils";
import { isNil } from "lodash";
const CV_BASE_URL = "https://comicvine.gamespot.com/api/";
console.log("ComicVine API Key: ", process.env.COMICVINE_API_KEY);
@@ -63,6 +63,39 @@ export default class ComicVineService extends Service {
return data;
},
},
getIssuesForVolume: {
rest: "POST /getIssuesForVolume",
params: {},
handler: async (
ctx: Context<{ comicObjectID: string }>
) => {
try {
const issues: any = [];
const comicBookDetails: any = await ctx.broker.call(
"import.getComicBookById",
{ id: ctx.params.comicObjectID }
);
if (!isUndefined(comicBookDetails)) {
const foo = comicBookDetails.sourcedMetadata.comicvine.volumeInformation.issues.map(
async (issue: any, idx: any) => {
const issueMetadata = await axios({
url: `${issue.api_detail_url}?api_key=${process.env.COMICVINE_API_KEY}`,
params: {
resources: "issues",
limit: "100",
format: "json",
},
});
return issueMetadata.data.results;
}
);
return Promise.all(foo);
}
} catch (error) {
throw error;
}
},
},
volumeBasedSearch: {
rest: "POST /volumeBasedSearch",
params: {},
@@ -95,31 +128,51 @@ export default class ComicVineService extends Service {
rawFileDetails: object;
}>
) => {
console.log("Searching against: ", ctx.params.scorerConfiguration.searchParams.searchTerms);
console.log(
"Searching against: ",
ctx.params.scorerConfiguration.searchParams
.searchTerms
);
const results: any = [];
const volumes = await this.fetchVolumesFromCV(
ctx.params,
results,
results
);
// 1. Run the current batch of volumes through the matcher
const potentialVolumeMatches = rankVolumes(volumes, ctx.params.scorerConfiguration);
const potentialVolumeMatches = rankVolumes(
volumes,
ctx.params.scorerConfiguration
);
// 2. Construct the filter string
// 2a. volume: 1111|2222|3333
let volumeIdString = "volume:";
potentialVolumeMatches.map((volumeId: string, idx: number) => {
if(idx >= potentialVolumeMatches.length - 1) {
volumeIdString += `${volumeId}`;
return volumeIdString;
potentialVolumeMatches.map(
(volumeId: string, idx: number) => {
if (idx >= potentialVolumeMatches.length - 1) {
volumeIdString += `${volumeId}`;
return volumeIdString;
}
volumeIdString += `${volumeId}|`;
}
volumeIdString += `${volumeId}|`;
});
);
// 2b. cover_date:2014-01-01|2016-12-31 for the issue year 2015
let coverDateFilter = "";
if(!isNil(ctx.params.scorerConfiguration.searchParams.searchTerms.year)) {
const issueYear = parseInt(ctx.params.scorerConfiguration.searchParams.searchTerms.year, 10);
coverDateFilter = `cover_date:${issueYear - 1}-01-01|${issueYear + 1}-12-31`;
if (
!isNil(
ctx.params.scorerConfiguration.searchParams
.searchTerms.year
)
) {
const issueYear = parseInt(
ctx.params.scorerConfiguration.searchParams
.searchTerms.year,
10
);
coverDateFilter = `cover_date:${
issueYear - 1
}-01-01|${issueYear + 1}-12-31`;
}
const filterString = `issue_number:${ctx.params.scorerConfiguration.searchParams.searchTerms.number},${volumeIdString},${coverDateFilter}`;
console.log(filterString);
@@ -132,23 +185,36 @@ export default class ComicVineService extends Service {
format: "json",
filter: filterString,
},
headers: {"User-Agent": "ThreeTwo"},
headers: { "User-Agent": "ThreeTwo" },
});
console.log(`Total issues matching the criteria: ${issueMatches.data.results.length}`);
console.log(
`Total issues matching the criteria: ${issueMatches.data.results.length}`
);
// 3. get volume information for the issue matches
if(issueMatches.data.results.length === 1) {
const volumeInformation = await this.broker.call("comicvine.getVolumes", { volumeURI: issueMatches.data.results[0].volume.api_detail_url });
issueMatches.data.results[0].volumeInformation = volumeInformation;
if (issueMatches.data.results.length === 1) {
const volumeInformation = await this.broker.call(
"comicvine.getVolumes",
{
volumeURI:
issueMatches.data.results[0].volume
.api_detail_url,
}
);
issueMatches.data.results[0].volumeInformation =
volumeInformation;
return issueMatches.data;
}
const finalMatches = issueMatches.data.results.map(async (issue: any) => {
const volumeDetails = await this.broker.call("comicvine.getVolumes", { volumeURI: issue.volume.api_detail_url });
issue.volumeInformation = volumeDetails;
return issue;
});
const finalMatches = issueMatches.data.results.map(
async (issue: any) => {
const volumeDetails = await this.broker.call(
"comicvine.getVolumes",
{ volumeURI: issue.volume.api_detail_url }
);
issue.volumeInformation = volumeDetails;
return issue;
}
);
return Promise.all(finalMatches);
},
},
},
@@ -158,8 +224,7 @@ export default class ComicVineService extends Service {
const response = await axios.request({
url: `https://comicvine.gamespot.com/api/search?api_key=${process.env.COMICVINE_API_KEY}`,
params,
headers: {"User-Agent": "ThreeTwo"},
headers: { "User-Agent": "ThreeTwo" },
});
const { data } = response;
@@ -169,7 +234,7 @@ export default class ComicVineService extends Service {
parseInt(params.limit, 10)
);
// 1a. If total results are <= 100, just return the results
if(parseInt(data.number_of_total_results, 10) <= 100 ) {
if (parseInt(data.number_of_total_results, 10) <= 100) {
return [...data.results];
}
// 1b. If not, recursively call fetchVolumesFromCV till we have fetched all pages
@@ -177,7 +242,11 @@ export default class ComicVineService extends Service {
output.push(...data.results);
currentPage += 1;
params.page = currentPage;
console.log(`Fetching results for page ${currentPage} (of ${totalPages + 1})...`);
console.log(
`Fetching results for page ${currentPage} (of ${
totalPages + 1
})...`
);
return await this.fetchVolumesFromCV(params, output);
} else {
return [...output];