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