-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Session + Page classes and rename Browser into Scraper
- Loading branch information
Showing
10 changed files
with
200 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
8.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"build:es2015": "tsc --project build/tsconfig.es2015.json", | ||
"build": "rm -rf dist && yarn build:es5 && yarn build:es2015", | ||
"build:example": "rm -rf example/dist && tsc --project build/tsconfig.example.json", | ||
"typecheck": "tsc --noEmit --project tsconfig.json", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
|
@@ -24,6 +25,9 @@ | |
"ts-node-dev": "^1.0.0-pre.32", | ||
"typescript": "^3.3.4000" | ||
}, | ||
"engines": { | ||
"node": ">=8.5.0" | ||
}, | ||
"repository": "[email protected]:devlucky/luckybot.git", | ||
"author": "devlucky", | ||
"license": "MIT", | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const HOST = 'https://www.instagram.com'; | ||
export const DEBUG = process.env.NODE_ENV !== 'production' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import puppeteer, { Browser } from 'puppeteer'; | ||
import { Page } from "puppeteer"; | ||
import { DEBUG } from './config'; | ||
|
||
export class PageProvider { | ||
// those need to be static since we don't want to have multiple pages at the same time | ||
static page?: Page; | ||
static browser?: Browser; | ||
|
||
private async createPage() { | ||
const browser = await puppeteer.launch({ headless: !DEBUG }); | ||
const page = await browser.newPage(); | ||
const userAgent = await browser.userAgent(); | ||
|
||
await page.setViewport({width: 1024, height: 768}); | ||
await page.setUserAgent(userAgent.replace('Headless', '')); | ||
|
||
PageProvider.browser = browser; | ||
PageProvider.page = page; | ||
|
||
return page; | ||
} | ||
|
||
getPage(): Promise<Page> { | ||
const {page} = PageProvider; | ||
|
||
if (page) return Promise.resolve(page); | ||
|
||
return this.createPage(); | ||
} | ||
|
||
async close() { | ||
const {page, browser} = PageProvider; | ||
|
||
page && await page.close() | ||
browser && await browser.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Page, Cookie } from "puppeteer"; | ||
import { HOST } from "./config"; | ||
import { PageProvider } from "./page"; | ||
|
||
export class Session { | ||
isLoggedIn: boolean = false; | ||
pageProvider: PageProvider; | ||
page?: Page; | ||
|
||
constructor() { | ||
this.pageProvider = new PageProvider(); | ||
} | ||
|
||
async login(userName: string, password: string): Promise<Cookie[]> { | ||
const page = await this.pageProvider.getPage(); | ||
|
||
if (this.isLoggedIn) { | ||
return page.cookies(); | ||
}; | ||
|
||
await page.goto(`${HOST}/accounts/login`); | ||
|
||
const userInput = await page.waitForSelector('input._2hvTZ'); | ||
const passInput = await page.waitForSelector('input[type=password]'); | ||
const logInButton = await page.waitForSelector('button[type="submit"]'); | ||
|
||
if (!userInput || !passInput || !logInButton) { | ||
throw new Error('Error loging'); | ||
} | ||
|
||
await userInput.type(userName); | ||
await passInput.type(password); | ||
await logInButton.click() | ||
await page.waitForNavigation(); | ||
|
||
const cookies = await page.cookies(); | ||
|
||
this.isLoggedIn = true; | ||
return cookies; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// TODO: xhr call to private rest api goes here | ||
|
||
export class RestApi { | ||
|
||
} |
Oops, something went wrong.