115 lines
4.7 KiB
Python
115 lines
4.7 KiB
Python
# 웹 호출 라이브러리를 호출합니다.
|
|
import os
|
|
import pandas as pd
|
|
import time
|
|
from datetime import datetime, timedelta
|
|
import requests
|
|
# JSON 포맷을 다루기 위한 라이브러리를 호출합니다.
|
|
import json
|
|
from TelegramBot import TelegramBot
|
|
|
|
# 로또 데이터를 수집하기 위한 파이썬 클래스를 선언합니다.
|
|
class DataCrawler:
|
|
|
|
bot = None
|
|
|
|
# 클래스 생성자로 수집할 회차를 입력받습니다.
|
|
def __init__(self):
|
|
self.bot = TelegramBot()
|
|
|
|
return
|
|
|
|
# 로또 당첨 데이터를 수집해서 파일로 저장합니다.
|
|
# lottoHistoryFile: 로또 당첨 데이터를 저장할 파일
|
|
def craw(self, lottoHistoryFile, drwNo=None):
|
|
|
|
if drwNo != None:
|
|
# 로또 데이터를 저장할 파일을 선언합니다.
|
|
jsonFp = open(lottoHistoryFile + ".json", 'a', encoding="utf-8")
|
|
textFp = open(lottoHistoryFile + ".txt", 'a', encoding="utf-8")
|
|
|
|
url = 'https://dhlottery.co.kr/common.do?method=getLottoNumber&drwNo=' + str(drwNo)
|
|
# URL을 호출합니다.
|
|
res = requests.post(url)
|
|
# 호출한 결과에 대해서 Json 포맷을 가져옵니다.
|
|
result = res.json()
|
|
|
|
# 가져온 Json 포맷을 파일로 저장합니다.
|
|
jsonFp.write(json.dumps(result, ensure_ascii=False) + "\n")
|
|
|
|
textFp.write("%d,%d,%d,%d,%d,%d,%d,%d\n" % (drwNo, result['drwtNo1'], result['drwtNo2'], result['drwtNo3'], result['drwtNo4'], result['drwtNo5'], result['drwtNo6'], result['bnusNo']))
|
|
else:
|
|
# 로또 데이터를 저장할 파일을 선언합니다.
|
|
jsonFp = open(lottoHistoryFile + ".json", 'w', encoding="utf-8")
|
|
textFp = open(lottoHistoryFile + ".txt", 'w', encoding="utf-8")
|
|
|
|
# 1회차부터 지정된 회차까지 로또 당첨 번호를 수집합니다.
|
|
idx = 1
|
|
while True:
|
|
# 1회차부터 지정된 회차까지의 URL을 생성합니다.
|
|
url = 'https://dhlottery.co.kr/common.do?method=getLottoNumber&drwNo=' + str(idx)
|
|
# URL을 호출합니다.
|
|
res = requests.post(url)
|
|
# 호출한 결과에 대해서 Json 포맷을 가져옵니다.
|
|
result = res.json()
|
|
if result['returnValue'] != 'success':
|
|
break
|
|
# 가져온 Json 포맷을 파일로 저장합니다.
|
|
jsonFp.write(json.dumps(result, ensure_ascii=False) + "\n")
|
|
textFp.write("%d,%d,%d,%d,%d,%d,%d,%d\n" % (idx, result['drwtNo1'], result['drwtNo2'], result['drwtNo3'], result['drwtNo4'], result['drwtNo5'], result['drwtNo6'], result['bnusNo']))
|
|
idx += 1
|
|
time.sleep(0.5)
|
|
# 저장한 파일을 종료합니다.
|
|
jsonFp.close()
|
|
textFp.close()
|
|
return
|
|
|
|
def excute(self, resource_path):
|
|
"""
|
|
# 가져올 로또 회차를 지정합니다.
|
|
sDrwNo = 915
|
|
eDrwNo = 947
|
|
for i in range (sDrwNo, eDrwNo+1):
|
|
# 로또 데이터를 수집하기 위한 함수를 호출합니다.
|
|
dataCrawler.crawl(lottoHistoryFile, i)
|
|
"""
|
|
|
|
lottoHistoryFile = os.path.join(resource_path, 'lotto_history')
|
|
|
|
today = datetime.today()
|
|
if today.weekday() == 5:
|
|
if today.hour > 20:
|
|
this_weekend = today + timedelta(days=(12 - today.weekday()))
|
|
else:
|
|
this_weekend = today + timedelta(days=(5 - today.weekday()))
|
|
elif today.weekday() == 6:
|
|
this_weekend = today + timedelta(days=(12 - today.weekday()))
|
|
else:
|
|
this_weekend = today + timedelta(days=(5 - today.weekday()))
|
|
|
|
last_weekend = (this_weekend - timedelta(days=7)).strftime('%Y-%m-%d')
|
|
|
|
lottoHistoryFileName = lottoHistoryFile + '.json'
|
|
with open(lottoHistoryFileName, "r", encoding='utf-8') as f:
|
|
for line in f:
|
|
if line != '\n':
|
|
last_json = json.loads(line)
|
|
|
|
if 'returnValue' not in last_json or last_json['returnValue'] == 'fail':
|
|
return False
|
|
|
|
if last_json['drwNoDate'] == last_weekend:
|
|
self.bot.sendMsg("[Lottery Crawler] {} already existed..".format(last_weekend))
|
|
else:
|
|
self.craw(lottoHistoryFile, drwNo=last_json['drwNo'] + 1)
|
|
self.bot.sendMsg("[Lottery Crawler] {} ({}) crawled..".format(last_weekend, last_json['drwNo'] + 1))
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
PROJECT_HOME = '.'
|
|
resource_path = os.path.join(PROJECT_HOME, 'resources')
|
|
|
|
# 로또 데이터를 수집하기 위한 파이썬 클래스를 지정합니다.
|
|
dataCrawler = DataCrawler()
|
|
dataCrawler.excute(resource_path) |