Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
5,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const puppeteer = require('puppeteer');
  2.  
  3. class NetflixService {
  4.   constructor(credentials) {
  5.     this.credentials = credentials;
  6.     this.endpoints = {
  7.       activity: 'https://www.netflix.com/viewingactivity',
  8.       login: 'https://www.netflix.com/pl/login',
  9.       users: 'https://www.netflix.com/profiles/manage'
  10.     }
  11.   }
  12.  
  13.   async getActivityInfo() {
  14.     // Todo: Remove existing files
  15.     await this.fetchActivityFiles();
  16.     // Todo: Parse downloaded files and return json data
  17.   }
  18.  
  19.   async fetchActivityFiles() {
  20.     const browser = await puppeteer.launch({ headless: false });
  21.     const page = await browser.newPage();
  22.     await page.setJavaScriptEnabled(true);
  23.     await page._client.send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: './downloads' });
  24.  
  25.     await this.signIn(page, this.credentials);
  26.    
  27.     for (let i = 0; i < 4; ++i) {
  28.       // Todo: Check if this is a user or 'create user' button
  29.       await this.switchUser(page, i);
  30.       await this.fetchActivity(page);
  31.     }
  32.   }
  33.  
  34.   async signIn(page, credentials) {
  35.     const { identifier, password } = credentials;
  36.  
  37.     await page.goto(this.endpoints.login);
  38.     await page.type('#id_userLoginId', identifier);
  39.     await page.type('#id_password', password);
  40.     await page.$eval('[data-uia=login-submit-button]', element => element.click());
  41.   }
  42.  
  43.   async switchUser(page, index) {
  44.     await page.goto(this.endpoints.users);
  45.     await page.$eval('.profile-button a', element => element.click());
  46.     await page.$eval(`.profile:nth-of-type(${index + 1}) .profile-link`, element => element.click());
  47.     await page.waitForNavigation();
  48.   }
  49.  
  50.   async fetchActivity(page) {
  51.     await page.goto(this.endpoints.activity);
  52.     await page.$eval('.viewing-activity-footer-download', element => element.click());
  53.     await page.waitForNavigation();
  54.   }
  55. }
  56.  
  57. module.exports = NetflixService;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement