|
|
@ -1,10 +1,8 @@
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
from hashlib import md5
|
|
|
|
|
|
|
|
from random import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
from hashlib import md5
|
|
|
|
from loguru import logger
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
|
|
from .captcha import recognize
|
|
|
|
from .captcha import recognize
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -14,11 +12,15 @@ class Tree: # 任务树
|
|
|
|
self.child = []
|
|
|
|
self.child = []
|
|
|
|
|
|
|
|
|
|
|
|
def sort(self):
|
|
|
|
def sort(self):
|
|
|
|
self.child.sort(
|
|
|
|
try:
|
|
|
|
key=lambda node: node.task['displayOrder']
|
|
|
|
self.child.sort(
|
|
|
|
)
|
|
|
|
key=lambda node: node.task['displayOrder']
|
|
|
|
for ch in self.child:
|
|
|
|
)
|
|
|
|
ch.sort()
|
|
|
|
for ch in self.child:
|
|
|
|
|
|
|
|
ch.sort()
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[构建任务树] | 排序出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Spider(httpx.AsyncClient):
|
|
|
|
class Spider(httpx.AsyncClient):
|
|
|
@ -27,148 +29,186 @@ class Spider(httpx.AsyncClient):
|
|
|
|
self.is_login = False
|
|
|
|
self.is_login = False
|
|
|
|
|
|
|
|
|
|
|
|
async def login(self, username, password):
|
|
|
|
async def login(self, username, password):
|
|
|
|
if self.is_login:
|
|
|
|
try:
|
|
|
|
return {}
|
|
|
|
if self.is_login:
|
|
|
|
|
|
|
|
return {}
|
|
|
|
self.cookies.clear() # 重置 cookies
|
|
|
|
|
|
|
|
logger.info('正在获取验证码...')
|
|
|
|
self.cookies.clear() # 重置 cookies
|
|
|
|
result = await self.get(f'http://sso.ismartlearning.cn/captcha.html?{random()}')
|
|
|
|
logger.info('[登录] | 正在获取验证码...')
|
|
|
|
code = recognize(result.content)
|
|
|
|
result = await self.get('http://sso.ismartlearning.cn/captcha.html')
|
|
|
|
token = md5(password.encode()).hexdigest()
|
|
|
|
code = recognize(result.content)
|
|
|
|
info = (await self.post(
|
|
|
|
password = md5(md5(password.encode()).hexdigest().encode() + b'fa&s*l%$k!fq$k!ld@fjlk').hexdigest()
|
|
|
|
'http://sso.ismartlearning.cn/v2/tickets-v2',
|
|
|
|
logger.info('[登录] | 正在登录...')
|
|
|
|
data={
|
|
|
|
info = (await self.post(
|
|
|
|
'username': username,
|
|
|
|
'http://sso.ismartlearning.cn/v2/tickets-v2',
|
|
|
|
'password': md5(token.encode() + b'fa&s*l%$k!fq$k!ld@fjlk').hexdigest(), # 啥时候炸了就写成动态获取的
|
|
|
|
data={
|
|
|
|
'captcha': code
|
|
|
|
'username': username,
|
|
|
|
},
|
|
|
|
'password': password,
|
|
|
|
headers={
|
|
|
|
'captcha': code
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
},
|
|
|
|
'Origin': 'http://me.ismartlearning.cn',
|
|
|
|
headers={
|
|
|
|
'Referer': 'http://me.ismartlearning.cn/'
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
}
|
|
|
|
'Origin': 'http://me.ismartlearning.cn',
|
|
|
|
)).json()
|
|
|
|
'Referer': 'http://me.ismartlearning.cn/'
|
|
|
|
logger.debug(info['result'])
|
|
|
|
}
|
|
|
|
|
|
|
|
)).json()['result']
|
|
|
|
assert info['result']['code'] == -26 # 断言登录结果
|
|
|
|
logger.debug(f"[登录] | {info}")
|
|
|
|
self.is_login = True
|
|
|
|
assert info['code'] == -26 # 断言登录结果
|
|
|
|
return info['result']
|
|
|
|
self.is_login = True
|
|
|
|
|
|
|
|
logger.success('[登录] | 登录成功')
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[登录] | 登录出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
async def get_courses(self): # 获取课程列表
|
|
|
|
async def get_courses(self): # 获取课程列表
|
|
|
|
logger.info('正在获取课程列表...')
|
|
|
|
try:
|
|
|
|
courses = (await self.post(
|
|
|
|
logger.info('[获取课程列表] | 正在获取课程列表...')
|
|
|
|
'https://school.ismartlearning.cn/client/course/list-of-student?status=1',
|
|
|
|
courses = (await self.post(
|
|
|
|
data={
|
|
|
|
'https://school.ismartlearning.cn/client/course/list-of-student?status=1',
|
|
|
|
'pager.currentPage': 1,
|
|
|
|
data={
|
|
|
|
'pager.pageSize': 32767
|
|
|
|
'pager.currentPage': 1,
|
|
|
|
}
|
|
|
|
'pager.pageSize': 100
|
|
|
|
)).json()['data']
|
|
|
|
}
|
|
|
|
return courses['list']
|
|
|
|
)).json()['data']['list']
|
|
|
|
|
|
|
|
logger.debug(f"[获取课程列表] | {courses}")
|
|
|
|
|
|
|
|
logger.success('[获取课程列表] | 获取课程列表成功')
|
|
|
|
|
|
|
|
return courses
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[获取课程列表] | 获取课程列表出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
async def get_books(self, course_id): # 获取某课程的书籍列表
|
|
|
|
async def get_books(self, course_id): # 获取某课程的书籍列表
|
|
|
|
logger.info('正在获取书籍列表...')
|
|
|
|
try:
|
|
|
|
await self.post( # 必须有这个请求,否则后面会报错
|
|
|
|
logger.info('[获取书籍列表] | 正在获取书籍列表...')
|
|
|
|
'http://school.ismartlearning.cn/client/course/list-of-student?status=1',
|
|
|
|
await self.post( # 必须有这个请求,否则后面会报错
|
|
|
|
data={
|
|
|
|
'http://school.ismartlearning.cn/client/course/list-of-student?status=1',
|
|
|
|
'pager.currentPage': 1,
|
|
|
|
data={
|
|
|
|
'pager.pageSize': 32767
|
|
|
|
'pager.currentPage': 1,
|
|
|
|
}
|
|
|
|
'pager.pageSize': 100
|
|
|
|
)
|
|
|
|
}
|
|
|
|
books = (await self.post(
|
|
|
|
)
|
|
|
|
'http://school.ismartlearning.cn/client/course/textbook/list-of-student',
|
|
|
|
books = (await self.post(
|
|
|
|
data={
|
|
|
|
'http://school.ismartlearning.cn/client/course/textbook/list-of-student',
|
|
|
|
'courseId': course_id
|
|
|
|
data={
|
|
|
|
}
|
|
|
|
'courseId': course_id
|
|
|
|
)).json()['data']
|
|
|
|
}
|
|
|
|
return books
|
|
|
|
)).json()['data']
|
|
|
|
|
|
|
|
logger.success('[获取书籍列表] | 获取书籍列表成功')
|
|
|
|
|
|
|
|
return books
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[获取书籍列表] | 获取书籍列表出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
async def get_tasks(self, book_id, book_type, course_id): # 获取某书籍的任务树
|
|
|
|
async def get_tasks(self, book_id, book_type, course_id): # 获取某书籍的任务树
|
|
|
|
logger.info('正在获取任务列表...')
|
|
|
|
try:
|
|
|
|
await self.post('http://school.ismartlearning.cn/client/course/textbook/chapters')
|
|
|
|
logger.info('[获取任务列表] | 正在获取任务列表...')
|
|
|
|
tasks = (await self.post(
|
|
|
|
await self.post('http://school.ismartlearning.cn/client/course/textbook/chapters')
|
|
|
|
'http://school.ismartlearning.cn/client/course/textbook/chapters',
|
|
|
|
tasks = (await self.post(
|
|
|
|
data={
|
|
|
|
'http://school.ismartlearning.cn/client/course/textbook/chapters',
|
|
|
|
'bookId': book_id,
|
|
|
|
data={
|
|
|
|
'bookType': book_type,
|
|
|
|
'bookId': book_id,
|
|
|
|
'courseId': course_id
|
|
|
|
'bookType': book_type,
|
|
|
|
}
|
|
|
|
'courseId': course_id
|
|
|
|
)).json()['data']
|
|
|
|
}
|
|
|
|
id_record = {task['id']: Tree(task) for task in tasks}
|
|
|
|
)).json()['data']
|
|
|
|
book_name = (await self.book_info(book_id))['bookName']
|
|
|
|
id_record = {task['id']: Tree(task) for task in tasks}
|
|
|
|
root = Tree({
|
|
|
|
book_name = (await self.book_info(book_id))['bookName']
|
|
|
|
'book_id': tasks[0]['book_id'],
|
|
|
|
root = Tree({
|
|
|
|
'unitStudyPercent': 0,
|
|
|
|
'book_id': tasks[0]['book_id'],
|
|
|
|
'name': book_name
|
|
|
|
'unitStudyPercent': 0,
|
|
|
|
})
|
|
|
|
'name': book_name
|
|
|
|
for task_id in id_record:
|
|
|
|
})
|
|
|
|
node = id_record[task_id]
|
|
|
|
logger.info('[构建任务树] | 正在构建任务树...')
|
|
|
|
node_name = (f'{node.task["name"]} ' if 'name' in node.task else '') + f'[id:{node.task["id"]}]'
|
|
|
|
for task_id in id_record:
|
|
|
|
if 'parent_id' in node.task:
|
|
|
|
node = id_record[task_id]
|
|
|
|
if (parent_id := node.task['parent_id']) in id_record:
|
|
|
|
node_name = f'{node.task.get("name","")}[id:{node.task["id"]}]'
|
|
|
|
id_record[parent_id].child.append(node)
|
|
|
|
if 'parent_id' in node.task:
|
|
|
|
|
|
|
|
if node.task['parent_id'] in id_record:
|
|
|
|
|
|
|
|
id_record[node.task['parent_id']].child.append(node)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
logger.warning(f'[构建任务树] | {node_name} 父节点不存在')
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
logger.warning(f'父节点不存在:{node_name}')
|
|
|
|
root.child.append(node)
|
|
|
|
else:
|
|
|
|
root.sort()
|
|
|
|
root.child.append(node)
|
|
|
|
logger.success('[构建任务树] | 构建任务树完成')
|
|
|
|
root.sort()
|
|
|
|
logger.success('[获取任务列表] | 获取任务列表完成')
|
|
|
|
return root
|
|
|
|
return root
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[获取任务列表] | 获取任务列表出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
async def get_paper(self, paper_id): # 获取任务点信息(包括题目和答案)
|
|
|
|
async def get_paper(self, paper_id): # 获取任务点信息(包括题目和答案)
|
|
|
|
ticket = (await self.post(
|
|
|
|
try:
|
|
|
|
'http://sso.ismartlearning.cn/v1/serviceTicket',
|
|
|
|
logger.info('[获取任务点] | 正在获取任务点信息...')
|
|
|
|
data={
|
|
|
|
ticket = (await self.post(
|
|
|
|
'service': 'http://xot-api.ismartlearning.cn/client/textbook/paperinfo'
|
|
|
|
'http://sso.ismartlearning.cn/v1/serviceTicket',
|
|
|
|
}
|
|
|
|
data={
|
|
|
|
)).json()['data']['serverTicket']
|
|
|
|
'service': 'http://xot-api.ismartlearning.cn/client/textbook/paperinfo'
|
|
|
|
logger.debug(f'Ticket: {ticket}')
|
|
|
|
}
|
|
|
|
paper_info = (await self.post(
|
|
|
|
)).json()['data']['serverTicket']
|
|
|
|
'http://xot-api.ismartlearning.cn/client/textbook/paperinfo',
|
|
|
|
logger.debug(f'[获取任务点] | {ticket}')
|
|
|
|
data={
|
|
|
|
paper_info = (await self.post(
|
|
|
|
'paperId': paper_id
|
|
|
|
'http://xot-api.ismartlearning.cn/client/textbook/paperinfo',
|
|
|
|
},
|
|
|
|
params={
|
|
|
|
headers={
|
|
|
|
'ticket': ticket
|
|
|
|
'Origin': 'http://me.ismartlearning.cn',
|
|
|
|
},
|
|
|
|
'Referer': 'http://me.ismartlearning.cn/',
|
|
|
|
data={
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
'paperId': paper_id
|
|
|
|
'Accept-Encoding': 'gzip, deflate'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
headers={
|
|
|
|
params={
|
|
|
|
'Origin': 'http://me.ismartlearning.cn',
|
|
|
|
'ticket': ticket
|
|
|
|
'Referer': 'http://me.ismartlearning.cn/',
|
|
|
|
}
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
)).json()
|
|
|
|
'Accept-Encoding': 'gzip, deflate'
|
|
|
|
logger.debug(f'paper_info: {json.dumps(paper_info, indent=4)}')
|
|
|
|
}
|
|
|
|
return paper_info['data']
|
|
|
|
)).json()['data']
|
|
|
|
|
|
|
|
logger.debug(f'[获取任务点] | {json.dumps(paper_info, indent=4)}')
|
|
|
|
|
|
|
|
logger.success('[获取任务点] | 获取任务点信息完成')
|
|
|
|
|
|
|
|
return paper_info
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[获取任务点] | 获取任务点出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
async def user_info(self):
|
|
|
|
async def user_info(self):
|
|
|
|
logger.info('正在获取用户信息...')
|
|
|
|
try:
|
|
|
|
return (await self.post(
|
|
|
|
logger.info('[获取用户信息] | 正在获取用户信息...')
|
|
|
|
'https://school.ismartlearning.cn/client/user/student-info')
|
|
|
|
info = await self.post('https://school.ismartlearning.cn/client/user/student-info').json()
|
|
|
|
).json()
|
|
|
|
logger.success('[获取用户信息] | 获取用户信息完成')
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[获取用户信息] | 获取用户信息出错:{exceptionInformation}')
|
|
|
|
|
|
|
|
|
|
|
|
async def book_info(self, book_id):
|
|
|
|
async def book_info(self, book_id):
|
|
|
|
logger.info('正在获取书籍信息...')
|
|
|
|
try:
|
|
|
|
ticket = (await self.post(
|
|
|
|
logger.info('[获取书籍信息] | 正在获取书籍信息...')
|
|
|
|
'http://sso.ismartlearning.cn/v1/serviceTicket',
|
|
|
|
ticket = (await self.post(
|
|
|
|
data={
|
|
|
|
'http://sso.ismartlearning.cn/v1/serviceTicket',
|
|
|
|
'service': 'http://book-api.ismartlearning.cn/client/v2/book/info'
|
|
|
|
data={
|
|
|
|
}
|
|
|
|
'service': 'http://book-api.ismartlearning.cn/client/v2/book/info'
|
|
|
|
)).json()['data']['serverTicket']
|
|
|
|
}
|
|
|
|
logger.debug(f'Ticket: {ticket}')
|
|
|
|
)).json()['data']['serverTicket']
|
|
|
|
book_info = (await self.post(
|
|
|
|
logger.debug(f'[获取书籍信息] | {ticket}')
|
|
|
|
'http://book-api.ismartlearning.cn/client/v2/book/info',
|
|
|
|
book_info = (await self.post(
|
|
|
|
headers={
|
|
|
|
'http://book-api.ismartlearning.cn/client/v2/book/info',
|
|
|
|
'Origin': 'http://me.ismartlearning.cn',
|
|
|
|
params={
|
|
|
|
'Referer': 'http://me.ismartlearning.cn/',
|
|
|
|
'ticket': ticket
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
},
|
|
|
|
'Accept-Encoding': 'gzip, deflate'
|
|
|
|
data={
|
|
|
|
},
|
|
|
|
'bookId': book_id,
|
|
|
|
params={
|
|
|
|
'bookType': 0
|
|
|
|
'ticket': ticket
|
|
|
|
},
|
|
|
|
},
|
|
|
|
headers={
|
|
|
|
data={
|
|
|
|
'Origin': 'http://me.ismartlearning.cn',
|
|
|
|
'bookId': book_id,
|
|
|
|
'Referer': 'http://me.ismartlearning.cn/',
|
|
|
|
'bookType': 0
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
}
|
|
|
|
'Accept-Encoding': 'gzip, deflate'
|
|
|
|
)).json()
|
|
|
|
}
|
|
|
|
logger.debug(f'book_info: {json.dumps(book_info, indent=4)}')
|
|
|
|
)).json()['data']
|
|
|
|
return book_info['data']
|
|
|
|
logger.debug(f'[获取书籍信息] | {json.dumps(book_info, indent=4)}')
|
|
|
|
|
|
|
|
logger.success('[获取书籍信息] | 获取书籍信息完成')
|
|
|
|
|
|
|
|
return book_info
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
exceptionInformation = sys.exc_info()
|
|
|
|
|
|
|
|
logger.warning(f'[获取书籍信息] | 获取书籍信息出错:{exceptionInformation}')
|
|
|
|