update: refresh lotto history and fix crawler API fallback
1230~1240회 당첨 데이터를 반영하고, DataCrawler에 lt645 내부 API fallback을 추가했으며 final_practice 포트폴리오 선정과 gitignore 산출물 규칙을 정리했다. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
233
DataCrawler.py
233
DataCrawler.py
@@ -22,10 +22,15 @@ except ModuleNotFoundError:
|
||||
def sendMsg(self, msg):
|
||||
pass
|
||||
|
||||
_LOTTO_URLS = (
|
||||
_LOTTO_LEGACY_URLS = (
|
||||
"https://www.dhlottery.co.kr/common.do?method=getLottoNumber&drwNo={}",
|
||||
"https://dhlottery.co.kr/common.do?method=getLottoNumber&drwNo={}",
|
||||
)
|
||||
_LT645_API_URLS = (
|
||||
"https://www.dhlottery.co.kr/lt645/selectPstLt645Info.do",
|
||||
"https://dhlottery.co.kr/lt645/selectPstLt645Info.do",
|
||||
)
|
||||
_LOTTO_RESULT_REFERER = "https://www.dhlottery.co.kr/result.do?method=byWin"
|
||||
_REQUEST_TIMEOUT = float(os.environ.get("LOTTO_REQUEST_TIMEOUT", "12"))
|
||||
_FETCH_RETRIES_PER_DRAW = int(os.environ.get("LOTTO_FETCH_RETRIES", "3"))
|
||||
_BACKOFF_BASE_SECONDS = float(os.environ.get("LOTTO_BACKOFF_BASE", "0.7"))
|
||||
@@ -38,7 +43,7 @@ _BROWSER_HEADERS = {
|
||||
),
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"Accept-Language": "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7",
|
||||
"Referer": "https://www.dhlottery.co.kr/gameResult.do?method=byWin",
|
||||
"Referer": _LOTTO_RESULT_REFERER,
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
}
|
||||
|
||||
@@ -62,6 +67,181 @@ class DataCrawler:
|
||||
self._session = requests.Session()
|
||||
self._session.headers.update(_BROWSER_HEADERS)
|
||||
self._last_fetch_error = ""
|
||||
self._session_warmed = False
|
||||
|
||||
def _warm_up_session(self):
|
||||
"""동행복권 결과 페이지를 먼저 방문해 쿠키/세션을 확보합니다."""
|
||||
if self._session_warmed:
|
||||
return
|
||||
warmup_headers = {
|
||||
"User-Agent": _BROWSER_HEADERS["User-Agent"],
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": _BROWSER_HEADERS["Accept-Language"],
|
||||
}
|
||||
for url in (_LOTTO_RESULT_REFERER, "https://www.dhlottery.co.kr/gameResult.do?method=byWin"):
|
||||
try:
|
||||
self._session.get(
|
||||
url,
|
||||
headers=warmup_headers,
|
||||
timeout=_REQUEST_TIMEOUT,
|
||||
verify=False,
|
||||
)
|
||||
self._session_warmed = True
|
||||
return
|
||||
except requests.RequestException:
|
||||
continue
|
||||
|
||||
def _parse_json_response(self, response):
|
||||
"""HTTP 응답에서 JSON dict를 파싱합니다. 실패 시 (None, reason) 반환."""
|
||||
if response.status_code != 200:
|
||||
return None, "http {}".format(response.status_code)
|
||||
text = response.text.strip()
|
||||
if not text.startswith("{"):
|
||||
return None, "non-json response"
|
||||
try:
|
||||
return json.loads(text), None
|
||||
except json.JSONDecodeError as ex:
|
||||
return None, str(ex)
|
||||
|
||||
def _normalize_lt645_record(self, item):
|
||||
"""
|
||||
lt645 내부 API 레코드를 기존 lotto_history.json 형식으로 변환합니다.
|
||||
|
||||
Args:
|
||||
item: data.list 항목 dict
|
||||
|
||||
Returns:
|
||||
변환된 dict 또는 파싱 실패 시 None
|
||||
"""
|
||||
if not isinstance(item, dict):
|
||||
return None
|
||||
|
||||
try:
|
||||
drw_no = int(item["ltEpsd"])
|
||||
nums = [
|
||||
int(item["tm1WnNo"]),
|
||||
int(item["tm2WnNo"]),
|
||||
int(item["tm3WnNo"]),
|
||||
int(item["tm4WnNo"]),
|
||||
int(item["tm5WnNo"]),
|
||||
int(item["tm6WnNo"]),
|
||||
]
|
||||
bonus = int(item["bnsWnNo"])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return None
|
||||
|
||||
date_raw = str(item.get("ltRflYmd", "")).strip()
|
||||
if len(date_raw) == 8 and date_raw.isdigit():
|
||||
drw_date = "{}-{}-{}".format(date_raw[:4], date_raw[4:6], date_raw[6:8])
|
||||
elif len(date_raw) == 10 and date_raw[4] == "-":
|
||||
drw_date = date_raw
|
||||
else:
|
||||
return None
|
||||
|
||||
record = {
|
||||
"returnValue": "success",
|
||||
"drwNoDate": drw_date,
|
||||
"drwNo": drw_no,
|
||||
"drwtNo1": nums[0],
|
||||
"drwtNo2": nums[1],
|
||||
"drwtNo3": nums[2],
|
||||
"drwtNo4": nums[3],
|
||||
"drwtNo5": nums[4],
|
||||
"drwtNo6": nums[5],
|
||||
"bnusNo": bonus,
|
||||
}
|
||||
if item.get("rnk1WnAmt") is not None:
|
||||
record["firstWinamnt"] = int(item["rnk1WnAmt"])
|
||||
if item.get("rnk1WnNope") is not None:
|
||||
record["firstPrzwnerCo"] = int(item["rnk1WnNope"])
|
||||
if item.get("rnk1SumWnAmt") is not None:
|
||||
record["firstAccumamnt"] = int(item["rnk1SumWnAmt"])
|
||||
if item.get("rlvtEpsdSumNtslAmt") is not None:
|
||||
record["totSellamnt"] = int(item["rlvtEpsdSumNtslAmt"])
|
||||
return record
|
||||
|
||||
def _fetch_draw_lt645(self, drw_no):
|
||||
"""동행복권 lt645 내부 API에서 단일 회차 결과를 가져옵니다."""
|
||||
self._warm_up_session()
|
||||
params = {"srchLtEpsd": int(drw_no), "_": int(time.time() * 1000)}
|
||||
verify_options = (_ssl_verify_arg(), False)
|
||||
last_error = "unknown"
|
||||
|
||||
for api_url in _LT645_API_URLS:
|
||||
for verify in verify_options:
|
||||
try:
|
||||
res = self._session.get(
|
||||
api_url,
|
||||
params=params,
|
||||
timeout=_REQUEST_TIMEOUT,
|
||||
verify=verify,
|
||||
)
|
||||
except requests.RequestException as ex:
|
||||
last_error = str(ex)
|
||||
continue
|
||||
|
||||
payload, parse_error = self._parse_json_response(res)
|
||||
if parse_error:
|
||||
last_error = parse_error
|
||||
continue
|
||||
|
||||
data = payload.get("data") if isinstance(payload, dict) else None
|
||||
items = data.get("list") if isinstance(data, dict) else None
|
||||
if not items:
|
||||
last_error = "lt645 empty list"
|
||||
continue
|
||||
|
||||
selected = None
|
||||
for item in items:
|
||||
if isinstance(item, dict) and int(item.get("ltEpsd", -1)) == int(drw_no):
|
||||
selected = item
|
||||
break
|
||||
if selected is None:
|
||||
selected = items[0]
|
||||
|
||||
record = self._normalize_lt645_record(selected)
|
||||
if record is None:
|
||||
last_error = "lt645 parse failed"
|
||||
continue
|
||||
if record["drwNo"] != int(drw_no):
|
||||
last_error = "lt645 draw mismatch"
|
||||
continue
|
||||
return record
|
||||
|
||||
self._last_fetch_error = last_error
|
||||
return None
|
||||
|
||||
def _fetch_draw_legacy(self, drw_no):
|
||||
"""기존 getLottoNumber API에서 단일 회차 결과를 가져옵니다."""
|
||||
verify_options = (_ssl_verify_arg(), False)
|
||||
last_error = "unknown"
|
||||
|
||||
for raw_url in _LOTTO_LEGACY_URLS:
|
||||
url = raw_url.format(int(drw_no))
|
||||
for verify in verify_options:
|
||||
for method in ("GET", "POST"):
|
||||
try:
|
||||
res = self._session.request(
|
||||
method,
|
||||
url,
|
||||
timeout=_REQUEST_TIMEOUT,
|
||||
verify=verify,
|
||||
)
|
||||
except requests.RequestException as ex:
|
||||
last_error = str(ex)
|
||||
continue
|
||||
|
||||
payload, parse_error = self._parse_json_response(res)
|
||||
if parse_error:
|
||||
last_error = parse_error
|
||||
continue
|
||||
if isinstance(payload, dict) and payload.get("returnValue") == "success":
|
||||
return payload
|
||||
rv = payload.get("returnValue") if isinstance(payload, dict) else "unknown"
|
||||
last_error = "api returnValue={}".format(rv)
|
||||
|
||||
self._last_fetch_error = last_error
|
||||
return None
|
||||
|
||||
def _can_reach_lottery_host(self):
|
||||
"""
|
||||
@@ -77,45 +257,28 @@ class DataCrawler:
|
||||
return False
|
||||
|
||||
def _fetch_draw(self, drw_no):
|
||||
"""동행복권 API에서 단일 회차 결과를 가져옵니다. 실패 시 None."""
|
||||
"""
|
||||
동행복권 API에서 단일 회차 결과를 가져옵니다.
|
||||
lt645 내부 API를 우선 사용하고, 실패 시 legacy API로 fallback 합니다.
|
||||
"""
|
||||
self._last_fetch_error = ""
|
||||
verify_options = (_ssl_verify_arg(), False)
|
||||
last_error = "unknown"
|
||||
|
||||
for attempt in range(1, _FETCH_RETRIES_PER_DRAW + 1):
|
||||
for raw_url in _LOTTO_URLS:
|
||||
url = raw_url.format(int(drw_no))
|
||||
for verify in verify_options:
|
||||
for method in ("POST", "GET"):
|
||||
try:
|
||||
res = self._session.request(
|
||||
method,
|
||||
url,
|
||||
timeout=_REQUEST_TIMEOUT,
|
||||
verify=verify,
|
||||
)
|
||||
if res.status_code != 200:
|
||||
last_error = "http {}".format(res.status_code)
|
||||
continue
|
||||
text = res.text.strip()
|
||||
if not text.startswith("{"):
|
||||
last_error = "non-json response"
|
||||
continue
|
||||
result = json.loads(text)
|
||||
except (
|
||||
requests.RequestException,
|
||||
ValueError,
|
||||
json.JSONDecodeError,
|
||||
) as ex:
|
||||
last_error = str(ex)
|
||||
continue
|
||||
if isinstance(result, dict) and result.get("returnValue") == "success":
|
||||
return result
|
||||
rv = result.get("returnValue") if isinstance(result, dict) else "unknown"
|
||||
last_error = "api returnValue={}".format(rv)
|
||||
result = self._fetch_draw_lt645(drw_no)
|
||||
if result is not None:
|
||||
return result
|
||||
last_error = self._last_fetch_error or "lt645 failed"
|
||||
|
||||
result = self._fetch_draw_legacy(drw_no)
|
||||
if result is not None:
|
||||
return result
|
||||
last_error = self._last_fetch_error or "legacy failed"
|
||||
|
||||
if attempt < _FETCH_RETRIES_PER_DRAW:
|
||||
# 지수 백오프 + 지터로 일시적 네트워크 혼잡 완화
|
||||
delay = _BACKOFF_BASE_SECONDS * (2 ** (attempt - 1)) + random.uniform(0, 0.25)
|
||||
time.sleep(delay)
|
||||
|
||||
self._last_fetch_error = last_error
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user