增加 夜间休息限制
This commit is contained in:
6
main.py
6
main.py
@@ -2,16 +2,16 @@
|
||||
import os
|
||||
from threading import Thread
|
||||
|
||||
from py12306.log.query_log import QueryLog
|
||||
from py12306.helpers.func import *
|
||||
from py12306.query.query import Query
|
||||
from py12306.user.user import User
|
||||
|
||||
|
||||
def main():
|
||||
# Thread(target=Query.run).start() # 余票查询
|
||||
# QueryLog.add_log('init')
|
||||
create_thread_and_run(User, 'run', wait=False)
|
||||
Query.run()
|
||||
# Query.run()
|
||||
User.run()
|
||||
pass
|
||||
|
||||
|
||||
|
||||
13
py12306/helpers/app.py
Normal file
13
py12306/helpers/app.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from py12306.helpers.func import *
|
||||
from py12306.log.common_log import CommonLog
|
||||
|
||||
|
||||
def app_available_check():
|
||||
now = time_now()
|
||||
if now.hour >= 23 or now.hour < 6:
|
||||
CommonLog.add_quick_log(CommonLog.MESSAGE_12306_IS_CLOSED.format(time_now())).flush()
|
||||
open_time = datetime.datetime(now.year, now.month, now.day, 6)
|
||||
if open_time < now:
|
||||
open_time += datetime.timedelta(1)
|
||||
sleep((open_time - now).seconds)
|
||||
return True
|
||||
@@ -73,4 +73,17 @@ def current_thread_id():
|
||||
|
||||
def time_now():
|
||||
return datetime.datetime.now()
|
||||
|
||||
|
||||
def create_thread_and_run(jobs, callback_name, wait=True):
|
||||
threads = []
|
||||
if not isinstance(jobs, list):
|
||||
jobs = [jobs]
|
||||
for job in jobs:
|
||||
thread = threading.Thread(target=getattr(job, callback_name))
|
||||
thread.start()
|
||||
threads.append(thread)
|
||||
if wait:
|
||||
for thread in threads: thread.join()
|
||||
|
||||
# def test:
|
||||
|
||||
@@ -4,6 +4,7 @@ from py12306.helpers.func import *
|
||||
|
||||
@singleton
|
||||
class CommonLog(BaseLog):
|
||||
MESSAGE_12306_IS_CLOSED = '当前时间: {} | 12306 休息时间,程序将在明天早上 6 点自动运行'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
@@ -2,6 +2,7 @@ import threading
|
||||
|
||||
from requests_html import HTMLSession
|
||||
|
||||
from py12306.helpers.app import app_available_check
|
||||
from py12306.helpers.func import *
|
||||
from py12306.log.query_log import QueryLog
|
||||
from py12306.query.job import Job
|
||||
@@ -25,6 +26,7 @@ class Query:
|
||||
@classmethod
|
||||
def run(cls):
|
||||
self = cls()
|
||||
app_available_check()
|
||||
self.start()
|
||||
pass
|
||||
|
||||
@@ -32,13 +34,9 @@ class Query:
|
||||
self.init_jobs()
|
||||
QueryLog.print_init_jobs(jobs=self.jobs)
|
||||
while True:
|
||||
threads = []
|
||||
app_available_check()
|
||||
if config.QUERY_JOB_THREAD_ENABLED: # 多线程
|
||||
for job in self.jobs:
|
||||
thread = threading.Thread(target=job.run)
|
||||
thread.start()
|
||||
threads.append(thread)
|
||||
for thread in threads: thread.join()
|
||||
create_thread_and_run(jobs=self.jobs, callback_name='run')
|
||||
else:
|
||||
for job in self.jobs:
|
||||
job.run()
|
||||
|
||||
@@ -2,6 +2,7 @@ import pickle
|
||||
from os import path
|
||||
|
||||
from py12306.helpers.api import API_USER_CHECK, API_BASE_LOGIN, API_AUTH_UAMTK, API_AUTH_UAMAUTHCLIENT, API_USER_INFO
|
||||
from py12306.helpers.app import *
|
||||
from py12306.helpers.auth_code import AuthCode
|
||||
from py12306.helpers.func import *
|
||||
from py12306.helpers.request import Request
|
||||
@@ -26,10 +27,10 @@ class UserJob:
|
||||
self.user_name = info.get('user_name')
|
||||
self.password = info.get('password')
|
||||
self.user = user
|
||||
# load user
|
||||
self.load_user()
|
||||
|
||||
def run(self):
|
||||
# load user
|
||||
self.load_user()
|
||||
self.start()
|
||||
|
||||
def start(self):
|
||||
@@ -38,6 +39,7 @@ class UserJob:
|
||||
:return:
|
||||
"""
|
||||
while True:
|
||||
app_available_check()
|
||||
self.check_heartbeat()
|
||||
sleep(self.heartbeat_interval)
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from py12306.helpers.app import *
|
||||
from py12306.helpers.func import *
|
||||
from py12306.log.user_log import UserLog
|
||||
from py12306.user.job import UserJob
|
||||
@@ -14,21 +15,15 @@ class User:
|
||||
@classmethod
|
||||
def run(cls):
|
||||
self = cls()
|
||||
app_available_check()
|
||||
self.start()
|
||||
pass
|
||||
|
||||
def start(self):
|
||||
self.init_users()
|
||||
UserLog.print_init_users(users=self.users)
|
||||
while True:
|
||||
# 多线程维护用户
|
||||
threads = []
|
||||
for user in self.users:
|
||||
thread = threading.Thread(target=user.run)
|
||||
thread.start()
|
||||
threads.append(thread)
|
||||
# user.run()
|
||||
for thread in threads: thread.join()
|
||||
# 多线程维护用户
|
||||
create_thread_and_run(jobs=self.users, callback_name='run', wait=False)
|
||||
|
||||
def init_users(self):
|
||||
accounts = config.USER_ACCOUNTS
|
||||
|
||||
Reference in New Issue
Block a user