🧃 Moving matchscorer to this service
This commit is contained in:
11073
package-lock.json
generated
11073
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -20,6 +20,8 @@
|
||||
],
|
||||
"author": "",
|
||||
"devDependencies": {
|
||||
"@types/lodash": "^4.14.171",
|
||||
"@types/string-similarity": "^4.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^2.26.0",
|
||||
"@typescript-eslint/parser": "^2.26.0",
|
||||
"axios": "^0.21.1",
|
||||
@@ -37,9 +39,13 @@
|
||||
"@types/jest": "^25.1.4",
|
||||
"@types/mkdirp": "^1.0.0",
|
||||
"@types/node": "^13.9.8",
|
||||
"imghash": "^0.0.9",
|
||||
"leven": "^3.1.0",
|
||||
"lodash": "^4.17.21",
|
||||
"moleculer": "^0.14.0",
|
||||
"moleculer-web": "^0.9.0",
|
||||
"nats": "^1.3.2",
|
||||
"string-similarity": "^4.0.4",
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"engines": {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { Service, ServiceBroker, Context } from "moleculer";
|
||||
import axios from "axios";
|
||||
import {matchScorer} from "../utils/searchmatchscorer.utils";
|
||||
const CV_BASE_URL = "https://comicvine.gamespot.com/api/";
|
||||
const CV_API_KEY = "a5fa0663683df8145a85d694b5da4b87e1c92c69";
|
||||
|
||||
@@ -40,13 +41,24 @@ export default class GreeterService extends Service {
|
||||
"?api_key=" +
|
||||
CV_API_KEY,
|
||||
params: ctx.params,
|
||||
transformResponse: (r) => JSON.parse(r),
|
||||
transformResponse: r => JSON.parse(r),
|
||||
headers: { Accept: "application/json" },
|
||||
});
|
||||
const { data } = response;
|
||||
return data;
|
||||
},
|
||||
},
|
||||
getScoredComicVineMatches: {
|
||||
rest: "/getscoredcomicvinematches",
|
||||
params: {},
|
||||
handler: async (
|
||||
ctx: Context<{
|
||||
|
||||
}>
|
||||
): Promise<any> => {
|
||||
return matchScorer()
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
102
utils/searchmatchscorer.utils.ts
Normal file
102
utils/searchmatchscorer.utils.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2015 Rishi Ghan
|
||||
*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Rishi Ghan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Revision History:
|
||||
* Initial: 2021/07/29 Rishi Ghan
|
||||
*/
|
||||
|
||||
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 leven from "leven";
|
||||
|
||||
const imghash = require("imghash");
|
||||
|
||||
export const matchScorer = (searchMatches, searchQuery, rawFileDetails) => {
|
||||
// 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: ++)
|
||||
// 4. Check if issue covers hash match strongly (score: +++)
|
||||
// 5. Check if issue year matches strongly (score: +)
|
||||
|
||||
each(searchMatches, (match, idx) => {
|
||||
// Check for the issue name match
|
||||
|
||||
if (
|
||||
!isNull(searchQuery.issue.searchParams.searchTerms.name) &&
|
||||
!isNull(match.name)
|
||||
) {
|
||||
const issueNameScore = stringSimilarity.compareTwoStrings(
|
||||
searchQuery.issue.searchParams.searchTerms.name,
|
||||
match.name
|
||||
);
|
||||
match.score = issueNameScore;
|
||||
}
|
||||
|
||||
// Issue number matches
|
||||
if (
|
||||
!isNull(searchQuery.issue.searchParams.searchTerms.number) &&
|
||||
!isNull(match.issue_number)
|
||||
) {
|
||||
if (
|
||||
parseInt(
|
||||
searchQuery.issue.searchParams.searchTerms.number,
|
||||
10
|
||||
) === parseInt(match.issue_number, 10)
|
||||
) {
|
||||
match.score += 1;
|
||||
}
|
||||
}
|
||||
// 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}`)
|
||||
);
|
||||
fileStream.on("finish", async () => {
|
||||
const hash1 = await imghash.hash(fileName);
|
||||
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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return match;
|
||||
});
|
||||
|
||||
return searchMatches;
|
||||
};
|
||||
Reference in New Issue
Block a user