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