加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
study.py 5.00 KB
一键复制 编辑 原始数据 按行查看 历史
陈海 提交于 2023-11-23 23:19 . 1123mod2
import json
import re
import time
import traceback
import requests
from utility import encrypt, cap_recognize
def study(username, password, ua):
# return 1:success;0:fail
text = ''
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")
capUrl = "https://m.bjyouth.net" + re.findall(
r'src="(/site/captcha.+)" alt=', touch.text)[0]
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]': capText
})
if login_r.text == '8':
print('Login:识别的验证码错误')
break
if 'fail' in login_r.text:
tryTime += 9
raise Exception('Login:账号密码错误')
# print('登录成功')
text += '登录成功\n'
r = json.loads(bjySession.get("https://m.bjyouth.net/dxx/course").text)
arr = r['data']['data']
# "rize" LOL
# print('********')
# print(r['data']['data'][2])
# print('********')
# if 'newCourse' not in r:
# print(r)
# url = r['newCourse']['url']
# title = r['newCourse']['title']
# courseId = r['newCourse']['id']
num = len(arr)
break
except:
time.sleep(3)
tryTime += 1
print(traceback.format_exc())
if not num:
print('登入失败,退出')
text += '登入失败,退出\n'
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 = '179196'
orgID = ""
try:
orgIdTemp = orgPattern.search(haveLearned['data'][0]['orgname'])
orgID = orgIdTemp.group(1)
except:
print('获取组织id-2')
text += '获取组织id-2\n'
orgIdTemp = orgPattern.search(bjySession.get('https://m.bjyouth.net/dxx/my').json()['data']['org'])
if orgIdTemp:
orgID = orgIdTemp.group(1)
if not orgID:
orgID = '179196' ## 朝阳区的orgID 需要换成自己所在区的id
print(f"无法获取orgID")
text += '无法获取orgID\n'
for i in range(num):
# print(num)
# t = r['data']['data'][i]
# print('111',t)
url = arr[i]['url']
title = arr[i]['title']
courseId = arr[i]['id']
nOrgID = int(bjySession.get('https://m.bjyouth.net/dxx/is-league').text)
if f"学习课程:《{title}》" in list(map(lambda x: x['text'], haveLearned['data'])):
print(f'{title} 在运行前已完成')
text += f'{title} 在运行前已完成'+'\n'
continue
# 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}')
text += f'Unexpected response: {r.text}\n'
continue
# return 0
haveLearned = bjySession.get(learnedInfo).json()
if int(orgID) != nOrgID:
raise Exception('组织id不匹配,如果看到这个请开个issue说下')
if f"学习课程:《{title}》" in list(map(lambda x: x['text'], haveLearned['data'])):
print(f'{title} 成功完成学习')
text += f'{title} 成功完成学习\n'
continue
# return 1
else:
print(f'完成{title}, 但未在检查中确认')
text += f'完成{title}, 但未在检查中确认\n'
continue
# return 0
return text
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化