Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
py12306
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
python
py12306
Commits
ac5cc082
Commit
ac5cc082
authored
Jan 06, 2019
by
Jalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加 夜间休息限制
parent
3446bfc9
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
42 additions
and
20 deletions
+42
-20
main.py
main.py
+3
-3
app.py
py12306/helpers/app.py
+13
-0
func.py
py12306/helpers/func.py
+13
-0
common_log.py
py12306/log/common_log.py
+1
-0
query.py
py12306/query/query.py
+4
-6
job.py
py12306/user/job.py
+4
-2
user.py
py12306/user/user.py
+4
-9
No files found.
main.py
View file @
ac5cc082
...
...
@@ -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
...
...
py12306/helpers/app.py
0 → 100644
View file @
ac5cc082
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
py12306/helpers/func.py
View file @
ac5cc082
...
...
@@ -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:
py12306/log/common_log.py
View file @
ac5cc082
...
...
@@ -4,6 +4,7 @@ from py12306.helpers.func import *
@
singleton
class
CommonLog
(
BaseLog
):
MESSAGE_12306_IS_CLOSED
=
'当前时间: {} | 12306 休息时间,程序将在明天早上 6 点自动运行'
def
__init__
(
self
):
super
()
.
__init__
()
...
...
py12306/query/query.py
View file @
ac5cc082
...
...
@@ -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
()
...
...
py12306/user/job.py
View file @
ac5cc082
...
...
@@ -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
)
...
...
py12306/user/user.py
View file @
ac5cc082
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment