-
Notifications
You must be signed in to change notification settings - Fork 584
/
study.py
140 lines (119 loc) · 5.09 KB
/
study.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
137
138
139
140
import json
import re
import time
import traceback
import cv2
import base64
import numpy as np
import requests
from utility import encrypt
def match(capImg):
imgData = base64.b64decode(capImg.encode("utf8"))
imgArray = np.fromstring(imgData, np.uint8)
BGRImg = cv2.imdecode(imgArray, cv2.COLOR_RGB2BGR)
HSVImg = cv2.cvtColor(BGRImg, cv2.COLOR_BGR2HSV)
H,S,V=cv2.split(HSVImg)
capImgMain = H[0:191,0:340]
capImgSquare = H[197:245,5:53]
ret1, thres= cv2.threshold(V[0:191,0:340], 200, 255, cv2.THRESH_BINARY_INV)
result = cv2.matchTemplate(capImgSquare, capImgMain, cv2.TM_CCOEFF_NORMED, thres)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(result)
(startX, startY) = maxLoc
# cv2.rectangle(BGRImg,(startX,0),(startX,250),(0,255,255),2) # 显示识别结果
# cv2.imshow("img", BGRImg)
# cv2.waitKey()
# cv2.destroyAllWindows()
return startX
def study(username, password, ua):
# return 1:success;0:fail
url = ''
tryTime = 0
while tryTime < 4:
try:
bjySession = requests.session()
bjySession.timeout = 5 # set session timeout
bjySession.headers.update({"User-Agent": ua, })
touch = bjySession.get(url="https://m.bjyouth.net/site/login")
cappp = bjySession.get(url="https://m.bjyouth.net/site/cappp")
ret = cappp.json()
capImg = ret["img"].replace("data:image/png;base64,","")
capKey = ret["key"]
value = match(capImg)
verifyUrl = f"https://m.bjyouth.net/site/valcappp"
data = {"value":value, "key":capKey, "leeway":5}
verifyResult = bjySession.post(url=verifyUrl, data=data)
if verifyResult.text == 'false':
print('Login:识别的验证码错误')
continue
if "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD5uIDebA2qU746e/NVPiQSBA0Q" not in touch.text:
print("记录的公钥没有出现")
# capText = cap_recognize(bjySession.get(url=capUrl).content)
# print(f'验证码识别: {capText}')
login_r = bjySession.post('https://m.bjyouth.net/site/login',
data={
'_csrf_mobile': bjySession.cookies.get_dict()['_csrf_mobile'],
'Login[password]': encrypt(password),
'Login[username]': encrypt(username),
'Login[verifyCode]': capKey
})
if 'fail' in login_r.text:
tryTime += 9
raise Exception('Login:账号密码错误')
print('登录成功')
r = json.loads(bjySession.get("https://m.bjyouth.net/dxx/index").text)
# "rize" LOL
if 'newCourse' not in r:
print(r)
url = r['newCourse']['url']
title = r['newCourse']['title']
courseId = r['newCourse']['id']
break
except:
time.sleep(3)
tryTime += 1
print(traceback.format_exc())
if not url:
print('登入失败,退出')
return 0
orgIdTemp = ''
orgPattern = re.compile(r'[\((]\s*(\d+)\s*[)\)]') # 组织id应该是被括号包的
learnedInfo = 'https://m.bjyouth.net/dxx/my-study?page=1&limit=15&year=' + time.strftime("%Y", time.localtime())
haveLearned = bjySession.get(learnedInfo).json()
orgID = ""
try:
orgIdTemp = orgPattern.search(haveLearned['data'][0]['orgname'])
orgID = orgIdTemp.group(1)
except:
print('获取组织id-2')
orgIdTemp = orgPattern.search(bjySession.get('https://m.bjyouth.net/dxx/my').json()['data']['org'])
if orgIdTemp:
orgID = orgIdTemp.group(1)
if not orgID:
orgID = '172442'
print(f"无法获取orgID")
nOrgID = int(bjySession.get('https://m.bjyouth.net/dxx/is-league').text)
# if int(orgID) != nOrgID:
# raise Exception('组织id不匹配,如果看到这个请开个issue说下')
if f"学习课程:《{title}》" in list(map(lambda x: x['text'], haveLearned['data'])):
print(f'{title} 在运行前已完成,退出')
return 1
# pattern = re.compile(r'https://h5.cyol.com/special/daxuexi/(\w+)/m.html\?t=1&z=201')
# result = pattern.search(url)
# if not result:
# print(f'Url pattern not matched: {url}')
# return 0
#
# end_img_url = f'https://h5.cyol.com/special/daxuexi/{result.group(1)}/images/end.jpg'
study_url = f"https://m.bjyouth.net/dxx/check"
r = bjySession.post(study_url, json={"id": str(courseId), "org_id": int(nOrgID)}) # payload
if r.text:
print(f'Unexpected response: {r.text}')
return 0
haveLearned = bjySession.get(learnedInfo).json()
time.sleep(5)
if f"学习课程:《{title}》" in list(map(lambda x: x['text'], haveLearned['data'])):
print(f'{title} 成功完成学习')
return 1
else:
print(f'完成{title}, 但未在检查中确认')
return 0