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