Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const config = require('../config.js');
- const prompts = require('prompts');
- const fsp = require('fs').promises;
- const beautify = require("json-beautify");
- const parsingData = require('./functions.js');
- const puppeteer = require('puppeteer');
- const json2xls = require('json2xls');
- (async () => {
- const userResponse = await prompts(config.userQuestions);
- const profilesForParsing = userResponse.usernameForParsing.split(',');
- await interval(userResponse, profilesForParsing);
- })();
- async function interval(userResponse, profilesForParsing) {
- for (let i = 0; i < profilesForParsing.length; i++) {
- const oddsPortalProfile = `https://www.oddsportal.com/profile/${profilesForParsing[i].trim()}/my-predictions/next/`;
- const oddsPortalLogin = 'https://www.oddsportal.com/login/';
- const oddsPortalUsername = `${userResponse.oddsPortalUsername}`;
- const oddsPortalPassword = `${userResponse.oddsPortalPassword}`;
- const timeZone = 'https://www.oddsportal.com/set-timezone/31/';
- const browser = await puppeteer.launch({headless: false});
- const page = await browser.newPage();
- // Login
- await page.goto(oddsPortalLogin, {waitUntil: 'domcontentloaded'});
- // Login data
- await page.type('#login-username1', oddsPortalUsername);
- await page.type('#login-password1', oddsPortalPassword);
- await page.waitFor(1000);
- await Promise.all([
- page.waitForNavigation({waitUntil: 'domcontentloaded'}),
- page.click('#col-content > div:nth-child(3) > div > form > div:nth-child(3) > button'),
- ]);
- // Change time zone if needed
- const timeZoneCheck = await page.evaluate(() => {
- const currentTimeZone = document.querySelector('#user-header-timezone-expander > span');
- return currentTimeZone.textContent.includes('GMT 0');
- });
- if (!timeZoneCheck) {
- await page.goto(timeZone, {waitUntil: 'domcontentloaded'});
- }
- // Go to Odds Profile
- await page.goto(oddsPortalProfile, {waitUntil: 'domcontentloaded'});
- // Check pagination
- const pages = await page.evaluate(() => {
- if (document.querySelector('#pagination')) {
- return document.querySelector('#pagination').lastChild.getAttribute('x-page');
- } else {
- return false;
- }
- });
- let result = [];
- if (pages === false) {
- await parsingData(page, config, result);
- } else {
- for (let i = 1; i <= pages; i++) {
- await page.goto(`${oddsPortalProfile}page/${i}/`, {waitUntil: 'domcontentloaded'});
- await parsingData(page, config, result);
- }
- }
- if (result.length) {
- result = await result.flat();
- const xls = await json2xls(result);
- try {
- await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.xlsx`, xls, 'binary');
- // await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.json`, beautify(result, null, 2, 100));
- } catch (e) {
- if (e.code === 'ENOENT') {
- await fsp.mkdir('logs');
- await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.xlsx`, xls, 'binary');
- // await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.json`, beautify(result, null, 2, 100));
- } else {
- console.error(e);
- }
- }
- }
- console.log(`${profilesForParsing[i].trim()} parsed`);
- await browser.close();
- }
- await setTimeout(() => {
- interval(userResponse, profilesForParsing);
- }, 5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement