🏗 Added the getIssuesForVolume endpoint
This commit is contained in:
@@ -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(
|
||||||
|
(volumeId: string, idx: number) => {
|
||||||
if (idx >= potentialVolumeMatches.length - 1) {
|
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);
|
||||||
@@ -134,21 +187,34 @@ export default class ComicVineService extends Service {
|
|||||||
},
|
},
|
||||||
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);
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -159,7 +225,6 @@ export default class ComicVineService extends Service {
|
|||||||
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;
|
||||||
@@ -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];
|
||||||
|
|||||||
Reference in New Issue
Block a user