From c9de32463a7eb1a21dc097a64bd16ff6fcbbf1b0 Mon Sep 17 00:00:00 2001 From: wenxianping <931128603@qq.com> Date: Sun, 30 Sep 2018 14:35:27 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=AD=96=E7=95=A5=202=E3=80=81=E5=BF=AB=E8=AF=BB=E4=B8=8B?= =?UTF-8?q?=E5=8D=95=E4=BF=AE=E5=A4=8D=203=E3=80=81=E5=85=B3=E9=97=ADcdn?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init/select_ticket_info.py | 21 ++++++++++----------- inter/AutoSubmitOrderRequest.py | 4 ++-- inter/CheckUser.py | 9 +++++---- inter/Query.py | 14 +++++++++++--- myUrllib/httpUtils.py | 16 +++++++++++----- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/init/select_ticket_info.py b/init/select_ticket_info.py index 7767818..00a70be 100755 --- a/init/select_ticket_info.py +++ b/init/select_ticket_info.py @@ -123,8 +123,8 @@ class select: for i in range(len(cdn) - 1): http = HTTPClient() urls = self.urls["loginInit"] - start_time = datetime.datetime.now() http.cdn = cdn[i].replace("\n", "") + start_time = datetime.datetime.now() 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", ""))) @@ -140,20 +140,19 @@ class select: 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() + print(u"由于12306网站策略调整,cdn功能暂时关闭。") + # print(u"开启cdn查询") + # print(u"本次待筛选cdn总数为{}, 筛选时间大约为5-10min".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 = liftTicketInit(self) l.reqLiftTicketInit() self.call_login() checkUser(self).sendCheckUser() @@ -237,7 +236,7 @@ class select: self.station_trains), ( datetime.datetime.now() - start_time).microseconds / 1000, - self.httpClint.cdn, + wrapcache.get("cdn"), len( self.cdn_list), random_time) diff --git a/inter/AutoSubmitOrderRequest.py b/inter/AutoSubmitOrderRequest.py index 17f3d8d..c101e5c 100644 --- a/inter/AutoSubmitOrderRequest.py +++ b/inter/AutoSubmitOrderRequest.py @@ -24,17 +24,17 @@ class autoSubmitOrderRequest: stationTrainCode, leftTicket, set_type,): + self.set_type = set_type self.secretStr = urllib.unquote(secretStr) self.train_date = train_date self.query_from_station_name = query_from_station_name self.query_to_station_name = query_to_station_name - self.passengerTicketStr = passengerTicketStr + self.passengerTicketStr = passengerTicketStr.rstrip("_{0}".format(self.set_type)) self.oldPassengerStr = oldPassengerStr self.session = session self.train_no = train_no self.stationTrainCode = stationTrainCode self.leftTicket = leftTicket - self.set_type = set_type def data_par(self): """ diff --git a/inter/CheckUser.py b/inter/CheckUser.py index 81af1ea..960f0ce 100644 --- a/inter/CheckUser.py +++ b/inter/CheckUser.py @@ -11,9 +11,10 @@ class checkUser: def sendCheckUser(self): """ - 检查用户登录, 检查间隔为五分钟 + 检查用户登录, 检查间隔为2分钟 :return: """ + CHENK_TIME = 2 if wrapcache.get("user_time") is None: check_user_url = self.session.urls["check_user_url"] data = {"_json_att": ""} @@ -21,13 +22,13 @@ class checkUser: if check_user.get("data", False): check_user_flag = check_user["data"]["flag"] if check_user_flag is True: - wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * 5) + wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) else: if check_user['messages']: print (ticket.LOGIN_SESSION_FAIL.format(check_user['messages'])) self.session.call_login() - wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * 5) + wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) else: print (ticket.LOGIN_SESSION_FAIL.format(check_user['messages'])) self.session.call_login() - wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * 5) \ No newline at end of file + wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) \ No newline at end of file diff --git a/inter/Query.py b/inter/Query.py index 9c645f0..958813a 100644 --- a/inter/Query.py +++ b/inter/Query.py @@ -1,8 +1,13 @@ # coding=utf-8 import copy import datetime +import random + import wrapcache + +from config import urlConf from config.TicketEnmu import ticket +from myUrllib.httpUtils import HTTPClient class query: @@ -13,6 +18,8 @@ class query: def __init__(self, session, from_station, to_station, from_station_h, to_station_h, _station_seat, station_trains, ticke_peoples_num, station_dates=None,): self.session = session + self.httpClint = HTTPClient() + self.urls = urlConf.urls self.from_station = from_station self.to_station = to_station self.from_station_h = from_station_h @@ -45,9 +52,9 @@ class query: :return: """ for station_date in self.station_dates: - select_url = copy.copy(self.session.urls["select_url"]) + select_url = copy.copy(self.urls["select_url"]) select_url["req_url"] = select_url["req_url"].format(station_date, self.from_station, self.to_station) - station_ticket = self.session.httpClint.send(select_url) + station_ticket = self.httpClint.send(select_url) value = station_ticket.get("data", "") if not value: print (u'{0}-{1} 车次坐席查询为空'.format(self.from_station_h, self.to_station_h)) @@ -80,15 +87,16 @@ class query: if wrapcache.get(train_no): print(ticket.QUERY_IN_BLACK_LIST.format(train_no)) continue - else: if ticket_num != "有" and self.ticke_peoples_num > ticket_num: if self.session.is_more_ticket: print(u"余票数小于乘车人数,当前余票数: {}, 删减人车人数到: {}".format(ticket_num, ticket_num)) is_more_ticket_num = ticket_num else: + print(u"余票数小于乘车人数,当前设置不提交,放弃此次提交机会") continue else: + print(u"设置乘车人数为: {}".format(self.ticke_peoples_num)) is_more_ticket_num = self.ticke_peoples_num print (ticket.QUERY_C) return { diff --git a/myUrllib/httpUtils.py b/myUrllib/httpUtils.py index ca63d4d..36b4986 100755 --- a/myUrllib/httpUtils.py +++ b/myUrllib/httpUtils.py @@ -1,13 +1,11 @@ # -*- coding: utf8 -*- import json import socket -import urllib from collections import OrderedDict from time import sleep - import requests - from config import logger +import wrapcache def _set_header_default(): @@ -102,6 +100,8 @@ class HTTPClient(object): req_url = urls.get("req_url", "") re_try = urls.get("re_try", 0) s_time = urls.get("s_time", 0) + is_cdn = urls.get("is_cdn", False) + is_test_cdn = urls.get("is_test_cdn", False) error_data = {"code": 99999, "message": u"重试次数达到上限"} if data: method = "post" @@ -114,8 +114,14 @@ class HTTPClient(object): logger.log( u"url: {0}\n入参: {1}\n请求方式: {2}\n".format(req_url, data, method, )) self.setHeadersHost(urls["Host"]) - if self.cdn: - url_host = self.cdn + if is_test_cdn: + url_host = self._cdn + elif is_cdn: + cdn = wrapcache.get("cdn") + if cdn: + url_host = cdn + else: + url_host = urls["Host"] else: url_host = urls["Host"] for i in range(re_try):