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
bb136d17
Commit
bb136d17
authored
Jan 08, 2019
by
Jalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加输出日志到文件
parent
33be8c92
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
35 additions
and
10 deletions
+35
-10
env.py.example
env.py.example
+4
-0
main.py
main.py
+1
-0
config.py
py12306/config.py
+4
-0
auth_code.py
py12306/helpers/auth_code.py
+2
-2
base.py
py12306/log/base.py
+16
-4
common_log.py
py12306/log/common_log.py
+6
-2
user_log.py
py12306/log/user_log.py
+1
-1
user.py
py12306/user/user.py
+1
-1
No files found.
env.py.example
View file @
bb136d17
...
...
@@ -40,6 +40,10 @@ NOTIFICATION_BY_VOICE_CODE = 1 # 开启语音验证码
NOTIFICATION_API_APP_CODE = 'your app code'
NOTIFICATION_VOICE_CODE_PHONE = 'your phone' # 接受通知的手机号
# 输出日志到文件
OUT_PUT_LOG_TO_FILE_ENABLED = 0
OUT_PUT_LOG_TO_FILE_PATH = 'runtime/12306.log' # 日志目录
# 查询任务
QUERY_JOBS = [
{
...
...
main.py
View file @
bb136d17
...
...
@@ -34,6 +34,7 @@ def test():
:return:
"""
Const
.
IS_TEST
=
True
config
.
OUT_PUT_LOG_TO_FILE_ENABLED
=
False
if
'--test-notification'
in
sys
.
argv
or
'-n'
in
sys
.
argv
:
Const
.
IS_TEST_NOTIFICATION
=
True
pass
...
...
py12306/config.py
View file @
bb136d17
...
...
@@ -20,6 +20,10 @@ AUTO_CODE_ACCOUNT = {
'user'
:
''
,
'pwd'
:
''
}
# 输出日志到文件
OUT_PUT_LOG_TO_FILE_ENABLED
=
0
OUT_PUT_LOG_TO_FILE_PATH
=
'runtime/12306.log'
SEAT_TYPES
=
{
'特等座'
:
25
,
...
...
py12306/helpers/auth_code.py
View file @
bb136d17
...
...
@@ -36,7 +36,7 @@ class AuthCode:
return
position
def
retry_get_auth_code
(
self
):
# TODO 安全次数检测
CommonLog
.
add_quick_log
(
CommonLog
.
MESSAGE_RETRY_AUTH_CODE
.
format
(
self
.
retry_time
))
CommonLog
.
add_quick_log
(
CommonLog
.
MESSAGE_RETRY_AUTH_CODE
.
format
(
self
.
retry_time
))
.
flush
()
time
.
sleep
(
self
.
retry_time
)
return
self
.
get_auth_code
(
self
.
session
)
...
...
@@ -66,7 +66,7 @@ class AuthCode:
return
True
else
:
UserLog
.
add_quick_log
(
UserLog
.
MESSAGE_CODE_AUTH_FAIL
.
format
(
result
.
get
(
'result_message'
)
,
self
.
retry_time
))
.
flush
()
UserLog
.
MESSAGE_CODE_AUTH_FAIL
.
format
(
result
.
get
(
'result_message'
)))
.
flush
()
self
.
session
.
cookies
.
clear_session_cookies
()
return
False
...
...
py12306/log/base.py
View file @
bb136d17
...
...
@@ -3,6 +3,7 @@ import sys
from
py12306.helpers.func
import
*
class
BaseLog
:
logs
=
[]
thread_logs
=
{}
...
...
@@ -23,6 +24,17 @@ class BaseLog:
@
classmethod
def
flush
(
cls
,
sep
=
'
\n
'
,
end
=
'
\n
'
,
file
=
None
,
exit
=
False
):
self
=
cls
()
logs
=
self
.
get_logs
()
# 输出到文件
if
file
==
None
and
config
.
OUT_PUT_LOG_TO_FILE_ENABLED
:
# TODO 文件无法写入友好提示
file
=
open
(
config
.
OUT_PUT_LOG_TO_FILE_PATH
,
'a'
)
if
not
file
:
file
=
None
print
(
*
logs
,
sep
=
sep
,
end
=
end
,
file
=
file
)
self
.
empty_logs
(
logs
)
if
exit
:
sys
.
exit
()
def
get_logs
(
self
):
if
self
.
quick_log
:
logs
=
self
.
quick_log
else
:
...
...
@@ -30,7 +42,9 @@ class BaseLog:
logs
=
self
.
logs
else
:
logs
=
self
.
thread_logs
.
get
(
current_thread_id
())
print
(
*
logs
,
sep
=
sep
,
end
=
end
,
file
=
file
)
return
logs
def
empty_logs
(
self
,
logs
):
if
self
.
quick_log
:
self
.
quick_log
=
[]
else
:
...
...
@@ -38,11 +52,9 @@ class BaseLog:
self
.
logs
=
[]
else
:
if
logs
:
del
self
.
thread_logs
[
current_thread_id
()]
if
exit
:
sys
.
exit
()
@
classmethod
def
add_quick_log
(
cls
,
content
=
''
):
def
add_quick_log
(
cls
,
content
=
''
):
self
=
cls
()
self
.
quick_log
.
append
(
content
)
return
self
...
...
py12306/log/common_log.py
View file @
bb136d17
...
...
@@ -37,8 +37,12 @@ class CommonLog(BaseLog):
if
Const
.
IS_TEST
:
self
.
add_quick_log
()
self
.
add_quick_log
(
'当前为测试模式,程序运行完成后自动结束'
)
if
not
Const
.
IS_TEST
and
config
.
OUT_PUT_LOG_TO_FILE_ENABLED
:
self
.
add_quick_log
()
self
.
flush
()
self
.
add_quick_log
(
'日志已输出到文件中: {}'
.
format
(
config
.
OUT_PUT_LOG_TO_FILE_PATH
))
self
.
add_quick_log
()
self
.
flush
(
file
=
False
)
return
self
@
classmethod
...
...
@@ -49,7 +53,7 @@ class CommonLog(BaseLog):
disable
=
'未开启'
self
.
add_quick_log
(
'**** 当前配置 ****'
)
self
.
add_quick_log
(
'多线程查询: {}'
.
format
(
get_true_false_text
(
config
.
QUERY_JOB_THREAD_ENABLED
,
enable
,
disable
)))
self
.
add_quick_log
(
'语音验证码: {}'
.
format
(
get_true_false_text
(
config
.
QUERY_JOB_THREAD_ENABLED
,
enable
,
disable
)))
self
.
add_quick_log
(
'语音验证码: {}'
.
format
(
get_true_false_text
(
config
.
NOTIFICATION_BY_VOICE_CODE
,
enable
,
disable
)))
self
.
add_quick_log
(
'查询间隔: {} 秒'
.
format
(
config
.
QUERY_INTERVAL
))
self
.
add_quick_log
(
'用户心跳检测间隔: {} 秒'
.
format
(
config
.
USER_HEARTBEAT_INTERVAL
))
self
.
add_quick_log
()
...
...
py12306/log/user_log.py
View file @
bb136d17
...
...
@@ -11,7 +11,7 @@ class UserLog(BaseLog):
MESSAGE_DOWNLAOD_AUTH_CODE_FAIL
=
'验证码下载失败 错误原因: {} {} 秒后重试'
MESSAGE_DOWNLAODING_THE_CODE
=
'正在下载验证码...'
MESSAGE_CODE_AUTH_FAIL
=
'验证码验证失败 错误原因: {}
{} 秒后重试
'
MESSAGE_CODE_AUTH_FAIL
=
'验证码验证失败 错误原因: {}'
MESSAGE_CODE_AUTH_SUCCESS
=
'验证码验证成功 开始登录...'
MESSAGE_LOGIN_FAIL
=
'登录失败 错误原因: {}'
MESSAGE_LOADED_USER
=
'正在尝试恢复用户: {}'
...
...
py12306/user/user.py
View file @
bb136d17
...
...
@@ -25,7 +25,7 @@ class User:
self
.
init_users
()
UserLog
.
print_init_users
(
users
=
self
.
users
)
# 多线程维护用户
create_thread_and_run
(
jobs
=
self
.
users
,
callback_name
=
'run'
,
wait
=
False
)
create_thread_and_run
(
jobs
=
self
.
users
,
callback_name
=
'run'
,
wait
=
Const
.
IS_TEST
)
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