2019年5月14日 星期二

台鐵訂票系統外掛

工具程式碼
#為什麼只要勾選「我不是機器人」,Google 就知道你不是機器人
#https://buzzorange.com/techorange/2017/09/13/captcha-and-recaptcha/
#台鐵訂票系統
#https://tip.railway.gov.tw/tra-tip-web/tip/tip001/tip121/query
import pyautogui as ag
import pyperclip as clip
import random as rnd
ID='A195077385'
Departure ='台南'
Destination = '台北'
Date = "2019/05/19"
startTime = "13"
endTime = "14"
Num=2
# 在螢幕上找到和影像檔 f 相似度大於 c 的位址,並移動過去
# dx,dy 代表實際目標的水平修正量及垂直修正量
def move2Pic(f,c=0.95,dx=0,dy=0,dur=0):
    try:
        x,y=ag.locateCenterOnScreen(f,confidence=0.95)
        ag.moveTo(x+dx,y+dy,dur)
        return True
    except:pass
    return False
# 在螢幕上找到和影像檔 f 相似度大於 c 的位址,並移動過去點一下
# dx,dy 代表實際目標的水平修正量及垂直修正量
def clickPic(f,c=0.95,dx=0,dy=0,dur=0):
    if move2Pic(f,c,dx,dy,dur): 
        ag.click()
        return True
    else: return False

def R(s=0.5,e=0.9):#取亂數,預設0.5~0.914之間
    return rnd.uniform(0.5,0.9)

def copyPast(Text):#將文字放到剪貼簿並按下 ctrl+v
    clip.copy(Text)
    ag.hotkey('ctrl','v')
主程式
clickPic('enterID.png',dy=30,dur=R())#輸入身份証
for i in range(8):ag.scroll(25)
copyPast(ID)
ag.time.sleep(R())

clickPic('Departure.png',dy=30,dur=R())#輸入出發站
copyPast(Departure)
ag.time.sleep(R())

clickPic('Destination.png',dy=30,dur=R())#輸入抵達站
copyPast(Destination)
ag.time.sleep(R())

for i in range(Num-1):#選擇數量
    clickPic('plus.png',dur=R())
    ag.time.sleep(R())
    
clickPic('asTime.png',dur=R()) #依時段

for i in range(20):ag.scroll(-25)
#連點三下全選的日期
move2Pic('Date.png',dx=10,dy=30)
ag.doubleClick()
ag.click()
ag.time.sleep(R())
copyPast(Date) #貼上目標日期
ag.time.sleep(R())
for i in range(4):
    ag.press('tab')
    ag.time.sleep(R())
ag.typewrite(startTime,R(0.1,0.5))
ag.time.sleep(R())
ag.press('tab')
ag.time.sleep(R())
ag.typewrite(endTime,R(0.1,0.5))

move2Pic('imNotRobot.png',dx=-50,dur=R())
ag.time.sleep(R())
#ag.click()

#move2Pic('getTicket.png',dx=-50,dur=rnd.uniform(0.5,0.9))