UNPKG

pcso-lotto-web-scraper

Version:
124 lines (123 loc) 5.96 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsdom_1 = require("jsdom"); const qs_1 = __importDefault(require("qs")); class LottoScraper { constructor(url = 'https://www.pcso.gov.ph/SearchLottoResult.aspx', headers = { origin: 'https://www.pcso.gov.ph', referer: 'https://www.pcso.gov.ph/SearchLottoResult.aspx', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0', }) { this.url = url; this.headers = headers; } get(payload) { return __awaiter(this, void 0, void 0, function* () { const initialDomWindow = yield this.getInitialDomWindow(); if (!initialDomWindow) return []; const resultDomWindow = yield this.getResultDomWindow(initialDomWindow, new Date(payload.from), payload.to ? new Date(payload.to) : undefined); if (!resultDomWindow) return []; const results = this.extractResults(resultDomWindow, payload); return results; }); } extractResults(domWindow, payload) { var _a; const tbody = domWindow.document.querySelector('table > tbody'); const rows = Array.from((_a = tbody === null || tbody === void 0 ? void 0 : tbody.querySelectorAll('tr')) !== null && _a !== void 0 ? _a : []); const rawData = rows .map((row) => { var _a; const cells = Array.from((_a = row.querySelectorAll('td')) !== null && _a !== void 0 ? _a : []); const raw = cells.map((cell) => { var _a; return (_a = cell.textContent) === null || _a === void 0 ? void 0 : _a.trim(); }); return raw; }) .filter((data) => data.length === 5); const results = rawData .map((data) => ({ game: data.at(0), combinations: data .at(1) .split('-') .map((n) => parseInt(n)), drawDate: new Date(data.at(2)), jackpot: parseFloat(data.at(3).replace(/,/g, '')), winners: parseInt(data.at(4)), })) .filter((result) => (payload.filter ? payload.filter(result) : true)); return results; } getDomWindow(request) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield request; const html = yield response.text(); const jsDom = new jsdom_1.JSDOM(html); const domWindow = jsDom.window; return domWindow; } catch (error) { return null; } }); } getResultDomWindow(initialDomWindow_1, from_1) { return __awaiter(this, arguments, void 0, function* (initialDomWindow, from, to = new Date()) { const state = { __VIEWSTATE: initialDomWindow.document.querySelector('#__VIEWSTATE').value, __VIEWSTATEGENERATOR: initialDomWindow.document.querySelector('#__VIEWSTATEGENERATOR').value, __EVENTVALIDATION: initialDomWindow.document.querySelector('#__EVENTVALIDATION').value, }; const formBody = this.createFormBody(state, from, to); const domWindow = yield this.getDomWindow(fetch(this.url, { method: 'POST', headers: Object.assign(Object.assign({}, this.headers), { 'content-type': 'application/x-www-form-urlencoded' }), body: formBody, })); return domWindow; }); } createFormBody(state, from, to) { const [fromMonth, fromDate, fromYear] = Intl.DateTimeFormat('en-US', { dateStyle: 'long', }) .format(from) .replace(',', '') .split(' '); const [toMonth, toDate, toYear] = Intl.DateTimeFormat('en-US', { dateStyle: 'long', }) .format(to) .replace(',', '') .split(' '); const body = qs_1.default.stringify(Object.assign(Object.assign({}, state), { ctl00$ctl00$cphContainer$cpContent$ddlStartMonth: fromMonth, ctl00$ctl00$cphContainer$cpContent$ddlStartDate: fromDate, $ctl00$cphContainer$cpContent$ddlStartYear: fromYear, ctl00$ctl00$cphContainer$cpContent$ddlEndMonth: toMonth, ctl00$ctl00$cphContainer$cpContent$ddlEndDay: toDate, ctl00$ctl00$cphContainer$cpContent$ddlEndYear: toYear, ctl00$ctl00$cphContainer$cpContent$ddlSelectGame: 0, ctl00$ctl00$cphContainer$cpContent$btnSearch: 'Search Lotto' })); return body; } getInitialDomWindow() { return __awaiter(this, void 0, void 0, function* () { if (this.initialDomWindow) return this.initialDomWindow; const domWindow = yield this.getDomWindow(fetch(this.url, { headers: this.headers, })); if (domWindow) this.initialDomWindow = domWindow; return domWindow; }); } } exports.default = LottoScraper;