SHARE
TWEET

Untitled

a guest Jun 18th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const config = require('../config.js');
  2. const prompts = require('prompts');
  3. const fsp = require('fs').promises;
  4. const beautify = require("json-beautify");
  5. const parsingData = require('./functions.js');
  6. const puppeteer = require('puppeteer');
  7. const json2xls = require('json2xls');
  8.  
  9. (async () => {
  10.     const userResponse = await prompts(config.userQuestions);
  11.     const profilesForParsing = userResponse.usernameForParsing.split(',');
  12.  
  13.     await interval(userResponse, profilesForParsing);
  14. })();
  15.  
  16. async function interval(userResponse, profilesForParsing) {
  17.     for (let i = 0; i < profilesForParsing.length; i++) {
  18.         const oddsPortalProfile = `https://www.oddsportal.com/profile/${profilesForParsing[i].trim()}/my-predictions/next/`;
  19.         const oddsPortalLogin = 'https://www.oddsportal.com/login/';
  20.         const oddsPortalUsername = `${userResponse.oddsPortalUsername}`;
  21.         const oddsPortalPassword = `${userResponse.oddsPortalPassword}`;
  22.         const timeZone = 'https://www.oddsportal.com/set-timezone/31/';
  23.  
  24.         const browser = await puppeteer.launch({headless: false});
  25.         const page = await browser.newPage();
  26.         // Login
  27.         await page.goto(oddsPortalLogin, {waitUntil: 'domcontentloaded'});
  28.         // Login data
  29.         await page.type('#login-username1', oddsPortalUsername);
  30.         await page.type('#login-password1', oddsPortalPassword);
  31.         await page.waitFor(1000);
  32.         await Promise.all([
  33.             page.waitForNavigation({waitUntil: 'domcontentloaded'}),
  34.             page.click('#col-content > div:nth-child(3) > div > form > div:nth-child(3) > button'),
  35.         ]);
  36.         // Change time zone if needed
  37.         const timeZoneCheck = await page.evaluate(() => {
  38.             const currentTimeZone = document.querySelector('#user-header-timezone-expander > span');
  39.             return currentTimeZone.textContent.includes('GMT 0');
  40.         });
  41.         if (!timeZoneCheck) {
  42.             await page.goto(timeZone, {waitUntil: 'domcontentloaded'});
  43.         }
  44.         // Go to Odds Profile
  45.         await page.goto(oddsPortalProfile, {waitUntil: 'domcontentloaded'});
  46.         // Check pagination
  47.         const pages = await page.evaluate(() => {
  48.             if (document.querySelector('#pagination')) {
  49.                 return document.querySelector('#pagination').lastChild.getAttribute('x-page');
  50.             } else {
  51.                 return false;
  52.             }
  53.         });
  54.  
  55.         let result = [];
  56.         if (pages === false) {
  57.             await parsingData(page, config, result);
  58.         } else {
  59.             for (let i = 1; i <= pages; i++) {
  60.                 await page.goto(`${oddsPortalProfile}page/${i}/`, {waitUntil: 'domcontentloaded'});
  61.                 await parsingData(page, config, result);
  62.             }
  63.         }
  64.  
  65.         if (result.length) {
  66.             result = await result.flat();
  67.             const xls = await json2xls(result);
  68.             try {
  69.                 await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.xlsx`, xls, 'binary');
  70.                 // await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.json`, beautify(result, null, 2, 100));
  71.             } catch (e) {
  72.                 if (e.code === 'ENOENT') {
  73.                     await fsp.mkdir('logs');
  74.                     await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.xlsx`, xls, 'binary');
  75.                     // await fsp.writeFile(`logs/${profilesForParsing[i].trim()}.json`, beautify(result, null, 2, 100));
  76.                 } else {
  77.                     console.error(e);
  78.                 }
  79.             }
  80.         }
  81.         console.log(`${profilesForParsing[i].trim()} parsed`);
  82.         await browser.close();
  83.     }
  84.     await setTimeout(() => {
  85.         interval(userResponse, profilesForParsing);
  86.     }, 5000);
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top