1首先是自己学校学习通支持课表查询
这是我的课表,学习通网页版可以查到,得到课表的思路
1.我们要找到学习通课表的api接口
2.利用axios请求拿到数据
3.展示数据
2登陆学习通
我们F12打开开发者工具
切换第三周课表,可以看到
我们可以去其他浏览器去访问一下
发现需要登录,因此请求这个接口是要携带cookie
我们去访问登录接口
const axios = require("axios")
const {writeFileSync, readFileSync} = require("fs");
const sendMessage = require("./send")
const config = require("./config")
let cookie = []
let str = "", str1 = ""
//获取cookie
async function getCookie() {
const res = await axios({
method: "get",
url: "https://passport2.chaoxing.com/api/login",
params: {
name: ,//账号
pwd: "" //密码
},
withCredentials: true,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36',
'Accept-Language': ' zh-CN,zh;q=0.9',
'Accept-Encoding': ' gzip, deflate',
'X-Requested-With': 'XMLHttpRequest',
}
})
if (res.status === 200) {
for (let i of res.headers["set-cookie"]) {
cookie.push(i.slice(0, i.indexOf(";")))//处理cookie,
}
}
console.log(cookie)
}
得到的cookie
然后请求http://kb.chaoxing.com/pc/curriculum/getMyLessons?week=2接口(需要携带week,week为第几周,需要在请求中携带),我们把数据写入json文件中
async function getclass(cookie) {
try {
const {data: res} = await axios({
method: "get",
url: "https://kb.chaoxing.com/pc/curriculum/getMyLessons",
params: {
week:2
},
withCredentials: true,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9',
Cookie: cookie
},
})
console.log(res.data)
writeFileSync("chet.json", JSON.stringify(res.data.lessonArray))
k = res.data.lessonArray.sort((a, b) => {
return a.dayOfWeek - b.dayOfWeek
})
// console.log(k)
k.forEach(i => {
str += "周[" + i.dayOfWeek + "]" + "课程[" + i.name + "]" + "节次[" + i.beginNumber + "-" + (i.beginNumber + 1) + "]" + "教室[" + i.location + "]" + "任课教师[" + i.teacherName + "]" + "\n"
console.log(str += "周[" + i.dayOfWeek + "]" + "课程[" + i.name + "]" + "节次[" + i.beginNumber + "-" + (i.beginNumber + 1) + "]" + "教室[" + i.location + "]" + "任课教师[" + i.teacherName + "]" + "\n")
})
console.log(str)
} catch (e) {
}
}
好的,我们已经成功拿到数据了,我们发现返回的数据在 lessonArray中,
处理课表数据结构
async function getclass(cookie) {
try {
const {data: res} = await axios({
method: "get",
url: "https://kb.chaoxing.com/pc/curriculum/getMyLessons",
params: {
week:2
},
withCredentials: true,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9',
Cookie: cookie
},
})
console.log(res.data)
writeFileSync("chet.json", JSON.stringify(res.data.lessonArray))
k = res.data.lessonArray.sort((a, b) => {
return a.dayOfWeek - b.dayOfWeek
})
// console.log(k)
k.forEach(i => {
str += "周[" + i.dayOfWeek + "]" + "课程[" + i.name + "]" + "节次[" + i.beginNumber + "-" + (i.beginNumber + 1) + "]" + "教室[" + i.location + "]" + "任课教师[" + i.teacherName + "]" + "\n"
console.log(str += "周[" + i.dayOfWeek + "]" + "课程[" + i.name + "]" + "节次[" + i.beginNumber + "-" + (i.beginNumber + 1) + "]" + "教室[" + i.location + "]" + "任课教师[" + i.teacherName + "]" + "\n")
})
console.log(str)
} catch (e) {
}
}
然后调用发消息模块发送课表
await sendMessage.SendMessage(types, str, id)
代码已经开源在github
ehbcifnfn/qqbat: 基于开发的go-cqhttp机器人,可推送学习通课表,可聊天,查看天气,推送视频等功能 (github.com)