diff --git a/agency/cdn_utils.py b/agency/cdn_utils.py index 2182a2e..cc4364d 100755 --- a/agency/cdn_utils.py +++ b/agency/cdn_utils.py @@ -88,7 +88,7 @@ class CDNProxy: def all_cdn(self): """获取cdn列表""" - with open('../cdn_list', 'r') as f: + with open('cdn_list', 'r') as f: cdn = f.readlines() return cdn @@ -101,6 +101,9 @@ class CDNProxy: print(cdn_ip[0]) + + + if __name__ == '__main__': cdn = CDNProxy() cdn.get_cdn_list() diff --git a/config/ticket_config.yaml b/config/ticket_config.yaml index 796e5f2..097c9c1 100755 --- a/config/ticket_config.yaml +++ b/config/ticket_config.yaml @@ -1,10 +1,11 @@ --- +# 配置文件请严格遵循yaml语法格式,yaml学习地址 https://ansible-tran.readthedocs.io/en/latest/docs/YAMLSyntax.html set: - # 出发日期,格式ex:2018-01-06 + # 出发日期(list),格式ex:2018-01-06 station_dates: - "2018-09-05" - # 过滤车次,格式ex:- "G1353" - "G1329" + # 过滤车次(list),格式ex:- "G1353" - "G1329" station_trains: - "G6504" @@ -14,23 +15,21 @@ set: # 到达城市 比如深圳北,就填深圳就搜得到 to_station: "广州" - # 座位 多个座位ex: - "二等座" - "一等座" + # 座位(list) 多个座位ex: - "二等座" - "一等座" set_type: - "二等座" # 余票不足是否自动提交,目前应该没什么卵用 is_more_ticket: True - # 乘车人 多个乘车人ex: - "张三" - "李四" + # 乘车人(list) 多个乘车人ex: - "张三" - "李四" ticke_peoples: - "" - # 12306登录账号 + # 12306登录账号(list) 12306account: - user: "" - pwd: "" -select_refresh_interval: 1 - # 加入小黑屋时间,此功能为了防止僵尸票导致一直下单不成功错过正常的票 ticket_black_list_time: 5 @@ -66,11 +65,11 @@ email_conf: password: "" host: "smtp.qq.com" -# 是否开启cdn查询,可以更快的检测飘飘 1为开启,2为关闭 +# 是否开启cdn查询,可以更快的检测票票 1为开启,2为关闭 is_cdn: 2 # 下单接口分为两种,1 为快速下单,2 是普通下单 -order_type: 1 +order_type: 2 diff --git a/init/login.py b/init/login.py index c14a6c0..c228243 100755 --- a/init/login.py +++ b/init/login.py @@ -1,16 +1,8 @@ -#!/bin/env python # -*- coding=utf-8 -*- -import copy -import random -import json -import re -import socket from time import sleep from config.ticketConf import _get_yaml -from PIL import Image from damatuCode.damatuWeb import DamatuApi -from damatuCode.ruokuai import RClient from inter.GetPassCodeNewOrderAndLogin import getPassCodeNewOrderAndLogin from inter.GetRandCode import getRandCode from myException.UserPasswordException import UserPasswordException diff --git a/init/select_ticket_info.py b/init/select_ticket_info.py index 3877108..32fffe1 100755 --- a/init/select_ticket_info.py +++ b/init/select_ticket_info.py @@ -3,8 +3,11 @@ import datetime import random import socket import sys +import threading import time import wrapcache + +from agency.cdn_utils import CDNProxy from config import urlConf from config.TicketEnmu import ticket from config.ticketConf import _get_yaml @@ -33,7 +36,7 @@ class select: def __init__(self): self.from_station, self.to_station, self.station_dates, self._station_seat, self.is_more_ticket, \ - self.ticke_peoples, self.select_refresh_interval, self.station_trains, self.ticket_black_list_time, \ + self.ticke_peoples, self.station_trains, self.ticket_black_list_time, \ self.order_type = self.get_ticket_info() self.is_auto_code = _get_yaml()["is_auto_code"] self.auto_code_type = _get_yaml()["auto_code_type"] @@ -57,12 +60,11 @@ class select: set_type = ticket_info_config["set"]["set_type"] is_more_ticket = ticket_info_config["set"]["is_more_ticket"] ticke_peoples = ticket_info_config["set"]["ticke_peoples"] - select_refresh_interval = ticket_info_config["select_refresh_interval"] station_trains = ticket_info_config["set"]["station_trains"] ticket_black_list_time = ticket_info_config["ticket_black_list_time"] order_type = ticket_info_config["order_type"] print u"*" * 20 - print u"12306刷票小助手,最后更新于2018.2.28,请勿作为商业用途,交流群号:286271084" + print u"12306刷票小助手,最后更新于2018.8.31,请勿作为商业用途,交流群号:286271084" print u"如果有好的margin,请联系作者,表示非常感激\n" print u"当前配置:出发站:{0}\n到达站:{1}\n乘车日期:{2}\n坐席:{3}\n是否有票自动提交:{4}\n乘车人:{5}\n" \ u"刷新间隔:随机(1-4S)\n候选购买车次:{7}\n僵尸票关小黑屋时长:{8}\n 下单接口:{9}\n".format \ @@ -73,13 +75,12 @@ class select: ",".join(set_type), is_more_ticket, ",".join(ticke_peoples), - select_refresh_interval, ",".join(station_trains), ticket_black_list_time, order_type, ) print u"*" * 20 - return from_station, to_station, station_dates, set_type, is_more_ticket, ticke_peoples, select_refresh_interval, station_trains, ticket_black_list_time, order_type + return from_station, to_station, station_dates, set_type, is_more_ticket, ticke_peoples, station_trains, ticket_black_list_time, order_type def station_table(self, from_station, to_station): """ @@ -108,13 +109,56 @@ class select: else: self.login.go_login() + def set_cdn(self): + """ + 设置cdn + :return: + """ + if self.is_cdn == 1: + while True: + if self.cdn_list: + self.httpClint.cdn = self.cdn_list[random.randint(0, len(self.cdn_list) - 1)] + + def cdn_req(self, cdn): + for i in range(len(cdn) - 1): + http = HTTPClient() + urls = self.urls["loginInit"] + start_time = datetime.datetime.now() + http.cdn = cdn[i].replace("\n", "") + rep = http.send(urls) + if rep and "message" not in rep and (datetime.datetime.now() - start_time).microseconds / 1000 < 500: + print("加入cdn {0}".format(cdn[i].replace("\n", ""))) + self.cdn_list.append(cdn[i].replace("\n", "")) + print(u"所有cdn解析完成...") + + def cdn_certification(self): + """ + cdn 认证 + :return: + """ + if self.is_cdn == 1: + CDN = CDNProxy() + all_cdn = CDN.all_cdn() + if all_cdn: + print(u"开启cdn查询") + print(u"本次待筛选cdn总数为{}".format(len(all_cdn))) + t = threading.Thread(target=self.cdn_req, args=(all_cdn,)) + t2 = threading.Thread(target=self.set_cdn, args=()) + t.start() + t2.start() + else: + raise ticketConfigException(u"cdn列表为空,请先加载cdn") + else: + pass + def main(self): + self.cdn_certification() l = liftTicketInit(session=self) l.reqLiftTicketInit() self.call_login() checkUser(self).sendCheckUser() from_station, to_station = self.station_table(self.from_station, self.to_station) - num = 1 + num = 0 while 1: try: num += 1 @@ -184,18 +228,16 @@ class select: else: - random_time = random.uniform(1, 4) - time.sleep(round(random_time, 2)) + random_time = round(random.uniform(1, 4), 2) + time.sleep(random_time) print u"正在第{0}次查询 随机停留时长:{6} 乘车日期: {1} 车次:{2} 查询无票 cdn轮询IP:{4}当前cdn总数:{5} 总耗时:{3}ms".format(num, ",".join( self.station_dates), ",".join( self.station_trains), - ( - datetime.datetime.now() - start_time).microseconds / 1000, + (datetime.datetime.now() - start_time).microseconds / 1000, self.httpClint.cdn, - len( - self.cdn_list), + len(self.cdn_list), random_time) except PassengerUserException as e: print e.message diff --git a/inter/GetPassCodeNewOrderAndLogin.py b/inter/GetPassCodeNewOrderAndLogin.py index 3445b14..a1907ee 100644 --- a/inter/GetPassCodeNewOrderAndLogin.py +++ b/inter/GetPassCodeNewOrderAndLogin.py @@ -18,5 +18,5 @@ def getPassCodeNewOrderAndLogin(session, imgType): try: print(u"下载验证码成功") open(img_path, 'wb').write(result) - except OSError as e: - print (e) + except OSError: + print (u"验证码下载失败,可能ip被封,确认请手动请求: {0}".format(codeImgUrl)) diff --git a/inter/GetRandCode.py b/inter/GetRandCode.py index 4370cbd..432aa6e 100644 --- a/inter/GetRandCode.py +++ b/inter/GetRandCode.py @@ -23,7 +23,7 @@ def getRandCode(is_auto_code, auto_code_type): return codexy(Ofset=",".join(list(Result["Result"])), is_raw_input=False) else: if "Error" in Result and Result["Error"]: - print Result["Error"] + print u"打码平台错误: {0}, 请登录打码平台查看-http://www.ruokuai.com/client/index?6726".format(Result["Error"]) return "" else: img = Image.open('./tkcode') @@ -39,7 +39,14 @@ def codexy(Ofset=None, is_raw_input=True): :return: str """ if is_raw_input: - Ofset = raw_input(u"请输入验证码: ") + print(u""" + ***************** + | 0 | 1 | 2 | 3 | + ***************** + | 4 | 5 | 6 | 7 | + ***************** + """) + Ofset = raw_input(u"按照上图提示输入对应的验证码: ") Ofset = Ofset.replace(",", ",") select = Ofset.split(',') post = [] diff --git a/inter/Query.py b/inter/Query.py index d5a9ec1..50795c5 100644 --- a/inter/Query.py +++ b/inter/Query.py @@ -49,7 +49,7 @@ class query: station_ticket = self.session.httpClint.send(select_url) value = station_ticket.get("data", "") if not value: - print (u'{0}-{1} 车次坐席查询为空'.format(self.from_station, self.to_station)) + print (u'{0}-{1} 车次坐席查询为空'.format(self.from_station_h, self.to_station_h)) else: result = value.get('result', []) if result: @@ -67,15 +67,12 @@ class query: stationTrainCode = ticket_info[3] leftTicket = ticket_info[12] set_type = self._station_seat[j] - print (u'车次: {0} 始发车站: {1} 终点站: {2} {3}: {4}'.format(train_no, + print (u'车次: {0} 始发车站: {1} 终点站: {2} {3}: {4}'.format(ticket_info[3], self.from_station_h, self.to_station_h, self._station_seat[j].encode( "utf8"), - ticket_info[self.station_seat( - self._station_seat[ - j].encode("utf8"))] - )) + ticket_info[self.station_seat(self._station_seat[j].encode("utf8"))])) if wrapcache.get(train_no): print(ticket.QUERY_IN_BLACK_LIST.format(train_no)) break diff --git a/myUrllib/httpUtils.py b/myUrllib/httpUtils.py index 1b404d9..ca63d4d 100755 --- a/myUrllib/httpUtils.py +++ b/myUrllib/httpUtils.py @@ -113,7 +113,7 @@ class HTTPClient(object): if is_logger: logger.log( u"url: {0}\n入参: {1}\n请求方式: {2}\n".format(req_url, data, method, )) - # self.setHeadersHost(urls["Host"]) + self.setHeadersHost(urls["Host"]) if self.cdn: url_host = self.cdn else: