This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.py
136 lines (102 loc) · 4.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import datetime
import json
import re
import urllib.parse
import requests
from settings import *
request = requests.session()
today = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def to_python(json_str: str):
return json.loads(json_str)
def to_json(obj):
return json.dumps(obj, indent=4, ensure_ascii=False)
def cookie_2_python(cookie):
obj = {}
for header in cookie.split(";"):
param = header.split("=")
obj[param[0].strip()] = param[1]
return obj
def cookie_2_param(cookie_obj: dict):
param = ""
for k, v in cookie_obj.items():
param += "{k}={v}; ".format(**locals())
return param[:-2]
def decode_json_str(s):
pattern = re.compile('{.*}')
return json.loads(pattern.search(s).group())
def decode_urldecode(s):
return urllib.parse.unquote(s)
def notify(title, message):
if not CONFIG.SCKEY:
log.info("未配置SCKEY,正在跳过推送")
return
log.info("准备推送通知...")
urlencode = urllib.parse.urlencode
url = 'https://sctapi.ftqq.com/{}.send?{}&{}'.format(CONFIG.SCKEY, urlencode({'title': title}), urlencode({'desp': message}))
try:
response = to_python(requests.post(url).text)
# {"code":0,"message":"","data":{"pushid":"1111","readkey":"xxxx","error":"SUCCESS","errno":0}}
log.info('推送结果: {}'.format(response.get('data', {'error': 'no data'}).get('error', '')))
except Exception as e:
log.error('{}: {}'.format("推送异常", e))
return log.info('任务结束')
def main():
message = {
'today': today,
'ret': -1,
'checkin_score': "-1",
'mobile_checkin': "失败",
'end': ''
}
# 主要是判断是否登陆成功以及刷新cookie参数
response = request.get(url=CONFIG.AUTH_REFRESH_URL, headers=CONFIG.HEADERS).text
auth_refresh_obj = decode_json_str(response)
if (auth_refresh_obj.get('errcode', 9999) != 0) or (not auth_refresh_obj.get('nick', None)):
log.error("刷新cookie参数失败, {msg}".format(**auth_refresh_obj))
message.update({
'ret': auth_refresh_obj.get('errcode', -1),
'nick': decode_urldecode(auth_refresh_obj.get('nick', "刷新Cookie参数失败, 未获取到用户信息")),
})
log.error("签到失败", CONFIG.MESSGAE_TEMPLATE.format(**message))
notify("腾讯视频 签到失败", CONFIG.MESSGAE_TEMPLATE.format(**message))
exit(-1)
old_cookie_obj = cookie_2_python(CONFIG.HEADERS['Cookie'])
need_update_fields = {
'vuserid': 'vqq_vuserid',
'vusession': 'vqq_vusession',
'access_token': 'vqq_access_token'
}
log.info("更新Cookie参数")
# 更新Cookie参数
for k, v in need_update_fields.items():
old_cookie_obj[v] = auth_refresh_obj[k]
# 使用更新过的Cookie参数替换CONFIG.HEADERS中的Cookie参数
CONFIG.HEADERS.update({
'Cookie': cookie_2_param(old_cookie_obj),
'Referer': 'https://m.v.qq.com'
})
log.info("更新Cookie参数成功, 开始签到")
# QZOutputJson=({ "ret": 0,"checkin_score": 0,"msg":"OK"});
sign_response = request.get(url=CONFIG.SIGN_URL, headers=CONFIG.HEADERS).text
sign_obj = decode_json_str(sign_response)
message.update({
'ret': sign_obj['ret'],
'nick': decode_urldecode(auth_refresh_obj['nick']),
'message': sign_obj['msg'],
'checkin_score': sign_obj.get('checkin_score', 0) or "👀 今日已签到了哦"
})
# TODO 手机签到失败不会重置任务状态
m_checkin_response = request.get(url=CONFIG.MOBILE_CHECKIN, headers=CONFIG.HEADERS).text
if "page_signin_detail" in m_checkin_response:
message.update({'mobile_checkin': "成功"})
log.info("签到成功 {}".format(CONFIG.MESSGAE_TEMPLATE.format(**message)))
notify("腾讯视频 签到成功", CONFIG.MESSGAE_TEMPLATE.format(**message))
if __name__ == '__main__':
try:
main()
except Exception as e:
notify("腾讯视频 签到失败", {
"msg": "请前往执行日志查看详情",
"err": str(e)
})
raise e