/** * Kahoot Quiz Answers Extractor & Search API * Scrapes questions, choices, correct answers, media, time limits by: * - Direct Kahoot Quiz UUID / URL * - Kahoot Challenge URL / Challenge ID / PIN * - Search query string * * Author: Vamz Spectre / VamxAPI */ const DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"; const KAHOOT_REST_API = "https://create.kahoot.it/rest/kahoots"; const KAHOOT_CHALLENGE_API = "https://kahoot.it/rest/challenges"; function cleanText(text) { if (!text || typeof text !== "string") return ""; return text.replace(/<[^>]+>/g, "").trim(); } /** * Ekstraksi Quiz UUID dari input (mendukung URL challenge, URL details, atau UUID langsung) */ async function resolveQuizUuid(input) { if (!input || typeof input !== "string") return ""; const trimmed = input.trim(); // 1. Jika URL challenge (contoh: https://kahoot.it/challenge/01830802?challenge-id=90bf5879...) if (trimmed.includes("challenge")) { const challengeIdMatch = trimmed.match(/challenge-id=([a-f0-9\-_]+)/i); const challengeId = challengeIdMatch ? challengeIdMatch[1] : null; if (challengeId) { try { const res = await fetch(`${KAHOOT_CHALLENGE_API}/${challengeId}`, { headers: { "User-Agent": DEFAULT_USER_AGENT, "Accept": "application/json" } }); if (res.ok) { const data = await res.json(); if (data.quizId) return data.quizId; } } catch {} } } // 2. Deteksi UUID standar v4 (8-4-4-4-12) const uuidRegex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i; const match = trimmed.match(uuidRegex); return match ? match[0] : trimmed; } /** * Mengambil soal dan kunci jawaban dari Kahoot kuis * @param {string} quizIdOrUrl - UUID kuis, link kuis, atau link challenge * @param {object} options - Opsi timeout & user-agent */ async function getQuizAnswers(quizIdOrUrl, options = {}) { const uuid = await resolveQuizUuid(quizIdOrUrl); if (!uuid) { throw new Error("Valid Kahoot Quiz UUID, URL, or Challenge URL is required"); } const timeoutMs = options.timeout || 15000; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeoutMs); try { const url = `${KAHOOT_REST_API}/${uuid}`; const res = await fetch(url, { headers: { "User-Agent": options.userAgent || DEFAULT_USER_AGENT, "Accept": "application/json" }, signal: controller.signal }); if (!res.ok) { if (res.status === 404) { throw new Error("Kahoot quiz not found or is set to Private (HTTP 404)"); } throw new Error(`Failed to fetch Kahoot quiz data (HTTP ${res.status})`); } const data = await res.json(); const questions = (data.questions || []).map((q, index) => { const choices = (q.choices || []).map(c => ({ answer: cleanText(c.answer), correct: Boolean(c.correct) })); const correctAnswers = choices.filter(c => c.correct).map(c => c.answer); return { number: index + 1, question: cleanText(q.question || q.title || ""), type: q.type || "quiz", time_limit_ms: q.time || 20000, points: q.pointsMultiplier ?? 1, image_url: q.image || null, choices, correct_answers: correctAnswers }; }); return { success: true, uuid: data.uuid || uuid, title: data.title || "", description: data.description || "", creator_username: data.creator_username || "", total_questions: questions.length, cover_image: data.cover || null, questions }; } finally { clearTimeout(timeoutId); } } /** * Mencari kuis Kahoot berdasarkan keyword / judul kuis * @param {string} query - Kata kunci / judul kuis * @param {object} options - Opsi limit dan timeout */ async function searchQuiz(query, options = {}) { if (!query || typeof query !== "string") { throw new Error("Search query string is required"); } const limit = options.limit || 5; const timeoutMs = options.timeout || 15000; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeoutMs); try { const url = `${KAHOOT_REST_API}/?query=${encodeURIComponent(query)}&limit=${limit}`; const res = await fetch(url, { headers: { "User-Agent": options.userAgent || DEFAULT_USER_AGENT, "Accept": "application/json" }, signal: controller.signal }); if (!res.ok) { throw new Error(`Failed to search Kahoot quizzes (HTTP ${res.status})`); } const data = await res.json(); const entities = data.entities || []; const results = entities.map(item => { const card = item.card || {}; return { uuid: card.uuid || "", title: card.title || "", description: card.description || "", creator_username: card.creator_username || "", number_of_questions: card.number_of_questions || 0, cover_image: card.cover || null, details_url: `https://create.kahoot.it/details/${card.uuid}` }; }); return { success: true, query, total_results: results.length, results }; } finally { clearTimeout(timeoutId); } } if (typeof module !== "undefined" && module.exports) { module.exports = { getQuizAnswers, searchQuiz }; }