Update endpoints/antibot.js
Browse files- endpoints/antibot.js +86 -8
endpoints/antibot.js
CHANGED
|
@@ -4,25 +4,38 @@ const { extractTextFromImage, uploadImageToHosting } = require('./imageProcessor
|
|
| 4 |
|
| 5 |
async function extractTextFromBuffer(imageBuffer) {
|
| 6 |
try {
|
|
|
|
| 7 |
const tempPath = path.join(__dirname, `temp_${Date.now()}.jpg`);
|
| 8 |
fs.writeFileSync(tempPath, imageBuffer);
|
|
|
|
|
|
|
| 9 |
const result = await extractTextFromImage(tempPath);
|
|
|
|
|
|
|
| 10 |
fs.unlinkSync(tempPath);
|
|
|
|
|
|
|
| 11 |
return result;
|
| 12 |
} catch (error) {
|
|
|
|
| 13 |
return { status: false, response: 'Gagal memproses gambar' };
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
function mapAnswer(soalArray, jawaban, botIndex) {
|
|
|
|
| 18 |
return jawaban;
|
| 19 |
}
|
| 20 |
|
| 21 |
function normalizeText(text) {
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
function isValueMatch(value, targetSoal) {
|
|
|
|
|
|
|
| 26 |
const numberMap = {
|
| 27 |
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four',
|
| 28 |
'5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten',
|
|
@@ -62,6 +75,7 @@ function isValueMatch(value, targetSoal) {
|
|
| 62 |
const normalizedSoal = normalizeText(targetSoal);
|
| 63 |
|
| 64 |
if (normalizedValue === normalizedSoal) {
|
|
|
|
| 65 |
return true;
|
| 66 |
}
|
| 67 |
|
|
@@ -72,30 +86,59 @@ function isValueMatch(value, targetSoal) {
|
|
| 72 |
const leetValue = convertLeet(value);
|
| 73 |
const leetSoal = convertLeet(targetSoal);
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if (leetValue ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
const mappedValue = numberMap[normalizedValue] || numberMap[value] || normalizedValue;
|
| 80 |
const mappedSoal = numberMap[normalizedSoal] || numberMap[targetSoal] || normalizedSoal;
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
if (mappedValue ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
const similarity = calculateSimilarity(normalizedValue, normalizedSoal);
|
|
|
|
| 87 |
if (similarity >= 0.8) {
|
|
|
|
| 88 |
return true;
|
| 89 |
}
|
| 90 |
|
| 91 |
try {
|
| 92 |
const valueResult = evaluateSimpleMath(value);
|
| 93 |
const soalResult = evaluateSimpleMath(targetSoal);
|
|
|
|
| 94 |
if (valueResult !== null && soalResult !== null && valueResult === soalResult) {
|
|
|
|
| 95 |
return true;
|
| 96 |
}
|
| 97 |
-
} catch (e) {
|
|
|
|
|
|
|
| 98 |
|
|
|
|
| 99 |
return false;
|
| 100 |
}
|
| 101 |
|
|
@@ -130,35 +173,46 @@ function evaluateSimpleMath(expression) {
|
|
| 130 |
}
|
| 131 |
|
| 132 |
function parseSoalText(text) {
|
|
|
|
| 133 |
const delimiters = /[.,:;\\/\s]+/;
|
| 134 |
let parts = text.split(delimiters).filter(part => part.trim() !== '');
|
| 135 |
if (parts.length === 1) {
|
| 136 |
parts = text.split(/\s+/).filter(part => part.trim() !== '');
|
| 137 |
}
|
|
|
|
| 138 |
return parts;
|
| 139 |
}
|
| 140 |
|
| 141 |
async function antibot(data) {
|
|
|
|
|
|
|
|
|
|
| 142 |
try {
|
| 143 |
const { main, bots } = data;
|
| 144 |
|
|
|
|
| 145 |
const mainBuffer = Buffer.from(main, 'base64');
|
| 146 |
const mainText = await extractTextFromBuffer(mainBuffer);
|
| 147 |
|
|
|
|
|
|
|
| 148 |
if (!mainText.status) {
|
| 149 |
throw new Error('Gagal mengekstrak teks dari gambar utama: ' + mainText.response);
|
| 150 |
}
|
| 151 |
|
| 152 |
const soalArray = parseSoalText(mainText.response);
|
|
|
|
| 153 |
|
| 154 |
if (soalArray.length === 0) {
|
| 155 |
throw new Error('Tidak ada soal yang terdeteksi');
|
| 156 |
}
|
| 157 |
|
| 158 |
const botResults = [];
|
|
|
|
| 159 |
|
| 160 |
for (let i = 0; i < bots.length; i++) {
|
| 161 |
const bot = bots[i];
|
|
|
|
| 162 |
try {
|
| 163 |
const botBuffer = Buffer.from(bot.img, 'base64');
|
| 164 |
const botText = await extractTextFromBuffer(botBuffer);
|
|
@@ -171,7 +225,14 @@ async function antibot(data) {
|
|
| 171 |
normalized: normalizeText(botText.response)
|
| 172 |
});
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
} catch (error) {
|
|
|
|
| 175 |
botResults.push({
|
| 176 |
id: bot.id,
|
| 177 |
text: '',
|
|
@@ -182,12 +243,14 @@ async function antibot(data) {
|
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
|
|
|
| 185 |
const result = [];
|
| 186 |
const usedIds = new Set();
|
| 187 |
let matchedCount = 0;
|
| 188 |
|
| 189 |
for (let i = 0; i < soalArray.length; i++) {
|
| 190 |
const targetSoal = soalArray[i];
|
|
|
|
| 191 |
let foundId = null;
|
| 192 |
|
| 193 |
for (const bot of botResults) {
|
|
@@ -196,6 +259,7 @@ async function antibot(data) {
|
|
| 196 |
foundId = bot.id;
|
| 197 |
usedIds.add(bot.id);
|
| 198 |
matchedCount++;
|
|
|
|
| 199 |
break;
|
| 200 |
}
|
| 201 |
}
|
|
@@ -207,7 +271,10 @@ async function antibot(data) {
|
|
| 207 |
});
|
| 208 |
}
|
| 209 |
|
|
|
|
|
|
|
| 210 |
if (matchedCount < soalArray.length) {
|
|
|
|
| 211 |
for (let i = 0; i < result.length; i++) {
|
| 212 |
if (!result[i].id) {
|
| 213 |
const targetSoal = soalArray[i];
|
|
@@ -220,6 +287,7 @@ async function antibot(data) {
|
|
| 220 |
result[i].matchType = 'normalized';
|
| 221 |
usedIds.add(bot.id);
|
| 222 |
matchedCount++;
|
|
|
|
| 223 |
break;
|
| 224 |
}
|
| 225 |
}
|
|
@@ -228,7 +296,10 @@ async function antibot(data) {
|
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
|
|
|
|
|
|
| 231 |
if (matchedCount >= 2) {
|
|
|
|
| 232 |
for (let i = 0; i < result.length; i++) {
|
| 233 |
if (!result[i].id) {
|
| 234 |
for (const bot of botResults) {
|
|
@@ -236,12 +307,14 @@ async function antibot(data) {
|
|
| 236 |
result[i].id = bot.id;
|
| 237 |
result[i].matchType = 'fallback';
|
| 238 |
usedIds.add(bot.id);
|
|
|
|
| 239 |
break;
|
| 240 |
}
|
| 241 |
}
|
| 242 |
}
|
| 243 |
}
|
| 244 |
} else if (matchedCount === 1) {
|
|
|
|
| 245 |
for (let i = 0; i < result.length; i++) {
|
| 246 |
if (!result[i].id) {
|
| 247 |
result[i].id = 'invalid';
|
|
@@ -249,6 +322,7 @@ async function antibot(data) {
|
|
| 249 |
}
|
| 250 |
}
|
| 251 |
} else {
|
|
|
|
| 252 |
for (let i = 0; i < result.length; i++) {
|
| 253 |
result[i].id = 'invalid';
|
| 254 |
result[i].matchType = 'invalid';
|
|
@@ -262,6 +336,9 @@ async function antibot(data) {
|
|
| 262 |
}
|
| 263 |
}
|
| 264 |
|
|
|
|
|
|
|
|
|
|
| 265 |
return {
|
| 266 |
success: true,
|
| 267 |
data: {
|
|
@@ -278,6 +355,7 @@ async function antibot(data) {
|
|
| 278 |
};
|
| 279 |
|
| 280 |
} catch (error) {
|
|
|
|
| 281 |
return {
|
| 282 |
success: false,
|
| 283 |
error: error.message,
|
|
|
|
| 4 |
|
| 5 |
async function extractTextFromBuffer(imageBuffer) {
|
| 6 |
try {
|
| 7 |
+
console.log('๐ผ๏ธ DEBUG extractTextFromBuffer: Membuat file temporary');
|
| 8 |
const tempPath = path.join(__dirname, `temp_${Date.now()}.jpg`);
|
| 9 |
fs.writeFileSync(tempPath, imageBuffer);
|
| 10 |
+
console.log('๐ DEBUG: File temporary dibuat:', tempPath);
|
| 11 |
+
|
| 12 |
const result = await extractTextFromImage(tempPath);
|
| 13 |
+
console.log('โ
DEBUG extractTextFromBuffer: Hasil ekstraksi:', result);
|
| 14 |
+
|
| 15 |
fs.unlinkSync(tempPath);
|
| 16 |
+
console.log('๐งน DEBUG: File temporary dihapus');
|
| 17 |
+
|
| 18 |
return result;
|
| 19 |
} catch (error) {
|
| 20 |
+
console.error('โ DEBUG extractTextFromBuffer Error:', error.message);
|
| 21 |
return { status: false, response: 'Gagal memproses gambar' };
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
function mapAnswer(soalArray, jawaban, botIndex) {
|
| 26 |
+
console.log(`๐ค DEBUG mapAnswer: Bot ${botIndex}, Jawaban: "${jawaban}"`);
|
| 27 |
return jawaban;
|
| 28 |
}
|
| 29 |
|
| 30 |
function normalizeText(text) {
|
| 31 |
+
const normalized = text.toLowerCase().replace(/[^\w\s]/g, '').trim();
|
| 32 |
+
console.log(`๐ค DEBUG normalizeText: "${text}" -> "${normalized}"`);
|
| 33 |
+
return normalized;
|
| 34 |
}
|
| 35 |
|
| 36 |
function isValueMatch(value, targetSoal) {
|
| 37 |
+
console.log(`๐ DEBUG isValueMatch: Value="${value}", Soal="${targetSoal}"`);
|
| 38 |
+
|
| 39 |
const numberMap = {
|
| 40 |
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four',
|
| 41 |
'5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten',
|
|
|
|
| 75 |
const normalizedSoal = normalizeText(targetSoal);
|
| 76 |
|
| 77 |
if (normalizedValue === normalizedSoal) {
|
| 78 |
+
console.log('โ
DEBUG: Match exact normalized');
|
| 79 |
return true;
|
| 80 |
}
|
| 81 |
|
|
|
|
| 86 |
const leetValue = convertLeet(value);
|
| 87 |
const leetSoal = convertLeet(targetSoal);
|
| 88 |
|
| 89 |
+
console.log(`๐ข DEBUG Leet: Value="${leetValue}", Soal="${leetSoal}"`);
|
| 90 |
+
|
| 91 |
+
if (leetValue === normalizedSoal) {
|
| 92 |
+
console.log('โ
DEBUG: Match leet value -> normalized soal');
|
| 93 |
+
return true;
|
| 94 |
+
}
|
| 95 |
+
if (normalizedValue === leetSoal) {
|
| 96 |
+
console.log('โ
DEBUG: Match normalized value -> leet soal');
|
| 97 |
+
return true;
|
| 98 |
+
}
|
| 99 |
+
if (leetValue === leetSoal) {
|
| 100 |
+
console.log('โ
DEBUG: Match leet value -> leet soal');
|
| 101 |
+
return true;
|
| 102 |
+
}
|
| 103 |
|
| 104 |
const mappedValue = numberMap[normalizedValue] || numberMap[value] || normalizedValue;
|
| 105 |
const mappedSoal = numberMap[normalizedSoal] || numberMap[targetSoal] || normalizedSoal;
|
| 106 |
|
| 107 |
+
console.log(`๐ DEBUG Number Map: Value="${mappedValue}", Soal="${mappedSoal}"`);
|
| 108 |
+
|
| 109 |
+
if (mappedValue === normalizedSoal) {
|
| 110 |
+
console.log('โ
DEBUG: Match mapped value -> normalized soal');
|
| 111 |
+
return true;
|
| 112 |
+
}
|
| 113 |
+
if (normalizedValue === mappedSoal) {
|
| 114 |
+
console.log('โ
DEBUG: Match normalized value -> mapped soal');
|
| 115 |
+
return true;
|
| 116 |
+
}
|
| 117 |
+
if (mappedValue === mappedSoal) {
|
| 118 |
+
console.log('โ
DEBUG: Match mapped value -> mapped soal');
|
| 119 |
+
return true;
|
| 120 |
+
}
|
| 121 |
|
| 122 |
const similarity = calculateSimilarity(normalizedValue, normalizedSoal);
|
| 123 |
+
console.log(`๐ DEBUG Similarity: ${similarity}`);
|
| 124 |
if (similarity >= 0.8) {
|
| 125 |
+
console.log('โ
DEBUG: Match similarity >= 0.8');
|
| 126 |
return true;
|
| 127 |
}
|
| 128 |
|
| 129 |
try {
|
| 130 |
const valueResult = evaluateSimpleMath(value);
|
| 131 |
const soalResult = evaluateSimpleMath(targetSoal);
|
| 132 |
+
console.log(`๐งฎ DEBUG Math: Value=${valueResult}, Soal=${soalResult}`);
|
| 133 |
if (valueResult !== null && soalResult !== null && valueResult === soalResult) {
|
| 134 |
+
console.log('โ
DEBUG: Match math evaluation');
|
| 135 |
return true;
|
| 136 |
}
|
| 137 |
+
} catch (e) {
|
| 138 |
+
console.log('โ DEBUG Math evaluation failed');
|
| 139 |
+
}
|
| 140 |
|
| 141 |
+
console.log('โ DEBUG: No match found');
|
| 142 |
return false;
|
| 143 |
}
|
| 144 |
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
function parseSoalText(text) {
|
| 176 |
+
console.log(`๐ DEBUG parseSoalText: Input text: "${text}"`);
|
| 177 |
const delimiters = /[.,:;\\/\s]+/;
|
| 178 |
let parts = text.split(delimiters).filter(part => part.trim() !== '');
|
| 179 |
if (parts.length === 1) {
|
| 180 |
parts = text.split(/\s+/).filter(part => part.trim() !== '');
|
| 181 |
}
|
| 182 |
+
console.log(`๐ DEBUG parseSoalText: Parsed parts:`, parts);
|
| 183 |
return parts;
|
| 184 |
}
|
| 185 |
|
| 186 |
async function antibot(data) {
|
| 187 |
+
console.log('๐ DEBUG antibot: Memulai proses antibot');
|
| 188 |
+
console.log('๐ DEBUG: Data received - main:', data.main ? 'โ
' : 'โ', 'bots:', data.bots?.length || 0);
|
| 189 |
+
|
| 190 |
try {
|
| 191 |
const { main, bots } = data;
|
| 192 |
|
| 193 |
+
console.log('๐ผ๏ธ DEBUG: Processing main image...');
|
| 194 |
const mainBuffer = Buffer.from(main, 'base64');
|
| 195 |
const mainText = await extractTextFromBuffer(mainBuffer);
|
| 196 |
|
| 197 |
+
console.log('๐ DEBUG Main Text Result:', mainText);
|
| 198 |
+
|
| 199 |
if (!mainText.status) {
|
| 200 |
throw new Error('Gagal mengekstrak teks dari gambar utama: ' + mainText.response);
|
| 201 |
}
|
| 202 |
|
| 203 |
const soalArray = parseSoalText(mainText.response);
|
| 204 |
+
console.log(`๐ DEBUG: Soal array:`, soalArray);
|
| 205 |
|
| 206 |
if (soalArray.length === 0) {
|
| 207 |
throw new Error('Tidak ada soal yang terdeteksi');
|
| 208 |
}
|
| 209 |
|
| 210 |
const botResults = [];
|
| 211 |
+
console.log(`๐ค DEBUG: Processing ${bots.length} bots...`);
|
| 212 |
|
| 213 |
for (let i = 0; i < bots.length; i++) {
|
| 214 |
const bot = bots[i];
|
| 215 |
+
console.log(`๐ค DEBUG: Processing bot ${i+1}/${bots.length} - ID: ${bot.id}`);
|
| 216 |
try {
|
| 217 |
const botBuffer = Buffer.from(bot.img, 'base64');
|
| 218 |
const botText = await extractTextFromBuffer(botBuffer);
|
|
|
|
| 225 |
normalized: normalizeText(botText.response)
|
| 226 |
});
|
| 227 |
|
| 228 |
+
console.log(`โ
DEBUG Bot ${bot.id}:`, {
|
| 229 |
+
text: botText.response,
|
| 230 |
+
value: mappedValue,
|
| 231 |
+
normalized: normalizeText(botText.response)
|
| 232 |
+
});
|
| 233 |
+
|
| 234 |
} catch (error) {
|
| 235 |
+
console.error(`โ DEBUG Bot ${bot.id} Error:`, error.message);
|
| 236 |
botResults.push({
|
| 237 |
id: bot.id,
|
| 238 |
text: '',
|
|
|
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
| 246 |
+
console.log('๐ DEBUG: Starting matching process...');
|
| 247 |
const result = [];
|
| 248 |
const usedIds = new Set();
|
| 249 |
let matchedCount = 0;
|
| 250 |
|
| 251 |
for (let i = 0; i < soalArray.length; i++) {
|
| 252 |
const targetSoal = soalArray[i];
|
| 253 |
+
console.log(`๐ฏ DEBUG: Matching soal "${targetSoal}"`);
|
| 254 |
let foundId = null;
|
| 255 |
|
| 256 |
for (const bot of botResults) {
|
|
|
|
| 259 |
foundId = bot.id;
|
| 260 |
usedIds.add(bot.id);
|
| 261 |
matchedCount++;
|
| 262 |
+
console.log(`โ
DEBUG: Soal "${targetSoal}" matched with bot ${bot.id}`);
|
| 263 |
break;
|
| 264 |
}
|
| 265 |
}
|
|
|
|
| 271 |
});
|
| 272 |
}
|
| 273 |
|
| 274 |
+
console.log(`๐ DEBUG: Initial matches: ${matchedCount}/${soalArray.length}`);
|
| 275 |
+
|
| 276 |
if (matchedCount < soalArray.length) {
|
| 277 |
+
console.log('๐ DEBUG: Trying normalized matching...');
|
| 278 |
for (let i = 0; i < result.length; i++) {
|
| 279 |
if (!result[i].id) {
|
| 280 |
const targetSoal = soalArray[i];
|
|
|
|
| 287 |
result[i].matchType = 'normalized';
|
| 288 |
usedIds.add(bot.id);
|
| 289 |
matchedCount++;
|
| 290 |
+
console.log(`โ
DEBUG: Soal "${targetSoal}" normalized match with bot ${bot.id}`);
|
| 291 |
break;
|
| 292 |
}
|
| 293 |
}
|
|
|
|
| 296 |
}
|
| 297 |
}
|
| 298 |
|
| 299 |
+
console.log(`๐ DEBUG: After normalized: ${matchedCount}/${soalArray.length}`);
|
| 300 |
+
|
| 301 |
if (matchedCount >= 2) {
|
| 302 |
+
console.log('๐ DEBUG: Using fallback matching...');
|
| 303 |
for (let i = 0; i < result.length; i++) {
|
| 304 |
if (!result[i].id) {
|
| 305 |
for (const bot of botResults) {
|
|
|
|
| 307 |
result[i].id = bot.id;
|
| 308 |
result[i].matchType = 'fallback';
|
| 309 |
usedIds.add(bot.id);
|
| 310 |
+
console.log(`๐ DEBUG: Soal ${i+1} fallback to bot ${bot.id}`);
|
| 311 |
break;
|
| 312 |
}
|
| 313 |
}
|
| 314 |
}
|
| 315 |
}
|
| 316 |
} else if (matchedCount === 1) {
|
| 317 |
+
console.log('โ ๏ธ DEBUG: Only 1 match found, marking others as invalid');
|
| 318 |
for (let i = 0; i < result.length; i++) {
|
| 319 |
if (!result[i].id) {
|
| 320 |
result[i].id = 'invalid';
|
|
|
|
| 322 |
}
|
| 323 |
}
|
| 324 |
} else {
|
| 325 |
+
console.log('โ DEBUG: No matches found, all invalid');
|
| 326 |
for (let i = 0; i < result.length; i++) {
|
| 327 |
result[i].id = 'invalid';
|
| 328 |
result[i].matchType = 'invalid';
|
|
|
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
+
console.log('๐ DEBUG: Process completed successfully');
|
| 340 |
+
console.log('๐ DEBUG Final Result:', result);
|
| 341 |
+
|
| 342 |
return {
|
| 343 |
success: true,
|
| 344 |
data: {
|
|
|
|
| 355 |
};
|
| 356 |
|
| 357 |
} catch (error) {
|
| 358 |
+
console.error('๐ฅ DEBUG antibot Error:', error.message);
|
| 359 |
return {
|
| 360 |
success: false,
|
| 361 |
error: error.message,
|