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
01cf6cca
Commit
01cf6cca
authored
Jan 14, 2019
by
Jalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化查询错误
parent
f724139d
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
44 additions
and
11 deletions
+44
-11
cluster.py
py12306/cluster/cluster.py
+12
-0
OCR.py
py12306/helpers/OCR.py
+1
-1
api.py
py12306/helpers/api.py
+0
-1
request.py
py12306/helpers/request.py
+14
-0
common_log.py
py12306/log/common_log.py
+2
-0
query_log.py
py12306/log/query_log.py
+4
-4
job.py
py12306/user/job.py
+11
-5
No files found.
py12306/cluster/cluster.py
View file @
01cf6cca
...
...
@@ -23,6 +23,7 @@ class Cluster():
KEY_CHANNEL_LOG
=
KEY_PREFIX
+
'channel_log'
KEY_CHANNEL_EVENT
=
KEY_PREFIX
+
'channel_even'
KEY_USER_COOKIES
=
KEY_PREFIX
+
'user_cookies'
KEY_USER_INFOS
=
KEY_PREFIX
+
'user_infos'
KEY_USER_LAST_HEARTBEAT
=
KEY_PREFIX
+
'user_last_heartbeat'
KEY_NODES_ALIVE
=
KEY_PREFIX
+
'nodes_alive'
...
...
@@ -253,3 +254,14 @@ class Cluster():
def
set_user_cookie
(
cls
,
key
,
value
):
self
=
cls
()
return
self
.
session
.
hset
(
Cluster
.
KEY_USER_COOKIES
,
key
,
pickle
.
dumps
(
value
,
0
)
.
decode
())
@
classmethod
def
set_user_info
(
cls
,
key
,
info
):
self
=
cls
()
return
self
.
session
.
hset
(
Cluster
.
KEY_USER_INFOS
,
key
,
pickle
.
dumps
(
info
,
0
)
.
decode
())
@
classmethod
def
get_user_info
(
cls
,
key
,
default
=
None
):
self
=
cls
()
res
=
self
.
session
.
hget
(
Cluster
.
KEY_USER_INFOS
,
key
)
return
pickle
.
loads
(
res
.
encode
())
if
res
else
default
py12306/helpers/OCR.py
View file @
01cf6cca
...
...
@@ -73,7 +73,7 @@ class OCR:
position
=
check_result
.
get
(
'res'
)
return
position
.
replace
(
'('
,
''
)
.
replace
(
')'
,
''
)
.
split
(
','
)
CommonLog
.
print_auto_code_fail
(
result
.
get
(
"Error"
,
'-'
)
)
CommonLog
.
print_auto_code_fail
(
CommonLog
.
MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE
)
return
None
...
...
py12306/helpers/api.py
View file @
01cf6cca
...
...
@@ -46,4 +46,3 @@ API_NOTIFICATION_BY_VOICE_CODE = 'http://ali-voice.showapi.com/sendVoice?'
API_FREE_CODE_QCR_API
=
'http://60.205.200.159/api'
API_FREE_CODE_QCR_API_CHECK
=
'http://check.huochepiao.360.cn/img_vcode'
py12306/helpers/request.py
View file @
01cf6cca
from
requests.exceptions
import
*
from
py12306.helpers.func
import
*
from
requests_html
import
HTMLSession
,
HTMLResponse
...
...
@@ -43,3 +45,15 @@ class Request(HTMLSession):
return
Dict
(
result
)
except
:
return
Dict
(
default
)
def
request
(
self
,
*
args
,
**
kwargs
):
# 拦截所有错误
try
:
return
super
()
.
request
(
*
args
,
**
kwargs
)
except
RequestException
as
e
:
if
e
.
response
:
response
=
e
.
response
else
:
response
=
HTMLResponse
(
HTMLSession
)
response
.
status_code
=
500
expand_class
(
response
,
'json'
,
Request
.
json
)
return
response
py12306/log/common_log.py
View file @
01cf6cca
...
...
@@ -32,6 +32,8 @@ class CommonLog(BaseLog):
MESSAGE_OUTPUT_TO_FILE_IS_UN_ENABLE
=
'请先打开配置:输出到文件'
MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE
=
'从免费打码获取结果失败'
def
__init__
(
self
):
super
()
.
__init__
()
self
.
init_data
()
...
...
py12306/log/query_log.py
View file @
01cf6cca
...
...
@@ -152,10 +152,10 @@ class QueryLog(BaseLog):
@
classmethod
def
print_job_start
(
cls
,
job_name
):
self
=
cls
()
self
.
add_log
(
'=== 正在进行第 {query_count} 次查询 {job_name} === {time}'
.
format
(
query_count
=
int
(
self
.
data
.
get
(
'query_count'
,
0
))
+
1
,
job_name
=
job_name
,
time
=
datetime
.
datetime
.
now
())
)
message
=
'=== 正在进行第 {query_count} 次查询 {job_name} === {time}'
.
format
(
query_count
=
int
(
self
.
data
.
get
(
'query_count'
,
0
))
+
1
,
job_name
=
job_name
,
time
=
datetime
.
datetime
.
now
())
self
.
add_log
(
message
)
self
.
refresh_data
()
if
is_main_thread
():
self
.
flush
(
publish
=
False
)
...
...
py12306/user/job.py
View file @
01cf6cca
...
...
@@ -69,7 +69,7 @@ class UserJob:
app_available_check
()
if
Config
()
.
is_slave
():
self
.
load_user_from_remote
()
pass
# 虽然同一个 cookie,同时请求之后会导致失效,暂时不在子节点中加载用户
pass
else
:
if
Config
()
.
is_master
()
and
not
self
.
cookie
:
self
.
load_user_from_remote
()
# 主节点加载一次 Cookie
self
.
check_heartbeat
()
...
...
@@ -203,8 +203,9 @@ class UserJob:
return
self
.
info
.
get
(
'user_name'
,
''
)
def
save_user
(
self
):
if
Config
()
.
is_cluster_enabled
():
return
self
.
cluster
.
set_user_cookie
(
self
.
key
,
self
.
session
.
cookies
)
if
Config
()
.
is_master
():
self
.
cluster
.
set_user_cookie
(
self
.
key
,
self
.
session
.
cookies
)
self
.
cluster
.
set_user_info
(
self
.
key
,
self
.
info
)
with
open
(
self
.
get_cookie_path
(),
'wb'
)
as
f
:
pickle
.
dump
(
self
.
session
.
cookies
,
f
)
...
...
@@ -257,16 +258,21 @@ class UserJob:
def
load_user_from_remote
(
self
):
cookie
=
self
.
cluster
.
get_user_cookie
(
self
.
key
)
if
not
cookie
and
Config
()
.
is_slave
():
info
=
self
.
cluster
.
get_user_info
(
self
.
key
)
if
Config
()
.
is_slave
()
and
(
not
cookie
or
not
info
):
while
True
:
# 子节点只能取
UserLog
.
add_quick_log
(
UserLog
.
MESSAGE_USER_COOKIE_NOT_FOUND_FROM_REMOTE
.
format
(
self
.
user_name
))
.
flush
()
stay_second
(
self
.
retry_time
)
return
self
.
load_user_from_remote
()
if
info
:
self
.
info
=
info
if
cookie
:
self
.
session
.
cookies
.
update
(
cookie
)
if
not
self
.
cookie
:
# 第一次加载
self
.
cookie
=
True
self
.
did_loaded_user
()
if
not
Config
()
.
is_slave
():
self
.
did_loaded_user
()
else
:
UserLog
.
print_welcome_user
(
self
)
return
True
return
False
...
...
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