Skip to content

Commit

Permalink
Merge pull request #14 from code-yeongyu/refactor-imports
Browse files Browse the repository at this point in the history
`Naver` 앱의 naming 을 변경하고, `rxjs` 의존성을 제거합니다.
  • Loading branch information
code-yeongyu authored Feb 10, 2022
2 parents 38fc8e1 + 582b1b3 commit 9f2ee88
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 148 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"editor.tabSize": 2
"editor.tabSize": 2,
"cSpell.words": ["captchaimg", "catcha", "Interactor", "networkidle"]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import { NaverApp } from "trackpurchase";
const browser = await puppeteer.launch();
const page = await browser.newPage();

const module = NaverApp.ModuleFactory.create(page);
const crawlService = new NaverApp.Service(module);
const module = NaverApp.NaverModuleFactory.create(page);
const crawlService = new NaverApp.NaverService(module);

await crawlService.normalLogin(id, password, 100);
const history = await crawlService.getHistory();
Expand Down
4 changes: 2 additions & 2 deletions example/naver/printPaymentHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const printNaverPayHistory = async (id: string, password: string) => {
await page.setViewport({ height: 800, width: 1200 });
await page.setUserAgent(MOBILE_UA);

const module = NaverApp.ModuleFactory.create(page);
const crawlService = new NaverApp.Service(module);
const module = NaverApp.NaverModuleFactory.create(page);
const crawlService = new NaverApp.NaverService(module);

await crawlService.normalLogin(id, password, 100);

Expand Down
73 changes: 0 additions & 73 deletions example/naver/reactivelyPrintPaymentHistory.ts

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trackpurchase",
"version": "1.2.0",
"version": "2.0.0",
"main": "dist/index.js",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -35,8 +35,7 @@
},
"dependencies": {
"axios": "^0.25.0",
"puppeteer": "^12.0.1",
"rxjs": "^7.5.2"
"puppeteer": "^12.0.1"
},
"devDependencies": {
"@babel/core": "^7.16.5",
Expand Down
20 changes: 12 additions & 8 deletions src/app/naver/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { NaverModule } from "./module";
import ModuleFactory from "./moduleFactory";
import URLChanger from "./urlChanger";
import PageInteractor, { LoginEvent, CaptchaStatus } from "./pageInteractor";
import Service from "./service";
import { NaverModuleFactory } from "./moduleFactory";
import { NaverURLChanger } from "./urlChanger";
import {
LoginEvent,
CaptchaStatus,
NaverPageInteractor,
} from "./pageInteractor";
import { NaverService } from "./service";
import { NaverScraper } from "./scraper";
import { NaverParser } from "./parser";

export {
NaverModule,
ModuleFactory,
URLChanger,
PageInteractor,
Service,
NaverModuleFactory,
NaverURLChanger,
NaverPageInteractor,
NaverService,
LoginEvent,
CaptchaStatus,
NaverScraper,
Expand Down
13 changes: 9 additions & 4 deletions src/app/naver/module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { URLChanger, PageInteractor, NaverScraper, NaverParser } from ".";
import {
NaverURLChanger,
NaverPageInteractor,
NaverScraper,
NaverParser,
} from ".";

export default interface Module {
readonly urlChanger: URLChanger;
readonly pageInteractor: PageInteractor;
export interface NaverModule {
readonly urlChanger: NaverURLChanger;
readonly pageInteractor: NaverPageInteractor;
readonly scraper: NaverScraper;
readonly parser: NaverParser;
}
14 changes: 7 additions & 7 deletions src/app/naver/moduleFactory.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import puppeteer from "puppeteer";
import {
Module,
URLChanger,
PageInteractor,
NaverModule,
NaverURLChanger,
NaverPageInteractor,
NaverScraper,
NaverParser,
} from ".";

export default class ModuleFactory {
static create(page: puppeteer.Page): Module {
const urlChanger = new URLChanger(page);
const pageInteractor = new PageInteractor(page);
export class NaverModuleFactory {
static create(page: puppeteer.Page): NaverModule {
const urlChanger = new NaverURLChanger(page);
const pageInteractor = new NaverPageInteractor(page);
const scraper = new NaverScraper();
const parser = new NaverParser();

Expand Down
3 changes: 1 addition & 2 deletions src/app/naver/pageInteractor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import puppeteer from "puppeteer";
import { ElementParser } from ".";

export type LoginEvent =
| "success"
Expand All @@ -12,7 +11,7 @@ export interface CaptchaStatus {
readonly question: string;
}

export default class PageInteractor {
export class NaverPageInteractor {
constructor(private readonly page: puppeteer.Page) {
this.page = page;
}
Expand Down
35 changes: 4 additions & 31 deletions src/app/naver/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { PaymentHistory } from "app/common";
import { CommonResponse } from "app/common/types/response";
import {
concat,
defer,
distinctUntilChanged,
from,
interval,
mergeMap,
takeWhile,
} from "rxjs";
import { Module } from ".";
import { NaverModule } from ".";

export default class Service {
export class NaverService {
cookies?: string;
constructor(private readonly module: Module) {
constructor(private readonly module: NaverModule) {
this.module = module;
}

Expand All @@ -23,28 +14,10 @@ export default class Service {
this.cookies = await this.module.pageInteractor.getCookies();
}

interactiveLogin(id: string, password: string, delay?: number) {
const login$ = defer(() => from(this.normalLogin(id, password, delay)));
const loginStatus$ = interval(500)
.pipe(mergeMap(() => this.module.pageInteractor.getLoginStatus()))
.pipe(
distinctUntilChanged(),
takeWhile((loginStatus) => loginStatus !== "success")
);
const captchaStatus$ = interval(500)
.pipe(mergeMap(() => this.module.pageInteractor.getCaptchaStatus()))
.pipe(
distinctUntilChanged((a, b) => a?.question === b?.question),
takeWhile((captchaStatus) => captchaStatus !== null)
);

const result$ = concat(login$, captchaStatus$, loginStatus$);
return result$;
}

private async isResponseValid(response: CommonResponse) {
return response.status === 200;
}

private async getHistoryResult(response: CommonResponse) {
if (!this.isResponseValid(response)) {
throw new Error(`Invalid response: ${response.status} ${response.data}`);
Expand Down
4 changes: 2 additions & 2 deletions src/app/naver/urlChanger.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import URLChanger from "./urlChanger";
import { NaverURLChanger } from "./urlChanger";

describe("URLChanger", () => {
describe("moveToLoginURL", () => {
it("Should move page to login", async () => {
// given
const urlChanger = new URLChanger(page);
const urlChanger = new NaverURLChanger(page);
const pageSpy = jest.spyOn(page, "goto");

// when
Expand Down
2 changes: 1 addition & 1 deletion src/app/naver/urlChanger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import puppeteer from "puppeteer";

export default class URLChanger {
export class NaverURLChanger {
constructor(private readonly page: puppeteer.Page) {
this.page = page;
}
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4215,13 +4215,6 @@ rxjs@^7.1.0:
dependencies:
tslib "~2.1.0"

rxjs@^7.5.2:
version "7.5.2"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.5.2.tgz#11e4a3a1dfad85dbf7fb6e33cbba17668497490b"
integrity sha512-PwDt186XaL3QN5qXj/H9DGyHhP3/RYYgZZwqBv9Tv8rsAaiwFH1IsJJlcgD37J7UW5a6O67qX0KWKS3/pu0m4w==
dependencies:
tslib "^2.1.0"

safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
Expand Down Expand Up @@ -4570,11 +4563,6 @@ tsc@^2.0.3:
resolved "https://registry.yarnpkg.com/tsc/-/tsc-2.0.3.tgz#037fe579e3bd67a5cbdaa604b43c6c1991b04bef"
integrity sha512-SN+9zBUtrpUcOpaUO7GjkEHgWtf22c7FKbKCA4e858eEM7Qz86rRDpgOU2lBIDf0fLCsEg65ms899UMUIB2+Ow==

tslib@^2.1.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

tslib@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"
Expand Down

0 comments on commit 9f2ee88

Please sign in to comment.