Created
January 23, 2018 02:46
-
-
Save glenn-kim/9143dc0ae3eea756621fc5bdc6bbcb5e to your computer and use it in GitHub Desktop.
SRT 예매 시도 스크립트.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# srt.py | |
# 코드 표 | |
# 공주:0514 | |
# 광주송정:0036 | |
# 구미):0507 | |
# 나주:0037 | |
# 대전:0010 | |
# 동대구:0015 | |
# 동탄:0552 | |
# 목포:0041 | |
# 부산:0020 | |
# 수서:0551 | |
# 신경주:0508 | |
# 오송:0297 | |
# 울산:0509 | |
# 익산:0030 | |
# 정읍:0033 | |
# 지제:0553 | |
# 천안아산:0502 | |
import requests | |
import re | |
from bs4 import BeautifulSoup | |
import time | |
id = "##########" # 회원번호(숫자) | |
passwd = "####" # <4자리 비밀번호> | |
session = requests.Session() | |
def refresh_key(key): | |
if not key: | |
keyresp = session.get( | |
"http://211.173.10.71/ts.wseq?opcode=5101&nfid=0&prefix=NetFunnel.gRtype=5101;&sid=service_1&aid=act_11&js=true") | |
return (re.search("key=([^&]+)&", keyresp.text).group(1)) | |
else: | |
print( | |
"http://211.173.10.79/ts.wseq?opcode=5003&key=" + key + "&nfid=0&prefix=NetFunnel.gRtype=5003;&sid=service_1&aid=act_11&js=true&1516665731513") | |
keyresp = session.get( | |
"http://211.173.10.79/ts.wseq?opcode=5003&key=" + key + "&nfid=0&prefix=NetFunnel.gRtype=5003;&sid=service_1&aid=act_11&js=true&1516665731513") | |
print(keyresp.text) | |
return (re.search("key=([^&]+)&", keyresp.text).group(1)) | |
key = refresh_key(None) | |
login_url = "https://etk.srail.co.kr/hpg/hbt/06/selectBtLoginInfo.do" | |
body = { | |
"replace": "http://etk.srail.co.kr/hpg/hbt/06/selectMainFrame.do", | |
"srchDvNm": id, | |
"hmpgPwdCphd": passwd | |
} | |
res = session.post(login_url, data=body) | |
session.post("http://etk.srail.co.kr/hpg/hbt/06/selectMainFrame.do", data={"referer": "etk.srail.co.kr"}) | |
sc_data = { | |
"dptRsStnCdNm": "수서", | |
"arvRsStnCdNm": "동대구", | |
"stlbTrnClsfCd": "05", | |
"trnGpCd": "300", | |
"psgNum": "1", | |
"psgNum1": "1", | |
"psgNum2": "1", | |
"isRequest": "Y", | |
"menuId": "11", | |
"prechtnDvCd": "1", | |
"postchtnDvCd": "1", | |
"rsvTpCd": "02", | |
"dptRsStnCd": "0551", # 출발 역 코드 | |
"arvRsStnCd": "0015", # 도착 역 코드 | |
"chtnDvCd": "1", | |
"dptDt": "20180214", # 출발날짜 | |
"dptTm": "150000", # 출발시간 (해당 시간 이후 시간 검색) | |
"psgInfoPerPrnb1": "1", | |
"psgInfoPerPrnb5": "0", | |
"psgInfoPerPrnb4": "0", | |
"psgInfoPerPrnb2": "0", | |
"psgInfoPerPrnb3": "0", | |
"seatAttCd": "015", | |
} | |
def try_reserve(infos, cls): | |
data = { | |
"chtnDvCd": "1", | |
"dataType": "B", | |
"comeFlg": "N", | |
"rqSeatAttCd": "015", | |
"stjrnygoGridcnt": "1", | |
"dptDt1": "20180214", | |
"dptRsStnCd1": "0551", | |
"dptTm1": "060000", | |
"arvRsStnCd1": "0020", | |
"trnNo1": "00303", | |
"runDt1": "20180214", | |
"trnGpCd1": "300", | |
"roomClsfCd1": cls, | |
"trnOrdrNo1": "", | |
"scarYn1": "N", | |
"psrmClCd1": cls, | |
"totPrnb": "1", | |
"stcustGridcnt": "1", | |
"psgInfoPerPrnb1": "1", | |
"psgInfoPerPrnb2": "0", | |
"psgInfoPerPrnb3": "0", | |
"psgInfoPerPrnb4": "0", | |
"psgInfoPerPrnb5": "0" | |
} | |
print(data) | |
for key in data.keys(): | |
ckey = key | |
if (re.match("^(.+)\d+$", key)): | |
ckey = re.match("^(.+)\d$", key).group(1) | |
v = infos.get(key, None) or sc_data.get(key, None) or infos.get(ckey, None) or sc_data.get(ckey, None) | |
if v: | |
data[key] = v | |
res = session.post("http://etk.srail.co.kr/hpg/hbt/02/lrgCrgTicketReservationInfoReversion.do", data=data, | |
headers={"Referer": "http://etk.srail.co.kr/hpg/hbt/01/selectLrgCrgScheduleList.do"}) | |
if not ("고객님께서 요구하신 열차는 없거나 예약이 불가능한 열차입니다." in res.text): | |
print(res.text) | |
return not ("고객님께서 요구하신 열차는 없거나 예약이 불가능한 열차입니다." in res.text) | |
ticket_limit = 3 | |
while (ticket_limit > 0): | |
print("sleep 500") | |
time.sleep(0.5) | |
print("trying..") | |
# key = refresh_key(key) | |
session.get( | |
"http://211.173.10.71/ts.wseq?opcode=5101&nfid=0&prefix=NetFunnel.gRtype=5101;&sid=service_1&aid=act_12&js=true") | |
schedule_url = "http://etk.srail.co.kr/hpg/hbt/01/selectLrgCrgScheduleList.do" | |
res = session.post(schedule_url, data=sc_data, | |
headers={"Referer": "http://etk.srail.co.kr/hpg/hbt/01/selectLrgCrgScheduleList.do"}) | |
soup = BeautifulSoup(res.text) | |
trs = soup.select(".srt-table-tab-center") | |
for tr in trs: | |
infos_soup = tr.select_one('.trnNo') | |
infos = {} | |
for inp in infos_soup.select("input[type=hidden]"): | |
key = re.search("^(.+)\[\d+\]$", inp['name']).group(1) | |
val = inp['value'] | |
infos[key] = val | |
for btn in tr.select(".srt-btn-pink"): | |
cls = re.search("'([12])'\);", btn['onclick']).group(1) | |
result = try_reserve(infos, cls) | |
print(result) | |
if (result): | |
ticket_limit -= 1 | |
else: | |
time.sleep(0.5) | |
if ticket_limit == 0: | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment