Example #1
0
def main():
    load_argvs()
    CommonLog.print_welcome()
    # 初始化配置
    App.run()
    CommonLog.print_configs()
    # 加载站点信息
    App.did_start()
    #print('App.did_start() done.')
    # 检查运行环境的相关目录以及验证码校验的api用户名密码设置是否正确
    App.run_check()
    #print('App.run_check() done')
    # 初始化查询任务,获取余票查询url
    Query.check_before_run()
    #print('Query.check_before_run() done')

    ####### 运行任务
    Web.run()
    Cdn.run()
    #12306账号登录
    User.run()
    Query.run()
    if not Const.IS_TEST:
        while True:
            sleep(10000)
    else:
        if Config().is_cluster_enabled(): stay_second(5)  # 等待接受完通知
    CommonLog.print_test_complete()
Example #2
0
 def query_by_date(self, date):
     """
     通过日期进行查询
     :return:
     """
     from py12306.helpers.cdn import Cdn
     QueryLog.add_log(('\n' if not is_main_thread() else '') +
                      QueryLog.MESSAGE_QUERY_START_BY_DATE.format(
                          date, 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=self.query.api_type)
     print("ticket query url=%s" % url)
     if Config.is_cdn_enabled() and Cdn().is_ready:
         self.is_cdn = True
         print("12306 cdn is enabled.")
         return self.query.session.cdn_request(url,
                                               timeout=self.query_time_out,
                                               allow_redirects=False)
     self.is_cdn = False
     return self.query.session.get(url,
                                   timeout=self.query_time_out,
                                   allow_redirects=False)
Example #3
0
    def cdn_request(self, url: str, cdn=None, method='GET', **kwargs):
        from py12306.helpers.api import HOST_URL_OF_12306
        from py12306.helpers.cdn import Cdn
        if not cdn: cdn = Cdn.get_cdn()
        url = url.replace(HOST_URL_OF_12306, cdn)

        return self.request(method, url, headers={'Host': HOST_URL_OF_12306}, verify=False, **kwargs)
Example #4
0
def main():
    load_argvs()
    CommonLog.print_welcome()
    App.run()
    CommonLog.print_configs()
    App.did_start()

    App.run_check()
    Query.check_before_run()

    ####### 运行任务
    Web.run()
    Cdn.run()
    User.run()
    Query.run()
    if not Const.IS_TEST:
        while True:
            sleep(10000)
    else:
        if Config().is_cluster_enabled(): stay_second(5)  # 等待接受完通知
    CommonLog.print_test_complete()
Example #5
0
 def update_configs_from_remote(self, envs, first=False):
     if envs == self.envs: return
     from py12306.query.query import Query
     from py12306.user.user import User
     from py12306.helpers.cdn import Cdn
     self.envs = envs
     for key, value in envs:
         if key in self.disallow_update_configs: continue
         if value != -1:
             old = getattr(self, key)
             setattr(self, key, value)
             if not first and old != value:
                 if key == 'USER_ACCOUNTS':
                     User().update_user_accounts(auto=True, old=old)
                 elif key == 'QUERY_JOBS':
                     Query().update_query_jobs(auto=True)  # 任务修改
                 elif key == 'QUERY_INTERVAL':
                     Query().update_query_interval(auto=True)
                 elif key == 'CDN_ENABLED':
                     Cdn().update_cdn_status(auto=True)
Example #6
0
def dashboard():
    """
    状态统计
    任务数量,用户数量,查询次数
    节点信息(TODO)
    :return:
    """
    from py12306.log.query_log import QueryLog
    query_job_count = len(Query().jobs)
    user_job_count = len(User().users)
    query_count = QueryLog().data.get('query_count')
    res = {
        'query_job_count': query_job_count,
        'user_job_count': user_job_count,
        'query_count': query_count,
    }
    if Config().CDN_ENABLED:
        from py12306.helpers.cdn import Cdn
        res['cdn_count'] = len(Cdn().available_items)
    return jsonify(res)
Example #7
0
    def query_by_date(self, date):
        """
        通过日期进行查询
        :return:
        """
        from py12306.helpers.cdn import Cdn
        QueryLog.add_log(('\n' if not is_main_thread() else '') +
                         QueryLog.MESSAGE_QUERY_START_BY_DATE.format(
                             date, 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')
        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)
        self.is_cdn = False
        if Config.is_zf():
            headers = ZFProxyUtil.getProxySign()
            proxy = ZFProxyUtil.getProxy()
            yp_by_data = self.query.session.get(url,
                                                headers=headers,
                                                proxies=proxy,
                                                verify=False,
                                                allow_redirects=False,
                                                timeout=self.query_time_out)
        else:
            yp_by_data = self.query.session.get(url,
                                                verify=False,
                                                allow_redirects=False,
                                                timeout=self.query_time_out)

        return yp_by_data