From 80aaec2f543a49afcf5b48542403890d99f9882f Mon Sep 17 00:00:00 2001 From: Leslie-Wong-H <79917148leslie@gmail.com> Date: Fri, 11 Aug 2023 07:18:48 +0800 Subject: [PATCH 1/2] Implement shotit-api duration response --- src/search.js | 6 ++++-- src/search.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/search.js b/src/search.js index c24a6ce..4dae0f1 100644 --- a/src/search.js +++ b/src/search.js @@ -346,7 +346,7 @@ export default async (req, res) => { result = result // .reduce((list, { d, hash_id }) => { - .reduce((list, { score: d, hash_id }) => { + .reduce((list, { score: d, hash_id, duration }) => { // merge nearby results within 5 seconds in the same filename const imdb_id = String(hash_id.split("/")[0]); const filename = hash_id.split("/")[1]; @@ -362,6 +362,7 @@ export default async (req, res) => { imdb_id, filename, t, + duration, from: t, to: t, d, @@ -380,7 +381,7 @@ export default async (req, res) => { const window = 60 * 60; // 3600 seconds const now = ((Date.now() / 1000 / window) | 0) * window + window; - result = result.map(({ imdb_id, filename, t, from, to, d }) => { + result = result.map(({ imdb_id, filename, t, duration, from, to, d }) => { const mid = from + (to - from) / 2; const videoToken = crypto .createHash("sha1") @@ -397,6 +398,7 @@ export default async (req, res) => { imdb: imdb_id, filename, episode: aniep(filename), + duration, from, to, // similarity: (100 - d) / 100, diff --git a/src/search.test.js b/src/search.test.js index ae5973f..628e51e 100644 --- a/src/search.test.js +++ b/src/search.test.js @@ -104,6 +104,11 @@ beforeAll(async () => { max_length: 500, description: "${imdbID}/${fileName}/${time}", }, + { + name: "duration", + data_type: DataType.Float, + description: "Video duration of the given video file", + }, { name: "primary_key", data_type: DataType.Int64, @@ -149,6 +154,7 @@ beforeAll(async () => { 0.07166414845048348, 0.14825548206146674, 0.10861803715423188, 0.09339655442625933, 0.11284133872037455, 0.15155493641001572, 0.17421118960338527, 0.1372573008996369, ], + duration: 1422.01, primary_key: 3694, }, { @@ -180,6 +186,7 @@ beforeAll(async () => { 0.07166414845048348, 0.14825548206146674, 0.10861803715423188, 0.09339655442625933, 0.11284133872037455, 0.15155493641001572, 0.17421118960338527, 0.1372573008996369, ], + duration: 1422.01, primary_key: 3695, }, ], @@ -236,6 +243,7 @@ describe("without API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -243,6 +251,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -269,6 +278,7 @@ describe("without API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -276,6 +286,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); await fs.remove("32B15UXxymfSMwKGTObY5e.jpg"); }, @@ -303,6 +314,7 @@ describe("without API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -310,6 +322,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); await fs.remove("32B15UXxymfSMwKGTObY5e.jpg"); }, @@ -330,6 +343,7 @@ describe("without API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -337,6 +351,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -356,6 +371,7 @@ describe("without API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -363,6 +379,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -382,6 +399,7 @@ describe("without API Key", () => { // expect(typeof topResult.imdb).toBe("object"); // expect(typeof topResult.filename).toBe("string"); // expect(typeof topResult.episode).toBe("number"); + // expect(typeof topResult.duration).toBe("number"); // expect(typeof topResult.from).toBe("number"); // expect(typeof topResult.to).toBe("number"); // expect(typeof topResult.similarity).toBe("number"); @@ -389,6 +407,7 @@ describe("without API Key", () => { // expect(typeof topResult.image).toBe("string"); // expect(topResult.imdb.id).toBe("21034"); // expect(topResult.episode).toBe(1); + // expect(topResult.duration).toBe(1422.01); // expect(topResult.similarity).toBeGreaterThan(0.9); // }, // 1000 * 10 @@ -409,12 +428,14 @@ describe("without API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); expect(typeof topResult.video).toBe("string"); expect(typeof topResult.image).toBe("string"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -458,6 +479,7 @@ describe("with API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -465,6 +487,7 @@ describe("with API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -513,6 +536,7 @@ describe("with system system API Key", () => { expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -520,6 +544,7 @@ describe("with system system API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -601,6 +626,7 @@ describe.each([ expect(typeof topResult.imdb).toBe("string"); expect(typeof topResult.filename).toBe("string"); expect(typeof topResult.episode).toBe("number"); + expect(typeof topResult.duration).toBe("number"); expect(typeof topResult.from).toBe("number"); expect(typeof topResult.to).toBe("number"); expect(typeof topResult.similarity).toBe("number"); @@ -608,6 +634,7 @@ describe.each([ expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); + expect(topResult.duration).toBe(1422.01); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 From cbf9cc62de6858066041942d403151ae3348d289 Mon Sep 17 00:00:00 2001 From: Leslie-Wong-H <79917148leslie@gmail.com> Date: Fri, 11 Aug 2023 07:33:30 +0800 Subject: [PATCH 2/2] Fix duration e2e error --- src/search.test.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/search.test.js b/src/search.test.js index 628e51e..350bc98 100644 --- a/src/search.test.js +++ b/src/search.test.js @@ -251,7 +251,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -286,7 +286,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); await fs.remove("32B15UXxymfSMwKGTObY5e.jpg"); }, @@ -322,7 +322,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); await fs.remove("32B15UXxymfSMwKGTObY5e.jpg"); }, @@ -351,7 +351,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -379,7 +379,7 @@ describe("without API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -407,7 +407,7 @@ describe("without API Key", () => { // expect(typeof topResult.image).toBe("string"); // expect(topResult.imdb.id).toBe("21034"); // expect(topResult.episode).toBe(1); - // expect(topResult.duration).toBe(1422.01); + // expect(Math.floor(topResult.duration)).toBe(1422); // expect(topResult.similarity).toBeGreaterThan(0.9); // }, // 1000 * 10 @@ -435,7 +435,7 @@ describe("without API Key", () => { expect(typeof topResult.video).toBe("string"); expect(typeof topResult.image).toBe("string"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -487,7 +487,7 @@ describe("with API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -544,7 +544,7 @@ describe("with system system API Key", () => { expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10 @@ -634,7 +634,7 @@ describe.each([ expect(typeof topResult.image).toBe("string"); expect(topResult.imdb).toBe("21034"); expect(topResult.episode).toBe(1); - expect(topResult.duration).toBe(1422.01); + expect(Math.floor(topResult.duration)).toBe(1422); expect(topResult.similarity).toBeGreaterThan(0.9); }, 1000 * 10