/** * ======================================================== * Name: Quizizz & Wayground Live Questions & Answers Scraper * * Cara Download: * curl -sL "https://code.vamxapi.my.id/snippet/raw/quizizz-questions-scraper" -o quizizz-questions-scraper.js * * Cara Eksekusi: * node quizizz-questions-scraper.js "601541" * node quizizz-questions-scraper.js "https://wayground.com/join?gc=601541" * node quizizz-questions-scraper.js "https://wayground.com/admin/quiz/60ca9d27d88141001c1d792d" * node quizizz-questions-scraper.js "60ca9d27d88141001c1d792d" * * Author: Vamz Spectre * Source: https://vamxapi.my.id * ======================================================== */ 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 QUIZIZZ_PLAY_API = "https://game.quizizz.com/play-api"; class Encoding { static decode(t, e = false) { if (e) { const head = this.extractHeader(t); return this.decodeRaw(head, true); } const headDecoded = this.decode(this.extractHeader(t), true); const data = this.extractData(t); return this.decodeRaw(data, false, headDecoded); } static decodeRaw(t, e, o = "quizizz.com") { const s = this.extractVersion(t); let r = e ? o.charCodeAt(0) : o.charCodeAt(0) + o.charCodeAt(o.length - 1); r = -r; const n = []; for (let i = 0; i < t.length; i++) { const c = t[i].charCodeAt(0); const a = e ? this.safeAdd(c, r) : this.addOffset(c, r, i, s); n.push(String.fromCharCode(a)); } return n.join(""); } static addOffset(t, e, o, s) { return 2 === s ? this.verifyCharCode(t) ? this.safeAdd(t, o % 2 === 0 ? e : -e) : t : this.safeAdd(t, o % 2 === 0 ? e : -e); } static extractData(t) { const e = t.charCodeAt(t.length - 2) - 33; return t.slice(e, -2); } static extractHeader(t) { const e = t.charCodeAt(t.length - 2) - 33; return t.slice(0, e); } static extractVersion(t) { if (typeof t === "string" && t[t.length - 1]) { const e = parseInt(t[t.length - 1], 10); if (!isNaN(e)) return e; } return null; } static safeAdd(t, e) { const o = t + e; return o > 65535 ? o - 65535 + 0 - 1 : o < 0 ? 65535 - (0 - o) + 1 : o; } static verifyCharCode(t) { if (typeof t === "number") { return !(t >= 55296 && t <= 56319 || t >= 56320 && t <= 57343); } return false; } } function cleanText(text) { if (!text || typeof text !== "string") return ""; return text .replace(/<[^>]+>/g, "") .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, '"') .replace(/'/g, "'") .replace(/ /g, " ") .trim(); } function extractIdentifier(input) { if (!input || typeof input !== "string") return { type: "unknown", value: "" }; const trimmed = input.trim(); // 1. Direct MongoDB ObjectId atau URL quiz admin const quizMatch = trimmed.match(/quiz\/([a-f0-9]{24})/i) || trimmed.match(/^([a-f0-9]{24})$/i); if (quizMatch) { return { type: "quiz_id", value: quizMatch[1] }; } // 2. Game Code (PIN) 4-8 digit const codeMatch = trimmed.match(/[?&](?:gc|code)=([0-9]{4,8})/i) || trimmed.match(/([0-9]{4,8})/); if (codeMatch) { return { type: "game_code", value: codeMatch[1] }; } return { type: "unknown", value: trimmed }; } function extractGameCode(input) { const ident = extractIdentifier(input); return ident.value; } function decodeQuizizzAnswer(rawAnswer, structure, options) { if (rawAnswer === undefined || rawAnswer === null) { const kind = (structure.kind || structure.type || "").toUpperCase(); if (kind === "BLANK") { const texts = options.map((o) => o.text).filter(Boolean); options.forEach((o) => { o.is_correct = true; }); return { answer_indices: options.map((o) => o.index), answers: texts }; } return { answer_indices: [], answers: [] }; } let resolved = null; if (typeof rawAnswer === "number") { resolved = [rawAnswer]; } else if (Array.isArray(rawAnswer)) { resolved = rawAnswer.map((x) => (typeof x === "number" ? x : parseInt(x, 10))).filter((x) => !isNaN(x)); } else if (typeof rawAnswer === "string") { const trimmed = rawAnswer.trim(); if (/^\d+$/.test(trimmed)) { resolved = [parseInt(trimmed, 10)]; } else if (trimmed.startsWith("[") && trimmed.endsWith("]")) { try { const parsed = JSON.parse(trimmed); if (Array.isArray(parsed)) { resolved = parsed.map((x) => (typeof x === "number" ? x : parseInt(x, 10))).filter((x) => !isNaN(x)); } } catch {} } if (!resolved) { try { const decoded = Encoding.decode(trimmed); if (decoded) { try { const parsed = JSON.parse(decoded); if (Array.isArray(parsed)) { resolved = parsed.map((x) => (typeof x === "number" ? x : parseInt(x, 10))).filter((x) => !isNaN(x)); } else if (typeof parsed === "number") { resolved = [parsed]; } } catch {} if (!resolved && /^\d+$/.test(decoded.trim())) { resolved = [parseInt(decoded.trim(), 10)]; } if (!resolved && decoded.length > 0) { return { answer_indices: [], answers: [cleanText(decoded)] }; } } } catch {} } } if (Array.isArray(resolved) && resolved.length > 0) { const answerTexts = []; resolved.forEach((idx) => { if (options[idx]) { options[idx].is_correct = true; const text = options[idx].text || (options[idx].media && options[idx].media[0]?.url ? `(Media: ${options[idx].media[0].url})` : ""); if (text) answerTexts.push(text); } }); return { answer_indices: resolved, answers: answerTexts }; } const kind = (structure.kind || structure.type || "").toUpperCase(); if (kind === "BLANK") { const texts = options.map((o) => o.text).filter(Boolean); options.forEach((o) => { o.is_correct = true; }); return { answer_indices: options.map((o) => o.index), answers: texts }; } return { answer_indices: [], answers: [] }; } async function fetchQuizById(quizId, meta = {}) { const res = await fetch(`https://wayground.com/_api/main/quiz/${quizId}`, { headers: { "User-Agent": DEFAULT_USER_AGENT }, signal: AbortSignal.timeout(12000), }); if (!res.ok) { throw new Error(`Kuis Quizizz dengan ID "${quizId}" tidak ditemukan (HTTP ${res.status}).`); } const data = await res.json(); const quiz = data?.data?.quiz; const info = quiz?.info || {}; const rawQuestions = info.questions || []; const questions = rawQuestions.map((item, index) => { const structure = item.structure || {}; const query = structure.query || {}; const rawOptions = structure.options || []; const options = rawOptions.map((opt, optIdx) => ({ index: optIdx, id: opt.id || opt._id || `opt_${optIdx}`, text: cleanText(opt.text), media: opt.media || [], is_correct: false, })); const ansData = decodeQuizizzAnswer(structure.answer, structure, options); return { number: index + 1, id: item._id || item.id, type: item.type || structure.kind || "MCQ", time_limit_ms: info.qm?.[item._id]?.time || structure.time || 30000, question: cleanText(query.text || "(Media / Gambar Pertanyaan)"), media: query.media || [], options, answer_indices: ansData.answer_indices, answers: ansData.answers, }; }); return { quiz_id: quizId, title: cleanText(info.name || quiz?.name || "Quizizz Quiz"), subject: (info.subjects || []).join(", "), total_questions: questions.length, ...meta, questions, }; } async function getQuizizzQuestions(gameCodeOrUrl) { const ident = extractIdentifier(gameCodeOrUrl); if (!ident.value || ident.type === "unknown") { throw new Error("Game Code (PIN) atau Quiz URL/ID Quizizz / Wayground tidak valid."); } // 1. Direct Quiz ID if (ident.type === "quiz_id") { return await fetchQuizById(ident.value); } const roomCode = ident.value; // 2. Check Room untuk mendapatkan roomHash dan metadata const checkRes = await fetch(`${QUIZIZZ_PLAY_API}/v5/checkRoom`, { method: "POST", headers: { "Content-Type": "application/json", "User-Agent": DEFAULT_USER_AGENT, }, body: JSON.stringify({ roomCode }), signal: AbortSignal.timeout(10000), }); const checkData = await checkRes.json().catch(() => null); const room = checkData?.room; if (!room || !room.hash) { throw new Error(`Room Quizizz dengan kode "${roomCode}" tidak ditemukan atau sudah berakhir.`); } // 3. Coba ambil quizId agar mendapatkan jawaban 100% lengkap tanpa enkripsi let quizId = room.quizId || room.quizInfo?.quizId || null; if (!quizId && room.hash) { try { const studentRes = await fetch(`https://wayground.com/_gameapi/main/public/v1/students/games/${room.hash}`, { headers: { "User-Agent": DEFAULT_USER_AGENT }, signal: AbortSignal.timeout(6000), }); if (studentRes.ok) { const studentData = await studentRes.json(); quizId = studentData?.data?.items?.[0]?.quizId || null; } } catch {} } if (quizId) { try { const quizResult = await fetchQuizById(quizId, { game_code: room.code || roomCode, room_hash: room.hash, state: room.state || "active", type: room.type || "live", }); if (quizResult && quizResult.questions && quizResult.questions.length > 0) { return quizResult; } } catch {} } // 4. Fallback ke endpoint internal v4 jika quizId private const qRes = await fetch(`${QUIZIZZ_PLAY_API}/v4/getQuestions`, { method: "POST", headers: { "Content-Type": "application/json", "User-Agent": DEFAULT_USER_AGENT, }, body: JSON.stringify({ roomHash: room.hash }), signal: AbortSignal.timeout(15000), }); if (!qRes.ok) { throw new Error(`Gagal mengambil daftar pertanyaan kuis (HTTP ${qRes.status}).`); } const qData = await qRes.json(); const rawQ = qData.questions || {}; const questionIds = Object.keys(rawQ); const questions = questionIds.map((id, index) => { const item = rawQ[id]; const structure = item.structure || {}; const query = structure.query || {}; const rawOptions = structure.options || []; const options = rawOptions.map((opt, optIdx) => ({ index: optIdx, id: opt.id || opt._id || `opt_${optIdx}`, text: cleanText(opt.text), media: opt.media || [], is_correct: false, })); const ansData = decodeQuizizzAnswer(structure.answer, structure, options); return { number: index + 1, id, type: item.type || structure.kind || "MCQ", time_limit_ms: item.time || 30000, question: cleanText(query.text || "(Media / Gambar Pertanyaan)"), media: query.media || [], options, answer_indices: ansData.answer_indices, answers: ansData.answers, }; }); return { game_code: room.code || roomCode, room_hash: room.hash, state: room.state || "active", type: room.type || "live", total_questions: questions.length, questions, }; } if (typeof module !== "undefined" && module.exports) { module.exports = { extractGameCode, extractIdentifier, getQuizizzQuestions, default: getQuizizzQuestions, }; } /** * CLI Runner: * Langsung eksekusi jika dijalankan via terminal: node quizizz-questions-scraper.js "" */ const argCode = process.argv[2]; if (argCode) { getQuizizzQuestions(argCode) .then((res) => { console.log(JSON.stringify(res, null, 2)); }) .catch((err) => { console.error(JSON.stringify({ status: false, error: err.message }, null, 2)); process.exit(1); }); } else if ( process.argv[1] && (process.argv[1].endsWith("quizizz-questions-scraper.js") || process.argv[1].endsWith("quizizz-questions-scraper.cjs")) ) { console.log( JSON.stringify( { status: false, message: "Parameter Game Code (PIN) atau Quiz URL/ID belum ditentukan.", usage: 'node quizizz-questions-scraper.js ""', examples: [ 'node quizizz-questions-scraper.js "601541"', 'node quizizz-questions-scraper.js "https://wayground.com/join?gc=601541"', 'node quizizz-questions-scraper.js "60ca9d27d88141001c1d792d"', ], }, null, 2 ) ); process.exit(1); }