Skip to content

Commit 6dd1b0c

Browse files
authored
Merge pull request #10 from code-yeongyu/naver-parser
`NaverPay` 의 구매기록을 조회 할 경우 `pupeteer` 대신 `axios` 를 활용해 `http request` 를 보내어 parsing 하도록 변경합니다.
2 parents ad127ec + c26507e commit 6dd1b0c

20 files changed

+682
-18
lines changed

example/naver/printPaymentHistory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const printNaverPayHistory = async (id: string, password: string) => {
1818
const module = NaverApp.ModuleFactory.create(page);
1919
const crawlService = new NaverApp.Service(module);
2020

21-
await crawlService.normalLogin(id, password);
21+
await crawlService.normalLogin(id, password, 100);
2222

23+
browser.close();
2324
const history = await crawlService.getHistory();
2425
console.log(history);
2526
};

example/naver/reactivelyPrintPaymentHistory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import puppeteer from "puppeteer";
2-
import { NaverApp } from ".";
2+
import { NaverApp } from "trackpurchase";
33

44
import readline from "readline";
55
import { concat, defer, filter, from, tap } from "rxjs";
6-
import { CaptchaStatus } from "app/naver";
6+
import { CaptchaStatus } from "trackpurchase/app/naver";
77

88
const printNaverPayHistory = async (id: string, password: string) => {
99
const MOBILE_UA =
@@ -21,7 +21,7 @@ const printNaverPayHistory = async (id: string, password: string) => {
2121
const module = NaverApp.ModuleFactory.create(page);
2222
const crawlService = new NaverApp.Service(module);
2323

24-
const loginEvent$ = crawlService.interactiveLogin(id, password);
24+
const loginEvent$ = crawlService.interactiveLogin(id, password, 100);
2525
const history$ = defer(() => from(crawlService.getHistory()));
2626
const closePage$ = defer(() => from(page.close()));
2727
const closeBrowser$ = defer(() => from(browser.close()));

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "trackpurchase",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"main": "dist/index.js",
55
"license": "MIT",
66
"repository": {
@@ -34,6 +34,7 @@
3434
"build": "tsc -p ."
3535
},
3636
"dependencies": {
37+
"axios": "^0.25.0",
3738
"puppeteer": "^12.0.1",
3839
"rxjs": "^7.5.2"
3940
},
@@ -43,6 +44,7 @@
4344
"@babel/preset-env": "^7.16.5",
4445
"@babel/preset-typescript": "^7.16.5",
4546
"@babel/runtime": "^7.16.5",
47+
"@types/axios": "^0.14.0",
4648
"@types/expect-puppeteer": "^4.4.7",
4749
"@types/jest": "^27.0.3",
4850
"@types/jest-environment-puppeteer": "^4.4.1",

src/app/common/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import Module from "./module";
2-
import PaymentHistory from "./paymentHistory";
2+
import PaymentHistory from "./types/paymentHistory";
33

44
export { Module, PaymentHistory };

src/app/common/types/response.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface CommonResponse {
2+
readonly status: number;
3+
readonly data: string;
4+
}

src/app/naver/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import URLChanger from "./urlChanger";
44
import PageInteractor, { LoginEvent, CaptchaStatus } from "./pageInteractor";
55
import ElementParser from "./elementParser";
66
import Service from "./service";
7+
import { NaverScraper } from "./scraper";
8+
import { NaverParser } from "./parser";
79

810
export {
911
Module,
@@ -14,4 +16,6 @@ export {
1416
Service,
1517
LoginEvent,
1618
CaptchaStatus,
19+
NaverScraper,
20+
NaverParser,
1721
};

src/app/naver/module.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { URLChanger, PageInteractor, ElementParser } from ".";
1+
import {
2+
URLChanger,
3+
PageInteractor,
4+
ElementParser,
5+
NaverScraper,
6+
NaverParser,
7+
} from ".";
28
import { Module as BaseModule } from "../common";
39

410
export default interface Module extends BaseModule {
511
readonly urlChanger: URLChanger;
612
readonly pageInteractor: PageInteractor;
713
readonly elementParser: ElementParser;
14+
readonly scraper: NaverScraper;
15+
readonly parser: NaverParser;
816
}

src/app/naver/moduleFactory.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import puppeteer from "puppeteer";
2-
import { Module, URLChanger, ElementParser, PageInteractor } from ".";
2+
import {
3+
Module,
4+
URLChanger,
5+
ElementParser,
6+
PageInteractor,
7+
NaverScraper,
8+
NaverParser,
9+
} from ".";
310

411
export default class ModuleFactory {
512
static create(page: puppeteer.Page): Module {
613
const urlChanger = new URLChanger(page);
714
const elementParser = new ElementParser(page);
815
const pageInteractor = new PageInteractor(page, elementParser);
16+
const scraper = new NaverScraper();
17+
const parser = new NaverParser();
918

1019
return {
1120
urlChanger,
1221
pageInteractor,
1322
elementParser,
23+
scraper,
24+
parser,
1425
};
1526
}
1627
}

src/app/naver/pageInteractor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export default class PageInteractor {
5050
await this.typeLoginInfo(id, password, delay || 200);
5151
}
5252

53+
async getCookies() {
54+
const cookies = await this.page.cookies();
55+
let cookieString = "";
56+
for (const cookie of cookies) {
57+
cookieString += `${cookie.name}=${cookie.value}; `;
58+
}
59+
return cookieString;
60+
}
61+
5362
async getLoginStatus(loginURL?: string): Promise<LoginEvent> {
5463
const isLoginPage = this.page
5564
.url()

0 commit comments

Comments
 (0)