✂️ Refactoring
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -66,3 +66,4 @@ typings/
|
||||
# Don't track transpiled files
|
||||
dist/
|
||||
.vscode
|
||||
userdata
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
import { createWriteStream } from "fs";
|
||||
import path from "path";
|
||||
import https from "https";
|
||||
import { Service, ServiceBroker, Context } from "moleculer";
|
||||
import axios from "axios";
|
||||
import {matchScorer} from "../utils/searchmatchscorer.utils";
|
||||
import leven from "leven";
|
||||
import { matchScorer } from "../utils/searchmatchscorer.utils";
|
||||
const CV_BASE_URL = "https://comicvine.gamespot.com/api/";
|
||||
const CV_API_KEY = "a5fa0663683df8145a85d694b5da4b87e1c92c69";
|
||||
|
||||
@@ -32,6 +36,13 @@ export default class GreeterService extends Service {
|
||||
limit: string;
|
||||
offset: string;
|
||||
resources: string;
|
||||
scorerData: {
|
||||
searchQuery: {
|
||||
issue: object;
|
||||
series: object;
|
||||
};
|
||||
rawFileDetails: object;
|
||||
};
|
||||
}>
|
||||
): Promise<any> => {
|
||||
const response = await axios.request({
|
||||
@@ -41,24 +52,20 @@ export default class GreeterService extends Service {
|
||||
"?api_key=" +
|
||||
CV_API_KEY,
|
||||
params: ctx.params,
|
||||
transformResponse: r => JSON.parse(r),
|
||||
transformResponse: r => {
|
||||
const matches = JSON.parse(r);
|
||||
return matchScorer(
|
||||
matches.results,
|
||||
ctx.params.scorerData.searchQuery,
|
||||
ctx.params.scorerData.rawFileDetails
|
||||
);
|
||||
},
|
||||
headers: { Accept: "application/json" },
|
||||
});
|
||||
const { data } = response;
|
||||
return data;
|
||||
},
|
||||
},
|
||||
getScoredComicVineMatches: {
|
||||
rest: "/getscoredcomicvinematches",
|
||||
params: {},
|
||||
handler: async (
|
||||
ctx: Context<{
|
||||
|
||||
}>
|
||||
): Promise<any> => {
|
||||
return matchScorer()
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -35,12 +35,12 @@ import { createWriteStream } from "fs";
|
||||
import path from "path";
|
||||
import https from "https";
|
||||
import stringSimilarity from "string-similarity";
|
||||
import { each, map, isUndefined, isNull, assign } from "lodash";
|
||||
import { each, isNil, isNull, isUndefined } from "lodash";
|
||||
import leven from "leven";
|
||||
|
||||
const imghash = require("imghash");
|
||||
|
||||
export const matchScorer = (searchMatches, searchQuery, rawFileDetails) => {
|
||||
export const matchScorer = (searchMatches: any , searchQuery: any, rawFileDetails: any) => {
|
||||
// 1. Check if it exists in the db (score: 0)
|
||||
// 2. Check if issue name matches strongly (score: ++)
|
||||
// 3. Check if issue number matches strongly (score: ++)
|
||||
@@ -48,11 +48,12 @@ export const matchScorer = (searchMatches, searchQuery, rawFileDetails) => {
|
||||
// 5. Check if issue year matches strongly (score: +)
|
||||
|
||||
each(searchMatches, (match, idx) => {
|
||||
match.score = 0;
|
||||
// Check for the issue name match
|
||||
|
||||
if (
|
||||
!isNull(searchQuery.issue.searchParams.searchTerms.name) &&
|
||||
!isNull(match.name)
|
||||
!isNil(searchQuery.issue.searchParams.searchTerms.name) &&
|
||||
!isNil(match.name)
|
||||
) {
|
||||
const issueNameScore = stringSimilarity.compareTwoStrings(
|
||||
searchQuery.issue.searchParams.searchTerms.name,
|
||||
@@ -63,8 +64,8 @@ export const matchScorer = (searchMatches, searchQuery, rawFileDetails) => {
|
||||
|
||||
// Issue number matches
|
||||
if (
|
||||
!isNull(searchQuery.issue.searchParams.searchTerms.number) &&
|
||||
!isNull(match.issue_number)
|
||||
!isNil(searchQuery.issue.searchParams.searchTerms.number) &&
|
||||
!isNil(match.issue_number)
|
||||
) {
|
||||
if (
|
||||
parseInt(
|
||||
@@ -76,27 +77,39 @@ export const matchScorer = (searchMatches, searchQuery, rawFileDetails) => {
|
||||
}
|
||||
}
|
||||
// Cover image hash match
|
||||
const fileName = match.id + "_" + rawFileDetails.name;
|
||||
https.get(match.image.small_url, response => {
|
||||
const fileStream = response.pipe(
|
||||
createWriteStream(`./userdata/temporary/${fileName}`)
|
||||
);
|
||||
const fileName = match.id + "_" + rawFileDetails.name + ".jpg";
|
||||
|
||||
const file = createWriteStream(`./userdata/temporary/${fileName}`);
|
||||
|
||||
https.get(match.image.small_url, (response) => {
|
||||
const fileStream = response.pipe(file);
|
||||
fileStream.on("finish", async () => {
|
||||
const hash1 = await imghash.hash(fileName);
|
||||
const hash1 = await imghash.hash(
|
||||
path.resolve(rawFileDetails.path)
|
||||
);
|
||||
const hash2 = await imghash.hash(
|
||||
path.resolve(`./userdata/temporary/${fileName}`)
|
||||
);
|
||||
const levenshteinDistance = leven(hash1, hash2);
|
||||
if (levenshteinDistance === 0) {
|
||||
match.score += 4;
|
||||
} else {
|
||||
match.score -= 4;
|
||||
}
|
||||
});
|
||||
});
|
||||
if (!isUndefined(hash1) && !isUndefined(hash2)) {
|
||||
const levenshteinDistance = leven(hash1, hash2);
|
||||
if (levenshteinDistance === 0) {
|
||||
match.score += 4;
|
||||
} else if(levenshteinDistance > 0 && levenshteinDistance <= 2){
|
||||
match.score += 2;
|
||||
} else {
|
||||
match.score -= 4;
|
||||
}
|
||||
} else {
|
||||
console.log("Couldn't calculate image hashes");
|
||||
}
|
||||
console.log("MATCH SCORE inside:", match.score);
|
||||
|
||||
return match;
|
||||
});
|
||||
}).end();
|
||||
console.log("MATCH SCORE OUTSIDE:", match.score)
|
||||
});
|
||||
|
||||
|
||||
return searchMatches;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user