python 实现12306刷票提醒

又到一年一度的12306了。

去年python写的全自动抢票脚本不能用了。

今年的验证码太强大了。

今年暂不深入研究它,仅用python查询是否有票提醒,然后给你打个电话。绝对能够提醒你。

然后手工,上代码:

#coding=utf-8
import urllib2
import json
import time
import hashlib
import pynotify

i=0
def notify():
    strs="来了来了。。。。"
    pynotify.init ("12306s")
    n = pynotify.Notification ("Hello DL",strs)
    n.show()
    print "good_notify"

def call():
    username="xxx"
    to_phone="xxx"
    code="12306"
    rpass="123456"
    password=username+rpass+to_phone+code
    password=hashlib.md5(password).hexdigest()
    callurl="http://www.iaixin.com/index.php/voice/ajax_api_add?username=%s&password=%s&to_phone=%s&code=%s" %(username,password,to_phone,code)
    request = urllib2.Request(callurl)
    urllib2.urlopen(request)
    print("good")

def musearch():
    for i in ['2016-02-04','2016-02-05','2016-02-06']:
        search(i)

def search(date_str):
    global i
    url="https://kyfw.12306.cn/otn/leftTicket/queryT?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=CQW&purpose_codes=ADULT" %(date_str)
    req = urllib2.Request(url)
    try:
        fd = urllib2.urlopen(req)
        result= fd.read()
    except:
        return False
    try:
        decodejson = json.loads(result)
    except:
        return False
    if not decodejson.has_key('data'):
        return False
    for value in decodejson['data']:
        if value['queryLeftNewDTO']['station_train_code']!='G307' and value['queryLeftNewDTO']['station_train_code']!='G309':
            continue
        ze_num=value['queryLeftNewDTO']['ze_num']
        #print date_str+":"+value['queryLeftNewDTO']['station_train_code']
        #print(ze_num)
        if ze_num== u"无":
            continue
        now=time.time();
        print(ze_num+":"+date_str+value['queryLeftNewDTO']['station_train_code'])
        #notify()
        if now-i >20:
            i=time.time()
            #call()
            notify()

while 1:
    musearch()
    time.sleep(1)

发表评论

电子邮件地址不会被公开。 必填项已用*标注