@rich-automation/lotto 0.1.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -0
- package/lib/cjs/constants/index.js +1 -1
- package/lib/cjs/constants/selectors.js +1 -0
- package/lib/cjs/controllers/playwright/index.js +14 -10
- package/lib/cjs/index.js +9 -3
- package/lib/cjs/lottoService.js +12 -6
- package/lib/cjs/utils/checkWinning.js +2 -11
- package/lib/cjs/utils/getCheckWinningLink.js +1 -1
- package/lib/cjs/utils/{getCurrentLottoRound.js → getLastLottoRound.js} +3 -3
- package/lib/cjs/utils/getNextLottoRound.js +11 -0
- package/lib/esm/constants/index.js +1 -1
- package/lib/esm/constants/selectors.js +1 -0
- package/lib/esm/controllers/playwright/index.js +14 -10
- package/lib/esm/index.js +4 -1
- package/lib/esm/lottoService.js +12 -6
- package/lib/esm/utils/checkWinning.js +2 -11
- package/lib/esm/utils/getCheckWinningLink.js +1 -1
- package/lib/esm/utils/{getCurrentLottoRound.js → getLastLottoRound.js} +1 -1
- package/lib/esm/utils/getNextLottoRound.js +7 -0
- package/lib/typescript/constants/selectors.d.ts +1 -0
- package/lib/typescript/controllers/playwright/index.d.ts +4 -3
- package/lib/typescript/index.d.ts +4 -1
- package/lib/typescript/lottoService.d.ts +3 -3
- package/lib/typescript/types.d.ts +3 -3
- package/lib/typescript/utils/checkWinning.d.ts +2 -2
- package/lib/typescript/utils/getCheckWinningLink.d.ts +1 -1
- package/lib/typescript/utils/getLastLottoRound.d.ts +1 -0
- package/lib/typescript/utils/getNextLottoRound.d.ts +1 -0
- package/package.json +1 -1
- package/lib/typescript/utils/getCurrentLottoRound.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,4 +1,121 @@
|
|
|
1
1
|
# @rich-automation/lotto
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@rich-automation/lotto)
|
|
3
4
|
[](https://github.com/rich-automation/lotto-module/actions/workflows/ci.yml)
|
|
4
5
|
[](https://codecov.io/gh/rich-automation/lotto-module)
|
|
6
|
+
|
|
7
|
+
`@rich-automation/lotto` 패키지는 headless browser를 이용해 js환경에서 자동으로 로또를 구매할 수 있는 인터페이스를 제공합니다.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
```shell
|
|
13
|
+
# npm
|
|
14
|
+
npm install @rich-automation/lotto
|
|
15
|
+
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add @rich-automation/lotto
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Preparation
|
|
21
|
+
1. 내부적으로 playwright/puppeteer를 사용하므로 머신에 chromium 을 미리 설치해주셔야 합니다. ([link](https://github.com/rich-automation/lotto-module/blob/main/package.json#L38-L39))
|
|
22
|
+
2. [동행복권](https://dhlottery.co.kr/common.do?method=main) 사이트를 통해 구매가 이루어지며 예치금 충전은 지원하지 않으므로 미리 동행복권 계정에 예치금을 충전해두어야합니다.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
### 공통
|
|
26
|
+
lottoService 객체 인스턴스를 생성합니다.
|
|
27
|
+
```js
|
|
28
|
+
import { LottoService } from '@rich-automation/lotto';
|
|
29
|
+
|
|
30
|
+
const lottoService = new LottoService();
|
|
31
|
+
//...
|
|
32
|
+
```
|
|
33
|
+
LottoService 클래스의 생성자는 BrowserConfigs를 인수로 받을 수 있습니다. 인수로 전달된 configs는 다음과 같은 속성을 가질 수 있습니다:
|
|
34
|
+
|
|
35
|
+
- `controller(default: 'playwright')`: 내부적으로 어떤 컨트롤러(puppeteer/playwright)를 사용할 지 지정할 수 있습니다.
|
|
36
|
+
- `headless(default: false)`: 브라우저의 헤드리스 모드 여부를 설정합니다.
|
|
37
|
+
- `defaultViewport(default: { width: 1080, height: 1024 })`: 브라우저의 기본 뷰포트 크기를 설정합니다.
|
|
38
|
+
- `logLevel(default:2)`:콘솔 로그 수준을 설정합니다. (NONE = -1, ERROR = 0, WARN = 1, INFO = 2, DEBUG = 3)
|
|
39
|
+
```js
|
|
40
|
+
//example
|
|
41
|
+
import {LottoService, LogLevel} from '@rich-automation/lotto'
|
|
42
|
+
|
|
43
|
+
const lottoService = new LottoService({
|
|
44
|
+
headless: true,
|
|
45
|
+
defaultViewport: { width: 1280, height: 720 },
|
|
46
|
+
logLevel: LogLevel.DEBUG
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
### 구매
|
|
50
|
+
```js
|
|
51
|
+
import {LottoService} from "@rich-automation/lotto";
|
|
52
|
+
|
|
53
|
+
const ID = "<YOUR_ID>";
|
|
54
|
+
const PWD = "<YOUR_PASSWORD>";
|
|
55
|
+
|
|
56
|
+
const lottoService = new LottoService({
|
|
57
|
+
headless:true
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await lottoService.signIn(ID,PWD);
|
|
61
|
+
|
|
62
|
+
const numbers = await lottoService.purchase(5);
|
|
63
|
+
console.log(numbers); //[[ 1, 14, 21, 27, 30, 44 ],[ 4, 5, 27, 29, 40, 44 ],[ 9, 18, 19, 24, 38, 42 ],[ 4, 6, 13, 20, 38, 39 ],[ 8, 9, 10, 19, 32, 40 ]]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 당첨 확인
|
|
70
|
+
이번 회차 당첨 확인
|
|
71
|
+
```js
|
|
72
|
+
import {getLastLottoRound,LottoService} from "@rich-automation/lotto";
|
|
73
|
+
|
|
74
|
+
const numbers = "[[1,2,3,4,5,6],[5,6,7,8,9,10]";
|
|
75
|
+
|
|
76
|
+
const lottoService = new LottoService({
|
|
77
|
+
headless:true
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const currentRound = getLastLottoRound();
|
|
81
|
+
|
|
82
|
+
const result = await lottoService.check(numbers,currentRound)
|
|
83
|
+
console.log(result) //[{rank:1,matchedNumbers:[1,2,3,4,5,6]},{rank:5,matchedNumbers:[5,6]]
|
|
84
|
+
```
|
|
85
|
+
다음 회차 당첨 링크 생성
|
|
86
|
+
```js
|
|
87
|
+
import {getNextLottoRound,LottoService} from "@rich-automation/lotto";
|
|
88
|
+
|
|
89
|
+
const numbers = "[[1,2,3,4,5,6],[5,6,7,8,9,10]";
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
const lottoService = new LottoService({
|
|
93
|
+
headless:true
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const nextRound = getNextLottoRound();
|
|
97
|
+
const link = lottoService.getCheckWinningLink(numbers,nextRound);
|
|
98
|
+
console.log(link) //"https://dhlottery.co.kr/qr.do?method=winQr&v=1071q010203040506q050607080910";
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## Method
|
|
104
|
+
### signIn
|
|
105
|
+
- `(id: string, password: string) => Promise<string>`
|
|
106
|
+
- description: id와 pwd를 입력받아 동행복권에 로그인합니다. 성공할경우 로그인 쿠키를 반환합니다.
|
|
107
|
+
### signInWithCookie
|
|
108
|
+
- `(cookies: string) => Promise<string>`
|
|
109
|
+
- description: 로그인 쿠키를 입력받아 동행복권에 로그인합니다. 성공할경우 로그인 쿠키를 반환합니다.
|
|
110
|
+
### purchase
|
|
111
|
+
- `(amount?: number) => Promise<number[][]>`
|
|
112
|
+
- description: 구매할 게임 횟수를 입력받아 로또를 구매하고, 구매한 번호를 이차원 배열 형태로 반환합니다. amount는 1~5사이 값을 가집니다.
|
|
113
|
+
### check
|
|
114
|
+
- `(numbers: number[][], round?: number) => Promise<{ rank:number; matchedNumbers:number[] }[]>`
|
|
115
|
+
- description: 회차와 해당 회차에 구매한 로또번호를 입력받아 당첨 등수(rank)와 맞춘 번호(matchedNumbers)목록을 반환합니다. 회차를 지정하지 않으면 최신 회차를 기준으로 확인합니다.
|
|
116
|
+
### getCheckWinningLink
|
|
117
|
+
- `(numbers: number[][], round?: number) => string`
|
|
118
|
+
- description: 회차와 구매한 로또 번호를 입력받아 당첨 확인 링크를 생성합니다. 로또 번호는 해당 회차에 구매한 모든 게임을 이차원 배열 형태로 입력받습니다. 회차를 지정하지 않으면 다음 회차를 기준으로 링크를 생성합니다.
|
|
119
|
+
### destroy
|
|
120
|
+
- `() => Promise<void>`
|
|
121
|
+
- description: LottoService 인스턴스에서 사용한 브라우저 컨트롤러를 종료합니다.
|
|
@@ -5,6 +5,7 @@ exports.SELECTORS = {
|
|
|
5
5
|
ID_INPUT: '#userId',
|
|
6
6
|
PWD_INPUT: '#article > div:nth-child(2) > div > form > div > div.inner > fieldset > div.form > input[type=password]:nth-child(2)',
|
|
7
7
|
LOGIN_BUTTON: '#article > div:nth-child(2) > div > form > div > div.inner > fieldset > div.form > a',
|
|
8
|
+
ENVIRONMENT_ALERT_CONFIRM: 'input[value="확인"][onclick="javascript:closepopupLayerAlert();"]',
|
|
8
9
|
PURCHASE_TYPE_RANDOM_BTN: 'a[href="#divWay2Buy1"]#num2',
|
|
9
10
|
PURCHASE_AMOUNT_SELECT: 'select#amoundApply',
|
|
10
11
|
PURCHASE_AMOUNT_CONFIRM_BTN: 'input[value="확인"]#btnSelectNum',
|
|
@@ -16,10 +16,10 @@ const playwright_page_1 = require("./playwright.page");
|
|
|
16
16
|
const constants_1 = require("../../constants");
|
|
17
17
|
class PlaywrightController {
|
|
18
18
|
constructor(configs, logger) {
|
|
19
|
-
this.
|
|
19
|
+
this.getBrowserContext = () => __awaiter(this, void 0, void 0, function* () {
|
|
20
20
|
const p = (0, deferred_1.deferred)();
|
|
21
|
-
if (this.
|
|
22
|
-
p.resolve(this.
|
|
21
|
+
if (this.context) {
|
|
22
|
+
p.resolve(this.context);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
25
|
let retry = 0;
|
|
@@ -28,9 +28,9 @@ class PlaywrightController {
|
|
|
28
28
|
clearInterval(interval);
|
|
29
29
|
p.reject(new Error('Browser is not initialized'));
|
|
30
30
|
}
|
|
31
|
-
else if (this.
|
|
31
|
+
else if (this.context) {
|
|
32
32
|
clearInterval(interval);
|
|
33
|
-
p.resolve(this.
|
|
33
|
+
p.resolve(this.context);
|
|
34
34
|
}
|
|
35
35
|
retry++;
|
|
36
36
|
}, constants_1.CONST.BROWSER_INIT_RETRY_TIMEOUT);
|
|
@@ -38,7 +38,7 @@ class PlaywrightController {
|
|
|
38
38
|
return p.promise;
|
|
39
39
|
});
|
|
40
40
|
this.focus = (pageIndex = -1) => __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
const browser = yield this.
|
|
41
|
+
const browser = yield this.getBrowserContext();
|
|
42
42
|
const pages = browser.pages();
|
|
43
43
|
if (pages.length === 0) {
|
|
44
44
|
const page = yield browser.newPage();
|
|
@@ -53,11 +53,12 @@ class PlaywrightController {
|
|
|
53
53
|
}
|
|
54
54
|
});
|
|
55
55
|
this.close = () => __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
const
|
|
57
|
-
|
|
56
|
+
const context = yield this.getBrowserContext();
|
|
57
|
+
yield context.close();
|
|
58
|
+
return this.browser.close();
|
|
58
59
|
});
|
|
59
60
|
this.cleanPages = (remainingPageIndex) => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
const browser = yield this.
|
|
61
|
+
const browser = yield this.getBrowserContext();
|
|
61
62
|
const pages = browser.pages();
|
|
62
63
|
const promises = pages.map((page, index) => __awaiter(this, void 0, void 0, function* () {
|
|
63
64
|
if (!remainingPageIndex.includes(index)) {
|
|
@@ -68,7 +69,10 @@ class PlaywrightController {
|
|
|
68
69
|
});
|
|
69
70
|
this.configs = configs;
|
|
70
71
|
this.logger = logger;
|
|
71
|
-
playwright_1.chromium.launch(this.configs).then((browser) => __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
playwright_1.chromium.launch(this.configs).then((browser) => __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
this.browser = browser;
|
|
74
|
+
this.context = yield browser.newContext();
|
|
75
|
+
}));
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
exports.PlaywrightController = PlaywrightController;
|
package/lib/cjs/index.js
CHANGED
|
@@ -14,11 +14,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.checkWinning = exports.getCheckWinningLink = exports.getNextLottoRound = exports.getLastLottoRound = exports.LogLevel = exports.LottoService = void 0;
|
|
18
18
|
var lottoService_1 = require("./lottoService");
|
|
19
19
|
Object.defineProperty(exports, "LottoService", { enumerable: true, get: function () { return lottoService_1.LottoService; } });
|
|
20
|
-
var getCurrentLottoRound_1 = require("./utils/getCurrentLottoRound");
|
|
21
|
-
Object.defineProperty(exports, "getCurrentLottoRound", { enumerable: true, get: function () { return getCurrentLottoRound_1.getCurrentLottoRound; } });
|
|
22
20
|
var logger_1 = require("./logger");
|
|
23
21
|
Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return logger_1.LogLevel; } });
|
|
24
22
|
__exportStar(require("./types"), exports);
|
|
23
|
+
var getLastLottoRound_1 = require("./utils/getLastLottoRound");
|
|
24
|
+
Object.defineProperty(exports, "getLastLottoRound", { enumerable: true, get: function () { return getLastLottoRound_1.getLastLottoRound; } });
|
|
25
|
+
var getNextLottoRound_1 = require("./utils/getNextLottoRound");
|
|
26
|
+
Object.defineProperty(exports, "getNextLottoRound", { enumerable: true, get: function () { return getNextLottoRound_1.getNextLottoRound; } });
|
|
27
|
+
var getCheckWinningLink_1 = require("./utils/getCheckWinningLink");
|
|
28
|
+
Object.defineProperty(exports, "getCheckWinningLink", { enumerable: true, get: function () { return getCheckWinningLink_1.getCheckWinningLink; } });
|
|
29
|
+
var checkWinning_1 = require("./utils/checkWinning");
|
|
30
|
+
Object.defineProperty(exports, "checkWinning", { enumerable: true, get: function () { return checkWinning_1.checkWinning; } });
|
package/lib/cjs/lottoService.js
CHANGED
|
@@ -21,12 +21,13 @@ const deferred_1 = require("./utils/deferred");
|
|
|
21
21
|
const constants_1 = require("./constants");
|
|
22
22
|
const lazyRun_1 = require("./utils/lazyRun");
|
|
23
23
|
const logger_1 = __importDefault(require("./logger"));
|
|
24
|
-
const
|
|
24
|
+
const getLastLottoRound_1 = require("./utils/getLastLottoRound");
|
|
25
25
|
const validateLottoNumber_1 = require("./utils/validateLottoNumber");
|
|
26
26
|
const getWinningNumbers_1 = require("./apis/dhlottery/getWinningNumbers");
|
|
27
27
|
const checkWinning_1 = require("./utils/checkWinning");
|
|
28
28
|
const validatePurchaseAvailability_1 = require("./utils/validatePurchaseAvailability");
|
|
29
29
|
const getCheckWinningLink_1 = require("./utils/getCheckWinningLink");
|
|
30
|
+
const getNextLottoRound_1 = require("./utils/getNextLottoRound");
|
|
30
31
|
class LottoService {
|
|
31
32
|
constructor(configs) {
|
|
32
33
|
var _a;
|
|
@@ -106,6 +107,11 @@ class LottoService {
|
|
|
106
107
|
this.logger.debug('[purchase]', 'move to lotto game page');
|
|
107
108
|
const page = yield this.browserController.focus(0);
|
|
108
109
|
yield page.goto(urls_1.URLS.LOTTO_645);
|
|
110
|
+
// remove alert in linux env (비정상적인 방법으로 접속)
|
|
111
|
+
if (process.env.CI) {
|
|
112
|
+
this.logger.debug('[purchase]', 'click environment alert confirm');
|
|
113
|
+
yield page.click(selectors_1.SELECTORS.ENVIRONMENT_ALERT_CONFIRM);
|
|
114
|
+
}
|
|
109
115
|
// click auto button
|
|
110
116
|
this.logger.debug('[purchase]', 'click purchase type button -> auto');
|
|
111
117
|
yield page.click(selectors_1.SELECTORS.PURCHASE_TYPE_RANDOM_BTN);
|
|
@@ -126,15 +132,15 @@ class LottoService {
|
|
|
126
132
|
return elems.map(it => Array.from(it.children).map(child => Number(child.innerHTML)));
|
|
127
133
|
});
|
|
128
134
|
});
|
|
129
|
-
this.check = (numbers, round = (0,
|
|
130
|
-
(0, validateLottoNumber_1.validateLottoNumber)(
|
|
135
|
+
this.check = (numbers, round = (0, getLastLottoRound_1.getLastLottoRound)()) => __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
numbers.forEach(number => (0, validateLottoNumber_1.validateLottoNumber)(number));
|
|
131
137
|
this.logger.debug('[check]', 'getWinningNumbers');
|
|
132
138
|
const winningNumbers = yield (0, getWinningNumbers_1.getWinningNumbers)(round);
|
|
133
|
-
return (0, checkWinning_1.checkWinning)(
|
|
139
|
+
return numbers.map(game => (0, checkWinning_1.checkWinning)(game, winningNumbers));
|
|
134
140
|
});
|
|
135
|
-
this.getCheckWinningLink = (round,
|
|
141
|
+
this.getCheckWinningLink = (numbers, round = (0, getNextLottoRound_1.getNextLottoRound)()) => {
|
|
136
142
|
this.logger.debug('[getCheckWinningLink]', 'getCheckWinningLink');
|
|
137
|
-
return (0, getCheckWinningLink_1.getCheckWinningLink)(
|
|
143
|
+
return (0, getCheckWinningLink_1.getCheckWinningLink)(numbers, round);
|
|
138
144
|
};
|
|
139
145
|
this.logger = new logger_1.default(configs === null || configs === void 0 ? void 0 : configs.logLevel, '[LottoService]');
|
|
140
146
|
this.browserController = (0, factory_1.createBrowserController)((_a = configs === null || configs === void 0 ? void 0 : configs.controller) !== null && _a !== void 0 ? _a : 'playwright', Object.assign({ defaultViewport: { width: 1080, height: 1024 } }, configs), this.logger);
|
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.checkWinning = void 0;
|
|
13
|
-
const checkWinning = (myNumber, winningNumbers) =>
|
|
4
|
+
const checkWinning = (myNumber, winningNumbers) => {
|
|
14
5
|
const mainWinningNumbers = winningNumbers.slice(0, 6);
|
|
15
6
|
const bonusNumber = winningNumbers.at(-1);
|
|
16
7
|
const matchedNumbers = myNumber.filter(n => mainWinningNumbers.includes(n));
|
|
@@ -33,5 +24,5 @@ const checkWinning = (myNumber, winningNumbers) => __awaiter(void 0, void 0, voi
|
|
|
33
24
|
rank = 5;
|
|
34
25
|
}
|
|
35
26
|
return { rank, matchedNumbers };
|
|
36
|
-
}
|
|
27
|
+
};
|
|
37
28
|
exports.checkWinning = checkWinning;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getCheckWinningLink = void 0;
|
|
4
4
|
const urls_1 = require("../constants/urls");
|
|
5
|
-
function getCheckWinningLink(
|
|
5
|
+
function getCheckWinningLink(numbers, round) {
|
|
6
6
|
const nums = numbers.map(it => 'q' + it.map(n => String(n).padStart(2, '0')).join('')).join('');
|
|
7
7
|
return `${urls_1.URLS.CHECK_WINNING}?method=winQr&v=${round}${nums}`;
|
|
8
8
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getLastLottoRound = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
|
-
const
|
|
5
|
+
const getLastLottoRound = () => {
|
|
6
6
|
const standardDate = new Date(constants_1.CONST.THOUSAND_ROUND_DATE);
|
|
7
7
|
const now = new Date(global.Date.now());
|
|
8
8
|
const ADDITIONAL_ROUND = Math.floor((now.getTime() - standardDate.getTime()) / constants_1.CONST.WEEK_TO_MILLISECOND);
|
|
9
9
|
return 1000 + ADDITIONAL_ROUND;
|
|
10
10
|
};
|
|
11
|
-
exports.
|
|
11
|
+
exports.getLastLottoRound = getLastLottoRound;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getNextLottoRound = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const getNextLottoRound = () => {
|
|
6
|
+
const standardDate = new Date(constants_1.CONST.THOUSAND_ROUND_DATE);
|
|
7
|
+
const now = new Date(global.Date.now());
|
|
8
|
+
const ADDITIONAL_ROUND = Math.floor((now.getTime() - standardDate.getTime()) / constants_1.CONST.WEEK_TO_MILLISECOND) + 1;
|
|
9
|
+
return 1000 + ADDITIONAL_ROUND;
|
|
10
|
+
};
|
|
11
|
+
exports.getNextLottoRound = getNextLottoRound;
|
|
@@ -2,6 +2,7 @@ export const SELECTORS = {
|
|
|
2
2
|
ID_INPUT: '#userId',
|
|
3
3
|
PWD_INPUT: '#article > div:nth-child(2) > div > form > div > div.inner > fieldset > div.form > input[type=password]:nth-child(2)',
|
|
4
4
|
LOGIN_BUTTON: '#article > div:nth-child(2) > div > form > div > div.inner > fieldset > div.form > a',
|
|
5
|
+
ENVIRONMENT_ALERT_CONFIRM: 'input[value="확인"][onclick="javascript:closepopupLayerAlert();"]',
|
|
5
6
|
PURCHASE_TYPE_RANDOM_BTN: 'a[href="#divWay2Buy1"]#num2',
|
|
6
7
|
PURCHASE_AMOUNT_SELECT: 'select#amoundApply',
|
|
7
8
|
PURCHASE_AMOUNT_CONFIRM_BTN: 'input[value="확인"]#btnSelectNum',
|
|
@@ -14,10 +14,10 @@ import { CONST } from '../../constants';
|
|
|
14
14
|
import {} from '../../logger';
|
|
15
15
|
export class PlaywrightController {
|
|
16
16
|
constructor(configs, logger) {
|
|
17
|
-
this.
|
|
17
|
+
this.getBrowserContext = () => __awaiter(this, void 0, void 0, function* () {
|
|
18
18
|
const p = deferred();
|
|
19
|
-
if (this.
|
|
20
|
-
p.resolve(this.
|
|
19
|
+
if (this.context) {
|
|
20
|
+
p.resolve(this.context);
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
23
|
let retry = 0;
|
|
@@ -26,9 +26,9 @@ export class PlaywrightController {
|
|
|
26
26
|
clearInterval(interval);
|
|
27
27
|
p.reject(new Error('Browser is not initialized'));
|
|
28
28
|
}
|
|
29
|
-
else if (this.
|
|
29
|
+
else if (this.context) {
|
|
30
30
|
clearInterval(interval);
|
|
31
|
-
p.resolve(this.
|
|
31
|
+
p.resolve(this.context);
|
|
32
32
|
}
|
|
33
33
|
retry++;
|
|
34
34
|
}, CONST.BROWSER_INIT_RETRY_TIMEOUT);
|
|
@@ -36,7 +36,7 @@ export class PlaywrightController {
|
|
|
36
36
|
return p.promise;
|
|
37
37
|
});
|
|
38
38
|
this.focus = (pageIndex = -1) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
const browser = yield this.
|
|
39
|
+
const browser = yield this.getBrowserContext();
|
|
40
40
|
const pages = browser.pages();
|
|
41
41
|
if (pages.length === 0) {
|
|
42
42
|
const page = yield browser.newPage();
|
|
@@ -51,11 +51,12 @@ export class PlaywrightController {
|
|
|
51
51
|
}
|
|
52
52
|
});
|
|
53
53
|
this.close = () => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
const
|
|
55
|
-
|
|
54
|
+
const context = yield this.getBrowserContext();
|
|
55
|
+
yield context.close();
|
|
56
|
+
return this.browser.close();
|
|
56
57
|
});
|
|
57
58
|
this.cleanPages = (remainingPageIndex) => __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
const browser = yield this.
|
|
59
|
+
const browser = yield this.getBrowserContext();
|
|
59
60
|
const pages = browser.pages();
|
|
60
61
|
const promises = pages.map((page, index) => __awaiter(this, void 0, void 0, function* () {
|
|
61
62
|
if (!remainingPageIndex.includes(index)) {
|
|
@@ -66,6 +67,9 @@ export class PlaywrightController {
|
|
|
66
67
|
});
|
|
67
68
|
this.configs = configs;
|
|
68
69
|
this.logger = logger;
|
|
69
|
-
chromium.launch(this.configs).then((browser) => __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
chromium.launch(this.configs).then((browser) => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
this.browser = browser;
|
|
72
|
+
this.context = yield browser.newContext();
|
|
73
|
+
}));
|
|
70
74
|
}
|
|
71
75
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { LottoService } from './lottoService';
|
|
2
|
-
export { getCurrentLottoRound } from './utils/getCurrentLottoRound';
|
|
3
2
|
export { LogLevel } from './logger';
|
|
4
3
|
export * from './types';
|
|
4
|
+
export { getLastLottoRound } from './utils/getLastLottoRound';
|
|
5
|
+
export { getNextLottoRound } from './utils/getNextLottoRound';
|
|
6
|
+
export { getCheckWinningLink } from './utils/getCheckWinningLink';
|
|
7
|
+
export { checkWinning } from './utils/checkWinning';
|
package/lib/esm/lottoService.js
CHANGED
|
@@ -15,12 +15,13 @@ import { deferred } from './utils/deferred';
|
|
|
15
15
|
import { CONST } from './constants';
|
|
16
16
|
import { lazyRun } from './utils/lazyRun';
|
|
17
17
|
import Logger, {} from './logger';
|
|
18
|
-
import {
|
|
18
|
+
import { getLastLottoRound } from './utils/getLastLottoRound';
|
|
19
19
|
import { validateLottoNumber } from './utils/validateLottoNumber';
|
|
20
20
|
import { getWinningNumbers } from './apis/dhlottery/getWinningNumbers';
|
|
21
21
|
import { checkWinning } from './utils/checkWinning';
|
|
22
22
|
import { validatePurchaseAvailability } from './utils/validatePurchaseAvailability';
|
|
23
23
|
import { getCheckWinningLink } from './utils/getCheckWinningLink';
|
|
24
|
+
import { getNextLottoRound } from './utils/getNextLottoRound';
|
|
24
25
|
export class LottoService {
|
|
25
26
|
constructor(configs) {
|
|
26
27
|
var _a;
|
|
@@ -100,6 +101,11 @@ export class LottoService {
|
|
|
100
101
|
this.logger.debug('[purchase]', 'move to lotto game page');
|
|
101
102
|
const page = yield this.browserController.focus(0);
|
|
102
103
|
yield page.goto(URLS.LOTTO_645);
|
|
104
|
+
// remove alert in linux env (비정상적인 방법으로 접속)
|
|
105
|
+
if (process.env.CI) {
|
|
106
|
+
this.logger.debug('[purchase]', 'click environment alert confirm');
|
|
107
|
+
yield page.click(SELECTORS.ENVIRONMENT_ALERT_CONFIRM);
|
|
108
|
+
}
|
|
103
109
|
// click auto button
|
|
104
110
|
this.logger.debug('[purchase]', 'click purchase type button -> auto');
|
|
105
111
|
yield page.click(SELECTORS.PURCHASE_TYPE_RANDOM_BTN);
|
|
@@ -120,15 +126,15 @@ export class LottoService {
|
|
|
120
126
|
return elems.map(it => Array.from(it.children).map(child => Number(child.innerHTML)));
|
|
121
127
|
});
|
|
122
128
|
});
|
|
123
|
-
this.check = (numbers, round =
|
|
124
|
-
validateLottoNumber(
|
|
129
|
+
this.check = (numbers, round = getLastLottoRound()) => __awaiter(this, void 0, void 0, function* () {
|
|
130
|
+
numbers.forEach(number => validateLottoNumber(number));
|
|
125
131
|
this.logger.debug('[check]', 'getWinningNumbers');
|
|
126
132
|
const winningNumbers = yield getWinningNumbers(round);
|
|
127
|
-
return checkWinning(
|
|
133
|
+
return numbers.map(game => checkWinning(game, winningNumbers));
|
|
128
134
|
});
|
|
129
|
-
this.getCheckWinningLink = (
|
|
135
|
+
this.getCheckWinningLink = (numbers, round = getNextLottoRound()) => {
|
|
130
136
|
this.logger.debug('[getCheckWinningLink]', 'getCheckWinningLink');
|
|
131
|
-
return getCheckWinningLink(
|
|
137
|
+
return getCheckWinningLink(numbers, round);
|
|
132
138
|
};
|
|
133
139
|
this.logger = new Logger(configs === null || configs === void 0 ? void 0 : configs.logLevel, '[LottoService]');
|
|
134
140
|
this.browserController = createBrowserController((_a = configs === null || configs === void 0 ? void 0 : configs.controller) !== null && _a !== void 0 ? _a : 'playwright', Object.assign({ defaultViewport: { width: 1080, height: 1024 } }, configs), this.logger);
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
export const checkWinning = (myNumber, winningNumbers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1
|
+
export const checkWinning = (myNumber, winningNumbers) => {
|
|
11
2
|
const mainWinningNumbers = winningNumbers.slice(0, 6);
|
|
12
3
|
const bonusNumber = winningNumbers.at(-1);
|
|
13
4
|
const matchedNumbers = myNumber.filter(n => mainWinningNumbers.includes(n));
|
|
@@ -30,4 +21,4 @@ export const checkWinning = (myNumber, winningNumbers) => __awaiter(void 0, void
|
|
|
30
21
|
rank = 5;
|
|
31
22
|
}
|
|
32
23
|
return { rank, matchedNumbers };
|
|
33
|
-
}
|
|
24
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { URLS } from '../constants/urls';
|
|
2
|
-
export function getCheckWinningLink(
|
|
2
|
+
export function getCheckWinningLink(numbers, round) {
|
|
3
3
|
const nums = numbers.map(it => 'q' + it.map(n => String(n).padStart(2, '0')).join('')).join('');
|
|
4
4
|
return `${URLS.CHECK_WINNING}?method=winQr&v=${round}${nums}`;
|
|
5
5
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CONST } from '../constants';
|
|
2
|
-
export const
|
|
2
|
+
export const getLastLottoRound = () => {
|
|
3
3
|
const standardDate = new Date(CONST.THOUSAND_ROUND_DATE);
|
|
4
4
|
const now = new Date(global.Date.now());
|
|
5
5
|
const ADDITIONAL_ROUND = Math.floor((now.getTime() - standardDate.getTime()) / CONST.WEEK_TO_MILLISECOND);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { CONST } from '../constants';
|
|
2
|
+
export const getNextLottoRound = () => {
|
|
3
|
+
const standardDate = new Date(CONST.THOUSAND_ROUND_DATE);
|
|
4
|
+
const now = new Date(global.Date.now());
|
|
5
|
+
const ADDITIONAL_ROUND = Math.floor((now.getTime() - standardDate.getTime()) / CONST.WEEK_TO_MILLISECOND) + 1;
|
|
6
|
+
return 1000 + ADDITIONAL_ROUND;
|
|
7
|
+
};
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { BrowserConfigs, BrowserControllerInterface } from '../../types';
|
|
2
|
-
import type { BrowserContext } from 'playwright';
|
|
2
|
+
import type { Browser, BrowserContext } from 'playwright';
|
|
3
3
|
import { PlaywrightPage } from './playwright.page';
|
|
4
4
|
import { type LoggerInterface } from '../../logger';
|
|
5
5
|
export declare class PlaywrightController implements BrowserControllerInterface {
|
|
6
6
|
configs: BrowserConfigs;
|
|
7
7
|
logger: LoggerInterface;
|
|
8
|
-
browser:
|
|
8
|
+
browser: Browser;
|
|
9
|
+
context: BrowserContext;
|
|
9
10
|
constructor(configs: BrowserConfigs, logger: LoggerInterface);
|
|
10
|
-
private
|
|
11
|
+
private getBrowserContext;
|
|
11
12
|
focus: (pageIndex?: number) => Promise<PlaywrightPage>;
|
|
12
13
|
close: () => Promise<void>;
|
|
13
14
|
cleanPages: (remainingPageIndex: number[]) => Promise<void>;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { LottoService } from './lottoService';
|
|
2
|
-
export { getCurrentLottoRound } from './utils/getCurrentLottoRound';
|
|
3
2
|
export { LogLevel } from './logger';
|
|
4
3
|
export * from './types';
|
|
4
|
+
export { getLastLottoRound } from './utils/getLastLottoRound';
|
|
5
|
+
export { getNextLottoRound } from './utils/getNextLottoRound';
|
|
6
|
+
export { getCheckWinningLink } from './utils/getCheckWinningLink';
|
|
7
|
+
export { checkWinning } from './utils/checkWinning';
|
|
@@ -11,9 +11,9 @@ export declare class LottoService implements LottoServiceInterface {
|
|
|
11
11
|
signInWithCookie: (cookies: string) => Promise<string>;
|
|
12
12
|
signIn: (id: string, password: string) => Promise<string>;
|
|
13
13
|
purchase: (amount?: number) => Promise<number[][]>;
|
|
14
|
-
check: (numbers: number[], round?: number) => Promise<{
|
|
14
|
+
check: (numbers: number[][], round?: number) => Promise<{
|
|
15
15
|
rank: number;
|
|
16
16
|
matchedNumbers: number[];
|
|
17
|
-
}>;
|
|
18
|
-
getCheckWinningLink: (
|
|
17
|
+
}[]>;
|
|
18
|
+
getCheckWinningLink: (numbers: number[][], round?: number) => string;
|
|
19
19
|
}
|
|
@@ -3,12 +3,12 @@ export interface LottoServiceInterface {
|
|
|
3
3
|
destroy(): Promise<void>;
|
|
4
4
|
signIn(id: string, password: string): Promise<string>;
|
|
5
5
|
signInWithCookie(cookie: string): Promise<string>;
|
|
6
|
-
check(numbers: number[], volume?: number): Promise<{
|
|
6
|
+
check(numbers: number[][], volume?: number): Promise<{
|
|
7
7
|
rank: number;
|
|
8
8
|
matchedNumbers: number[];
|
|
9
|
-
}>;
|
|
9
|
+
}[]>;
|
|
10
10
|
purchase(amount: number): Promise<number[][]>;
|
|
11
|
-
getCheckWinningLink(
|
|
11
|
+
getCheckWinningLink(numbers: number[][], round: number): string;
|
|
12
12
|
}
|
|
13
13
|
export interface BrowserConfigs {
|
|
14
14
|
controller?: 'puppeteer' | 'playwright';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function getCheckWinningLink(
|
|
1
|
+
export declare function getCheckWinningLink(numbers: number[][], round: number): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getLastLottoRound: () => number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getNextLottoRound: () => number;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const getCurrentLottoRound: () => number;
|