/** * Goal.com Live Score & Football Match Scraper * Author: Vamz Spectre (VamxAPI) * License: MIT * Dependencies: axios, cheerio */ const axios = require("axios"); const cheerio = require("cheerio"); const GOAL_BASE = "https://www.goal.com"; const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; /** * Mengambil live score dan jadwal pertandingan dari Goal.com * @param {Object} options * @param {string} [options.status='all'] - 'all', 'live', 'fixture', atau 'result' * @param {string} [options.search] - Kata kunci pencarian nama tim atau kompetisi * @param {string} [options.league] - Filter spesifik nama liga / turnamen * @param {string} [options.group='league'] - 'league' (per turnamen) atau 'flat' (list datar) */ async function scrapeGoalLivescore(options = {}) { const { status = "all", search = "", league = "", group = "league" } = options; const res = await axios.get(`${GOAL_BASE}/id/livescore`, { headers: { "User-Agent": USER_AGENT, "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7", }, timeout: 15000, }); const $ = cheerio.load(res.data); const nextDataRaw = $("#__NEXT_DATA__").html(); if (!nextDataRaw) { throw new Error("Gagal mengekstrak data dari Goal.com: __NEXT_DATA__ tidak ditemukan."); } const parsed = JSON.parse(nextDataRaw); const rawLiveScores = parsed.props?.pageProps?.content?.liveScores || []; const normalizedStatus = status.toUpperCase().trim(); const searchLower = search.toLowerCase().trim(); const leagueLower = league.toLowerCase().trim(); const results = []; let totalMatchesCount = 0; let liveMatchesCount = 0; for (const compGroup of rawLiveScores) { const competition = { id: compGroup.competition?.id || "", name: compGroup.competition?.name || "Unknown Competition", area: compGroup.competition?.area?.name || "", logo: compGroup.competition?.image?.url || "", }; if (leagueLower && !competition.name.toLowerCase().includes(leagueLower)) { continue; } const matches = []; for (const m of compGroup.matches || []) { const matchStatus = (m.status || "UNKNOWN").toUpperCase(); if (matchStatus === "LIVE") liveMatchesCount++; if (normalizedStatus !== "ALL") { if (normalizedStatus === "LIVE" && matchStatus !== "LIVE") continue; if (normalizedStatus === "FIXTURE" && matchStatus !== "FIXTURE") continue; if (normalizedStatus === "RESULT" && matchStatus !== "RESULT") continue; } const teamAName = m.teamA?.name || m.teamA?.full || "Team A"; const teamBName = m.teamB?.name || m.teamB?.full || "Team B"; if (searchLower) { const matchesComp = competition.name.toLowerCase().includes(searchLower); const matchesTeamA = teamAName.toLowerCase().includes(searchLower); const matchesTeamB = teamBName.toLowerCase().includes(searchLower); if (!matchesComp && !matchesTeamA && !matchesTeamB) { continue; } } const matchUrl = m.link?.slug && m.id ? `${GOAL_BASE}/id/pertandingan/${m.link.slug}/${m.id}` : null; const formattedMatch = { id: m.id, status: matchStatus, startDate: m.startDate, period: m.period ? { type: m.period.type || null, minute: typeof m.period.minute === "number" ? m.period.minute : null, extra: typeof m.period.extra === "number" ? m.period.extra : null, display: m.period.minute !== undefined ? `${m.period.minute}'` : null, } : null, round: m.round?.name || null, gameset: m.gameset?.name || null, venue: m.venue?.name || null, teamHome: { id: m.teamA?.id || null, name: teamAName, short: m.teamA?.short || null, code: m.teamA?.code || null, logo: m.teamA?.image?.url || null, score: m.score?.teamA !== undefined ? m.score.teamA : null, redCards: m.redCards?.teamA || 0, }, teamAway: { id: m.teamB?.id || null, name: teamBName, short: m.teamB?.short || null, code: m.teamB?.code || null, logo: m.teamB?.image?.url || null, score: m.score?.teamB !== undefined ? m.score.teamB : null, redCards: m.redCards?.teamB || 0, }, penalty: m.penalty || null, aggregate: m.agg || null, matchUrl, }; matches.push(formattedMatch); totalMatchesCount++; } if (matches.length > 0) { results.push({ competition, totalMatches: matches.length, matches, }); } } const summary = { totalCompetitions: results.length, totalMatches: totalMatchesCount, liveMatches: liveMatchesCount, filter: { status: normalizedStatus, search: search || null, league: league || null, group }, updatedAt: new Date().toISOString(), }; if (group === "flat") { const flatMatches = []; for (const grp of results) { for (const m of grp.matches) { flatMatches.push({ competition: grp.competition, ...m }); } } return { summary, matches: flatMatches }; } return { summary, competitions: results }; } /** * Mengambil rincian detail pertandingan (lineup, event gol/kartu, wasit, stats) */ async function scrapeGoalMatchDetail(identifier) { let targetUrl = ""; if (typeof identifier === "string" && identifier.startsWith("http")) { targetUrl = identifier; } else if (typeof identifier === "object") { const { slug, id, url } = identifier; if (url) targetUrl = url; else if (slug && id) targetUrl = `${GOAL_BASE}/id/pertandingan/${encodeURIComponent(slug)}/${encodeURIComponent(id)}`; else if (id) targetUrl = `${GOAL_BASE}/id/pertandingan/_/${encodeURIComponent(id)}`; } if (!targetUrl) { throw new Error("Sertakan { id, slug } atau url pertandingan Goal.com."); } const res = await axios.get(targetUrl, { headers: { "User-Agent": USER_AGENT, "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7", }, timeout: 15000, }); const $ = cheerio.load(res.data); const nextDataRaw = $("#__NEXT_DATA__").html(); if (!nextDataRaw) { throw new Error("Gagal mengekstrak data dari Goal.com: __NEXT_DATA__ tidak ditemukan."); } const parsed = JSON.parse(nextDataRaw); const match = parsed.props?.pageProps?.content?.match; if (!match) { throw new Error("Data match tidak ditemukan."); } return { id: match.id, status: match.status, startDate: match.startDate, competition: match.competition, venue: match.venue, round: match.round, referee: match.referee, teamHome: { ...match.teamA, score: match.score?.teamA ?? null }, teamAway: { ...match.teamB, score: match.score?.teamB ?? null }, period: match.period, scorers: match.scorers, events: match.events, stats: match.stats, lineups: match.lineups, }; } if (typeof module !== "undefined" && module.exports) { module.exports = { scrapeGoalLivescore, scrapeGoalMatchDetail, }; } // Contoh eksekusi langsung: if (require.main === module) { (async () => { console.log("=== Goal.com Livescore & Match Scraper ==="); const res = await scrapeGoalLivescore({ status: "all" }); console.log(`Total Kompetisi: ${res.summary.totalCompetitions} | Total Laga: ${res.summary.totalMatches} | Laga LIVE: ${res.summary.liveMatches}`); })(); }