🦸🏻 Added metron service

This commit is contained in:
2022-01-04 07:39:00 -08:00
parent ba31650543
commit 4d1b4ceb71
3 changed files with 51 additions and 81 deletions

View File

@@ -1,92 +1,17 @@
"use strict"; "use strict";
import qs from "querystring";
import https from "https";
import { Service, ServiceBroker, Context } from "moleculer"; import { Service, ServiceBroker, Context } from "moleculer";
import axios from "axios"; import axios from "axios";
import {
cacheAdapterEnhancer,
throttleAdapterEnhancer,
} from "axios-extensions";
import { isNil } from "lodash";
import { matchScorer, rankVolumes } from "../utils/searchmatchscorer.utils"; import { matchScorer, rankVolumes } from "../utils/searchmatchscorer.utils";
const CV_BASE_URL = "https://comicvine.gamespot.com/api/"; const CV_BASE_URL = "https://comicvine.gamespot.com/api/";
console.log("KEYYYYYYYY", process.env.COMICVINE_API_KEY); console.log("ComicVine API Key: ", process.env.COMICVINE_API_KEY);
export default class ComicVineService extends Service { export default class ComicVineService extends Service {
public constructor(public broker: ServiceBroker) { public constructor(public broker: ServiceBroker) {
super(broker); super(broker);
this.parseServiceSchema({ this.parseServiceSchema({
name: "comicvine", name: "comicvine",
actions: { actions: {
fetchResource: {
rest: "/fetchresource",
params: {
format: { type: "string", optional: false },
sort: { type: "string", optional: true },
query: { type: "string", optional: false },
fieldList: { type: "string", optional: true },
limit: { type: "string", optional: false },
offset: { type: "string", optional: false },
resources: { type: "string", optional: false },
},
handler: async (
ctx: Context<{
format: string;
sort: string;
query: string;
fieldList: string;
limit: string;
offset: string;
resources: string;
scorerConfiguration: {
searchQuery: {
issue: object;
series: object;
};
rawFileDetails: object;
};
}>
): Promise<any> => {
const {
format,
sort,
query,
fieldList,
limit,
offset,
resources,
} = ctx.params;
const response = await axios.request({
url:
CV_BASE_URL +
"search" +
"?api_key=" +
process.env.COMICVINE_API_KEY,
params: {
format,
sort,
query,
fieldList,
limit,
offset,
resources,
},
transformResponse: r => {
const matches = JSON.parse(r);
return matchScorer(
matches.results,
ctx.params.scorerConfiguration.searchQuery,
ctx.params.scorerConfiguration
.rawFileDetails
);
},
headers: { Accept: "application/json" },
});
const { data } = response;
return data;
},
},
search: { search: {
rest: "/search", rest: "/search",
params: {}, params: {},
@@ -205,7 +130,7 @@ export default class ComicVineService extends Service {
}, },
headers: {"User-Agent": "ThreeTwo"}, headers: {"User-Agent": "ThreeTwo"},
}); });
console.log("YAHAHAHA", 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("comicvine.getVolumes", { volumeURI: issueMatches.data.results[0].volume.api_detail_url });
@@ -234,20 +159,21 @@ export default class ComicVineService extends Service {
}); });
const { data } = response; const { data } = response;
// 1. calculate total pages // 1. Calculate total pages
const totalPages = Math.floor( const totalPages = Math.floor(
parseInt(data.number_of_total_results, 10) / parseInt(data.number_of_total_results, 10) /
parseInt(params.limit, 10) parseInt(params.limit, 10)
); );
console.log(totalPages); // 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
if (currentPage <= totalPages) { if (currentPage <= totalPages) {
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}...`); 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];

View File

@@ -0,0 +1,44 @@
"use strict";
import { Service, ServiceBroker, Context } from "moleculer";
import axios from "axios";
const METRON_BASE_URL = "https://metron.cloud/api/";
export default class MetronService extends Service {
public constructor(public broker: ServiceBroker) {
super(broker);
this.parseServiceSchema({
name: "metron",
actions: {
fetchResource: {
rest: "POST /fetchResource",
params: {},
handler: async (
ctx: Context<{
resource: string;
method: string;
query: {
name: string;
page: number;
};
}>
) => {
console.log(ctx.params);
const results = await axios({
method: "GET",
url: `https://metron.cloud/api/${ctx.params.resource}`,
params: {
name: ctx.params.query.name,
page: ctx.params.query.page,
},
});
return results.data;
},
},
},
methods: {},
});
}
}

View File

@@ -133,7 +133,7 @@ export const rankVolumes = (volumes: any, scorerConfiguration: any) => {
volumeMatchScore += 3; volumeMatchScore += 3;
} }
} }
if(issueNameMatchScore > 0.2 && volumeMatchScore > 2) { if(issueNameMatchScore > 0.5 && volumeMatchScore > 2) {
console.log(`Found a match for criteria, volume ID: ${volume.id}`); console.log(`Found a match for criteria, volume ID: ${volume.id}`);
return volume.id; return volume.id;
} }