增加动态获取查询地址 #121

This commit is contained in:
Jalin
2019-02-20 23:11:55 +08:00
parent 733d512f72
commit 365453e0b3
3 changed files with 20 additions and 1 deletions

View File

@@ -39,6 +39,7 @@ API_INITDC_URL = BASE_URL_OF_12306 + '/otn/confirmPassenger/initDc' # 生成订
API_GET_QUEUE_COUNT = BASE_URL_OF_12306 + '/otn/confirmPassenger/getQueueCount'
API_CONFIRM_SINGLE_FOR_QUEUE = BASE_URL_OF_12306 + '/otn/confirmPassenger/confirmSingleForQueue'
API_QUERY_ORDER_WAIT_TIME = BASE_URL_OF_12306 + '/otn/confirmPassenger/queryOrderWaitTime?{}' # 排队查询
API_QUERY_INIT_PAGE = BASE_URL_OF_12306 + '/otn/leftTicket/init'
API_NOTIFICATION_BY_VOICE_CODE = 'http://ali-voice.showapi.com/sendVoice?'
API_NOTIFICATION_BY_VOICE_CODE_DINGXIN = 'http://yuyin2.market.alicloudapi.com/dx/voice_notice'

View File

@@ -146,7 +146,7 @@ class Job:
self.left_station,
self.arrive_station))
url = LEFT_TICKETS.get('url').format(left_date=date, left_station=self.left_station_code,
arrive_station=self.arrive_station_code, type='leftTicket/queryZ')
arrive_station=self.arrive_station_code, type=self.query.api_type)
if Config.is_cdn_enabled() and Cdn().is_ready:
self.is_cdn = True
return self.query.session.cdn_request(url, timeout=self.query_time_out, allow_redirects=False)

View File

@@ -5,6 +5,7 @@ from py12306.helpers.func import *
from py12306.helpers.request import Request
from py12306.log.query_log import QueryLog
from py12306.query.job import Job
from py12306.helpers.api import API_QUERY_INIT_PAGE
@singleton
@@ -24,12 +25,14 @@ class Query:
is_in_thread = False
retry_time = 3
is_ready = False
api_type = None # Query api url, Current know value leftTicket/queryX | leftTicket/queryZ
def __init__(self):
self.session = Request()
self.cluster = Cluster()
self.update_query_interval()
self.update_query_jobs()
self.get_query_api_type()
def update_query_interval(self, auto=False):
self.interval = init_interval_by_number(Config().QUERY_INTERVAL)
@@ -138,6 +141,21 @@ class Query:
self = cls()
return objects_find_object_by_key_value(self.jobs, 'account_key', account_key)
@classmethod
def get_query_api_type(cls):
import re
self = cls()
if self.api_type:
return self.api_type
response = self.session.get(API_QUERY_INIT_PAGE)
if response.status_code == 200:
res = re.search(r'var CLeftTicketUrl = \'(leftTicket/queryX)\';', response.text)
try:
self.api_type = res.group(1)
except IndexError:
pass
return cls.get_query_api_type()
# def get_jobs_from_cluster(self):
# jobs = self.cluster.session.get_dict(Cluster.KEY_JOBS)
# return jobs