-
Notifications
You must be signed in to change notification settings - Fork 42
/
lotto645.py
225 lines (179 loc) · 6.96 KB
/
lotto645.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import json
import datetime
import requests
from enum import Enum
from bs4 import BeautifulSoup as BS
from datetime import timedelta
import auth
class Lotto645Mode(Enum):
AUTO = 1
MANUAL = 2
BUY = 10
CHECK = 20
class Lotto645:
_REQ_HEADERS = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
"sec-ch-ua-mobile": "?0",
"Upgrade-Insecure-Requests": "1",
"Origin": "https://ol.dhlottery.co.kr",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Referer": "https://ol.dhlottery.co.kr/olotto/game/game645.do",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Accept-Language": "ko,en-US;q=0.9,en;q=0.8,ko-KR;q=0.7",
}
def buy_lotto645(
self,
auth_ctrl: auth.AuthController,
cnt: int,
mode: Lotto645Mode
) -> dict:
assert type(auth_ctrl) == auth.AuthController
assert type(cnt) == int and 1 <= cnt <= 5
assert type(mode) == Lotto645Mode
headers = self._generate_req_headers(auth_ctrl)
requirements = self._getRequirements(headers)
data = (
self._generate_body_for_auto_mode(cnt, requirements)
if mode == Lotto645Mode.AUTO
else self._generate_body_for_manual(cnt)
)
body = self._try_buying(headers, data)
self._show_result(body)
return body
def _generate_req_headers(self, auth_ctrl: auth.AuthController) -> dict:
assert type(auth_ctrl) == auth.AuthController
return auth_ctrl.add_auth_cred_to_headers(self._REQ_HEADERS)
def _generate_body_for_auto_mode(self, cnt: int, requirements: list) -> dict:
assert type(cnt) == int and 1 <= cnt <= 5
SLOTS = [
"A",
"B",
"C",
"D",
"E",
]
return {
"round": self._get_round(),
"direct": requirements[0], # TODO: test if this can be comment
"nBuyAmount": str(1000 * cnt),
"param": json.dumps(
[
{"genType": "0", "arrGameChoiceNum": None, "alpabet": slot}
for slot in SLOTS[:cnt]
]
),
'ROUND_DRAW_DATE' : requirements[1],
'WAMT_PAY_TLMT_END_DT' : requirements[2],
"gameCnt": cnt
}
def _generate_body_for_manual(self, cnt: int) -> dict:
assert type(cnt) == int and 1 <= cnt <= 5
raise NotImplementedError()
def _getRequirements(self, headers: dict) -> list:
org_headers = headers
headers["Referer"] ="https://ol.dhlottery.co.kr/olotto/game/game645.do"
headers["Content-Type"] = "application/json; charset=UTF-8"
headers["X-Requested-With"] ="XMLHttpRequest"
#no param needed at now
res = requests.post(
url="https://ol.dhlottery.co.kr/olotto/game/egovUserReadySocket.json",
headers=headers
)
direct = json.loads(res.text)["ready_ip"]
res = requests.post(
url="https://ol.dhlottery.co.kr/olotto/game/game645.do",
headers=org_headers
)
html = res.text
soup = BS(
html, "html5lib"
)
draw_date = soup.find("input", id="ROUND_DRAW_DATE").get('value')
tlmt_date = soup.find("input", id="WAMT_PAY_TLMT_END_DT").get('value')
return [direct, draw_date, tlmt_date]
def _get_round(self) -> str:
res = requests.get("https://www.dhlottery.co.kr/common.do?method=main")
html = res.text
soup = BS(
html, "html5lib"
) # 'html5lib' : in case that the html don't have clean tag pairs
last_drawn_round = int(soup.find("strong", id="lottoDrwNo").text)
return str(last_drawn_round + 1)
def get_balance(self, auth_ctrl: auth.AuthController) -> str:
headers = self._generate_req_headers(auth_ctrl)
res = requests.post(
url="https://dhlottery.co.kr/userSsl.do?method=myPage",
headers=headers
)
html = res.text
soup = BS(
html, "html5lib"
)
balance = soup.find("p", class_="total_new").find('strong').text
return balance
def _try_buying(self, headers: dict, data: dict) -> dict:
assert type(headers) == dict
assert type(data) == dict
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
res = requests.post(
"https://ol.dhlottery.co.kr/olotto/game/execBuy.do",
headers=headers,
data=data,
)
res.encoding = "utf-8"
return json.loads(res.text)
def check_winning(self, auth_ctrl: auth.AuthController) -> dict:
assert type(auth_ctrl) == auth.AuthController
headers = self._generate_req_headers(auth_ctrl)
parameters = self._make_search_date()
data = {
"nowPage": 1,
"searchStartDate": parameters["searchStartDate"],
"searchEndDate": parameters["searchEndDate"],
"winGrade": 1,
"lottoId": "LO40",
"sortOrder": "DESC"
}
res = requests.post(
"https://dhlottery.co.kr/myPage.do?method=lottoBuyList",
headers=headers,
data=data
)
html = res.text
soup = BS(html, "html5lib")
winnings = soup.find("table", class_="tbl_data tbl_data_col").find_all("tbody")[0].find_all("td")
result_data = {
"data": "no winning data"
}
if len(winnings) == 1:
return result_data
result_data = {
"round": winnings[2].text.strip(),
"money": winnings[6].text.strip(),
"purchased_date": winnings[0].text.strip(),
"winning_date": winnings[7].text.strip()
}
return result_data
def _make_search_date(self) -> dict:
today = datetime.datetime.today()
today_str = today.strftime("%Y%m%d")
weekago = today - timedelta(days=7)
weekago_str = weekago.strftime("%Y%m%d")
return {
"searchStartDate": weekago_str,
"searchEndDate": today_str
}
def _show_result(self, body: dict) -> None:
assert type(body) == dict
if body.get("loginYn") != "Y":
return
result = body.get("result", {})
if result.get("resultMsg", "FAILURE").upper() != "SUCCESS":
return