-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlotto_data.py
343 lines (317 loc) · 12.8 KB
/
lotto_data.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
import sqlite3
import os.path
import argparse
from bs4 import BeautifulSoup
from collections import defaultdict
from collections import Counter
class HandelColors:
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
class HandelSQLite:
"""
DataBase 관련 Handling Class
"""
def __init__(self, db_path):
self.conn = sqlite3.connect(db_path)
def __del__(self):
self.conn.close()
def check_latest_round(self, input_round):
try:
cur = self.conn.cursor()
cur.execute('SELECT round FROM tb_lotto_list ORDER BY round DESC')
select_round = cur.fetchone()
if int(input_round) == int(select_round[0]):
return False
return True
except Exception as err:
print(err)
return False
def latest_round(self):
try:
cur = self.conn.cursor()
cur.execute('SELECT round FROM tb_lotto_list ORDER BY round DESC')
ret = cur.fetchone()[0]
return int(ret)
except Exception as err:
print(err)
return 0
def insert_data(self, input_data):
try:
cur = self.conn.cursor()
query = """
INSERT INTO
tb_lotto_list
VALUES (
{round}, '{date}', {drw_1st}, {drw_2nd}, {drw_3rd}, {drw_4th}, {drw_5th}, {drw_6th}, {bonus}
)
""".format(
round=input_data.get('round'), date=input_data.get('round_date'), drw_1st=input_data.get('drw_1st'),
drw_2nd=input_data.get('drw_2nd'), drw_3rd=input_data.get('drw_3rd'), drw_4th=input_data.get('drw_4th'),
drw_5th=input_data.get('drw_5th'), drw_6th=input_data.get('drw_6th'), bonus=input_data.get('drw_bnus')
)
cur.execute(query)
self.conn.commit()
return True
except Exception as err:
print(err)
return False
def select_all(self):
try:
cur = self.conn.cursor()
query = """
SELECT
GROUP_CONCAT(data.num) AS num
FROM (
SELECT
("1st"||","||"2nd"||","||"3rd"||","||"4th"||","||"5th"||","||"6th"||","||"bonus") AS num
FROM
tb_lotto_list
) AS data
"""
cur.execute(query)
return cur.fetchone()
except Exception as err:
print(err)
return ""
def select_all_except_bonus(self):
try:
cur = self.conn.cursor()
query = """
SELECT
GROUP_CONCAT(data.num) AS num
FROM (
SELECT
("1st"||","||"2nd"||","||"3rd"||","||"4th"||","||"5th"||","||"6th") AS num
FROM
tb_lotto_list
) AS data
"""
cur.execute(query)
return cur.fetchone()
except Exception as err:
print(err)
return ""
def select_custom(self, sround, eround):
try:
cur = self.conn.cursor()
query = """
SELECT
GROUP_CONCAT(num) AS num
FROM (
SELECT
("1st"||","||"2nd"||","||"3rd"||","||"4th"||","||"5th"||","||"6th"||","||"bonus") AS num
FROM
tb_lotto_list
WHERE
round BETWEEN {sround} and {eround}
)
""".format(sround=sround, eround=eround)
cur.execute(query)
return cur.fetchone()
except Exception as err:
print(err)
return ""
def select_custom_except_bonus(self, sround, eround):
try:
cur = self.conn.cursor()
query = """
SELECT
GROUP_CONCAT(num) AS num
FROM (
SELECT
("1st"||","||"2nd"||","||"3rd"||","||"4th"||","||"5th"||","||"6th") AS num
FROM
tb_lotto_list
WHERE
round BETWEEN {sround} and {eround}
)
""".format(sround=sround, eround=eround)
cur.execute(query)
return cur.fetchone()
except Exception as err:
print(err)
return ""
class LottoData:
"""
로또 최신 데이터 확인 및 데이터베이스 Insert
"""
def __init__(self, db_path):
self.db_path = db_path
self.site_url = 'https://www.dhlottery.co.kr/common.do?method=main&mainMode=default'
@staticmethod
def __set_round_date(round_date):
try:
rd = round_date.replace('-', '.')
return rd.strip('(').strip(')').strip('추첨')
except Exception as err:
print(err)
return round_date
def __get_lotto_info(self):
"""
동행복권 사이트에서 최신 회차 데이터 가지고 오기
:return: bool, dict
"""
ret_json = defaultdict(int)
try:
info_data = requests.get(self.site_url)
if info_data.status_code != 200:
return False, {}
bs = BeautifulSoup(info_data.text, 'html.parser')
content_data = bs.find('div', {'class': 'content'})
if not content_data:
return False, {}
ret_json = {
"round": int(content_data.find('strong', {'id': 'lottoDrwNo'}).text),
"round_date": self.__set_round_date(content_data.find('span', {'id': 'drwNoDate'}).text),
"drw_1st": int(content_data.find('span', {'id': 'drwtNo1'}).text),
"drw_2nd": int(content_data.find('span', {'id': 'drwtNo2'}).text),
"drw_3rd": int(content_data.find('span', {'id': 'drwtNo3'}).text),
"drw_4th": int(content_data.find('span', {'id': 'drwtNo4'}).text),
"drw_5th": int(content_data.find('span', {'id': 'drwtNo5'}).text),
"drw_6th": int(content_data.find('span', {'id': 'drwtNo6'}).text),
"drw_bnus": int(content_data.find('span', {'id': 'bnusNo'}).text)
}
return True, ret_json
except Exception as err:
print(err)
return False, ret_json
def run(self):
try:
ret, data = self.__get_lotto_info()
if not ret:
print("동행복권 사이트에서 최신 로또 당첨 번호를 가지고 오지 못했습니다.")
sys.exit(0)
latest_round = data.get('round')
if not os.path.exists(self.db_path):
print("로또 데이터베이스 파일이 존재하지 않습니다.")
sys.exit(0)
handler = HandelSQLite(self.db_path)
if not handler.check_latest_round(latest_round):
print("{latest_round}회 로또 당첨 번호 데이터가 이미 존재합니다.".format(latest_round=latest_round))
sys.exit(0)
if not handler.insert_data(data):
print("{latest_round}회 로또 당첨 번호 입력 중 문제가 발생하였습니다.".format(latest_round=latest_round))
sys.exit(0)
print("정상적으로 {latest_round}회 로또 당첨 번호를 데이터베이스에 등록하였습니다.".format(latest_round=latest_round))
except Exception as err:
print(err)
sys.exit(0)
class LottoStatistics:
"""
로또 당첨 번호 통계 확인
"""
def __init__(self, db_path):
self.db_path = db_path
self.handler = HandelSQLite(self.db_path)
def all(self, bonus=True):
try:
last_round = self.handler.latest_round()
if bonus:
data = self.handler.select_all()
comment = "전체 통계"
else:
data = self.handler.select_all_except_bonus()
comment = "보너스볼 제외 통계"
data_list = list(map(int, data[0].split(",")))
counter = Counter(data_list)
print("### 1회 ~ {last_round}회 {comment} ###".format(last_round=last_round, comment=comment))
for i in counter.most_common(45):
print("{num}번 : {cnt}개".format(num=i[0], cnt=i[1]))
except Exception as err:
print(err)
sys.exit(0)
def custom_top(self, count=6, bonus=True):
try:
if not isinstance(count, int):
count = 6
last_round = self.handler.latest_round()
if bonus:
data = self.handler.select_all()
comment = "전체 통계"
else:
data = self.handler.select_all_except_bonus()
comment = "보너스볼 제외 통계"
data_list = list(map(int, data[0].split(",")))
counter = Counter(data_list)
print("### 1회 ~ {last_round}회 Top {count} {comment} ###".format(
last_round=last_round, count=count, comment=comment)
)
for i in counter.most_common(count):
print("{num}번 : {cnt}개".format(num=i[0], cnt=i[1]))
except Exception as err:
print(err)
sys.exit(0)
def custom_round_top(self, sround, eround, count=6, bonus=True):
try:
last_round = self.handler.latest_round()
if not isinstance(sround, int):
sround = 1
if not isinstance(eround, int):
eround = last_round
if not isinstance(count, int):
count = 6
if sround > last_round or eround > last_round:
print("입력하신 {eround}회차는 존재하지 않습니다. \n"
"마지막 회차는 {last_round}회차 입니다. \n"
"다시 입력 부탁 드립니다.".format(eround=eround, last_round=last_round))
sys.exit(0)
if bonus:
data = self.handler.select_custom(sround, eround)
comment = "전체 통계"
else:
data = self.handler.select_custom_except_bonus(sround, eround)
comment = "보너스볼 제외 통계"
data_list = list(map(int, data[0].split(",")))
counter = Counter(data_list)
print("### {sround}회 ~ {eround}회 Top {count} {comment} ###".format(
sround=sround, eround=eround, count=count, comment=comment)
)
for i in counter.most_common(count):
print("{num}번 : {cnt}개".format(num=i[0], cnt=i[1]))
except Exception as err:
print(err)
sys.exit(0)
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='lotto', description='로또 최신 당첨번호 입력 및 통계 Tool')
try:
parser.add_argument('-d', '--db', dest='db_path', type=str, default='./lotto_data.db',
help='로또 당첨번호 DataBase Path')
parser.add_argument('-i', '--insert', action='store_true', help='최신 로또 당첨번호 DataBase Insert')
parser.add_argument('-s', '--stat', action='store_true', help='로또 당첨번호 통계')
parser.add_argument('-t', '--top', type=int, nargs='?', const=6, help='로또 당첨번호 전체 top 통계')
parser.add_argument('-c', '--custom', type=int, nargs='?', const=6, help='로또 당첨번호 Custom top 통계')
parser.add_argument('-sr', '--sround', type=int, nargs='?', const=1, help='로또 당첨번호 top 통계')
parser.add_argument('-er', '--eround', type=int, nargs='?', const=1007, help='로또 당첨번호 top 통계')
parser.add_argument('-exb', '--ex_bonus', dest="ex_bonus", action='store_false', help='보너스볼 제외')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.1')
args = parser.parse_args()
db_path = args.db_path
if args.insert:
lotto_data = LottoData(db_path)
lotto_data.run()
sys.exit(0)
LottoStatistics = LottoStatistics(db_path)
if args.stat:
LottoStatistics.all(args.ex_bonus)
sys.exit(0)
if args.top:
LottoStatistics.custom_top(args.top, args.ex_bonus)
sys.exit(0)
if args.custom and args.sround and args.eround:
LottoStatistics.custom_round_top(args.sround, args.eround, args.custom, args.ex_bonus)
sys.exit(0)
except Exception as err:
print(err)
sys.exit(0)