koishi-plugin-best-cave 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +18 -6
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -625,8 +625,12 @@ var HashManager = class {
|
|
|
625
625
|
const flushHashes = /* @__PURE__ */ __name(async () => {
|
|
626
626
|
if (hashesToInsert.length > 0) {
|
|
627
627
|
this.logger.info(`补全第 ${batchStartCaveCount + 1} 到 ${historicalCount} 条回声洞哈希中...`);
|
|
628
|
-
|
|
629
|
-
|
|
628
|
+
try {
|
|
629
|
+
await this.ctx.database.upsert("cave_hash", hashesToInsert);
|
|
630
|
+
totalHashesGenerated += hashesToInsert.length;
|
|
631
|
+
} catch (error) {
|
|
632
|
+
this.logger.error(`导入哈希失败: ${error.message}`);
|
|
633
|
+
}
|
|
630
634
|
hashesToInsert = [];
|
|
631
635
|
batchStartCaveCount = historicalCount;
|
|
632
636
|
}
|
|
@@ -634,21 +638,29 @@ var HashManager = class {
|
|
|
634
638
|
for (const cave of allCaves) {
|
|
635
639
|
if (existingHashedCaveIds.has(cave.id)) continue;
|
|
636
640
|
historicalCount++;
|
|
641
|
+
const newHashesForCave = [];
|
|
637
642
|
const combinedText = cave.elements.filter((el) => el.type === "text" && el.content).map((el) => el.content).join(" ");
|
|
638
|
-
|
|
639
|
-
|
|
643
|
+
const textHash = this.generateTextSimhash(combinedText);
|
|
644
|
+
if (textHash) {
|
|
645
|
+
newHashesForCave.push({ cave: cave.id, hash: textHash, type: "sim" });
|
|
640
646
|
}
|
|
641
647
|
for (const el of cave.elements.filter((el2) => el2.type === "image" && el2.file)) {
|
|
642
648
|
try {
|
|
643
649
|
const imageBuffer = await this.fileManager.readFile(el.file);
|
|
644
650
|
const pHash = await this.generateImagePHash(imageBuffer);
|
|
645
|
-
|
|
651
|
+
newHashesForCave.push({ cave: cave.id, hash: pHash, type: "phash" });
|
|
646
652
|
const subHashes = await this.generateImageSubHashes(imageBuffer);
|
|
647
|
-
subHashes.forEach((subHash) =>
|
|
653
|
+
subHashes.forEach((subHash) => newHashesForCave.push({ cave: cave.id, hash: subHash, type: "sub" }));
|
|
648
654
|
} catch (e) {
|
|
649
655
|
this.logger.warn(`无法为回声洞(${cave.id})的内容(${el.file})生成哈希:`, e);
|
|
650
656
|
}
|
|
651
657
|
}
|
|
658
|
+
const uniqueHashesMap = /* @__PURE__ */ new Map();
|
|
659
|
+
newHashesForCave.forEach((h4) => {
|
|
660
|
+
const uniqueKey = `${h4.type}-${h4.hash}`;
|
|
661
|
+
uniqueHashesMap.set(uniqueKey, h4);
|
|
662
|
+
});
|
|
663
|
+
hashesToInsert.push(...uniqueHashesMap.values());
|
|
652
664
|
if (hashesToInsert.length >= 100) await flushHashes();
|
|
653
665
|
}
|
|
654
666
|
await flushHashes();
|