Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const puppeteer = require('puppeteer');
- class NetflixService {
- constructor(credentials) {
- this.credentials = credentials;
- this.endpoints = {
- activity: 'https://www.netflix.com/viewingactivity',
- login: 'https://www.netflix.com/pl/login',
- users: 'https://www.netflix.com/profiles/manage'
- }
- }
- async getActivityInfo() {
- // Todo: Remove existing files
- await this.fetchActivityFiles();
- // Todo: Parse downloaded files and return json data
- }
- async fetchActivityFiles() {
- const browser = await puppeteer.launch({ headless: false });
- const page = await browser.newPage();
- await page.setJavaScriptEnabled(true);
- await page._client.send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: './downloads' });
- await this.signIn(page, this.credentials);
- for (let i = 0; i < 4; ++i) {
- // Todo: Check if this is a user or 'create user' button
- await this.switchUser(page, i);
- await this.fetchActivity(page);
- }
- }
- async signIn(page, credentials) {
- const { identifier, password } = credentials;
- await page.goto(this.endpoints.login);
- await page.type('#id_userLoginId', identifier);
- await page.type('#id_password', password);
- await page.$eval('[data-uia=login-submit-button]', element => element.click());
- }
- async switchUser(page, index) {
- await page.goto(this.endpoints.users);
- await page.$eval('.profile-button a', element => element.click());
- await page.$eval(`.profile:nth-of-type(${index + 1}) .profile-link`, element => element.click());
- await page.waitForNavigation();
- }
- async fetchActivity(page) {
- await page.goto(this.endpoints.activity);
- await page.$eval('.viewing-activity-footer-download', element => element.click());
- await page.waitForNavigation();
- }
- }
- module.exports = NetflixService;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement