基本思路
- 课表几乎不会变动,一次爬取可以用一学期
- 教务系统爬下来的课表直接就是json格式,容易处理
- 平时看课表还得出去专门开个软件,远没有在QQ里搞个机器人方便
- 恰好有这方面的知识
效果展示
效果展示
代码简析
创建全局变量,方便别人直接使用
global furl
global key
#配置文件的地址
furl="/root/bot/lolbot/lolbot/src/plugins/nonebot_plugin_kebiao/qqinfo.json"
#和风天气的个人开发者key
key="4e7fe30df877*******************"
课表信息很多,只取需要的一小部分
#初步处理课表
def get_kb(fileurl):
original_content=open(fileurl,"r",encoding='UTF-8')
original_content=json.load(original_content)
sjklist=original_content["sjkList"]
sjk=[]
for i in sjklist:
sjk.append([i["qsjsz"],i["kcmc"]])
kblist=original_content["kbList"]
kb=[]
for i in kblist:
kb.append([i["zcd"],i["xqj"],i["xqjmc"],i["jc"],i["kcmc"],i["cdmc"],i["xm"]])
kkb=[]
for i in kb:
kkb.append(i)
for i in sjk:
kkb.append(i)
return kkb
#处理后单个课程信息:['1-6周', '6', '星期六', '1-2节', '电力系统分析', '莲32号教学楼501', '邵锐']
处理的周数是1-11周的格式,有的还有单双周,主要对周数进行处理,存到列表里,方便判断周数
#再次处理课表
def handleweeknum(kb):
afterhand=[]
for i in kb:
weeklist=[]
a=i[0].split(",")
k=0
#单双周处理
for j in a:
if "(单)" in j:
j=j.replace("(单)","")
k=1
if "(双)" in j:
j=j.replace("(双)","")
k=2
j=j[:-1]
if "-" in j:
b=j.split("-")
for m in range(int(b[0]),int(b[1])+1):
if k==0:
weeklist.append(m)
if k==1:
if m%2==1:
weeklist.append(m)
if k==2:
if m%2==0:
weeklist.append(m)
else:
weeklist.append(int(j))
after=[]
after.append(weeklist)
for n in i[1:]:
after.append(n)
afterhand.append(after)
return afterhand
周数处理后单课信息:[[1, 2, 3, 4, 5, 6], '6', '星期六', '1-2节', '电力 系统分析', '莲32号教学楼501', '邵锐']
有现成的获取某一天是今年的第几周的周几的库,直接拿来用
算出开学日是第几周,然后让现在是第几周,相减可以得到开学了几周
#获取今天周数和是周几
def gettime_today(startweeknum):
tdweeknum=datetime.datetime.now().isocalendar()[1]
nowweeknum=tdweeknum-startweeknum+1
weekdaynum=datetime.datetime.today().weekday()+1
return nowweeknum,weekdaynum
#获取明天周数和是周几
#写的有点臃肿了,但是就这样吧
def gettime_tom(startweeknum):
tomweeknum=(datetime.datetime.now()+datetime.timedelta(days=1)).isocalendar()[1]
tomweeknum=tomweeknum-startweeknum+1
tomweekdaynum=(datetime.datetime.today()+datetime.timedelta(days=1)).weekday()+1
return tomweeknum,tomweekdaynum
对课表的美化,加入了今日明日的天气直接嵌入课表中,用emoji表示
#根据城市信息返回对应的emoji
def getwea_toady_and_tom(city):
weapi="https://devapi.qweather.com/v7/weather/3d?"
idapi="https://geoapi.qweather.com/v2/city/lookup?"
city=ast.literal_eval(requests.get(idapi+"key="+key+"&"+"location="+city).text)["location"][0]["id"]
wea=ast.literal_eval(requests.get(weapi+"key="+key+"&"+"location="+city).text)
todaywea=wea["daily"][0]["textDay"]
tomwea=wea["daily"][1]["textDay"]
weathertab={
"雨":"🌧",
"雪":"❄",
"晴":"☀",
"云":"☁",
"阴":"⛅"
}
todw="🌟"
tomw="🌟"
for i in weathertab:
if i in todaywea:
todw=weathertab[i]
if i in tomwea:
tomw=weathertab[i]
return todw,tomw
核心所在,处理课表信息在QQ中的格式
但是写的有点凌乱和稀烂
#处理发出去的课表信息
def judge(weeknum,weekday,handkb,citywea="🌟"):
daymap={1:"⏳星期一⏳",2:"⏳星期二⏳",3:"⏳星期三⏳",4:"⏳星期四⏳",5:"⏳星期五⏳",6:"⏳星期六⏳",7:"⏳星期日⏳"}
if weekday in daymap:
msg=daymap[weekday]+"\n-------------------------------------\n"
for i in handkb:
timetab={
"1":"🕗",
"2":"🕘",
"3":"🕙",
"4":"🕚",
"5":"🕝",
"6":"🕞",
"7":"🕟",
"8":"🕠",
"9":"🕢",
"10":"🕣",
"11":"🕘",
"12":"🕤",
}
#根据第几节返回对应的emoji
try:
if i[3][0] in timetab:
kbtime=timetab[i[3][0]]
else:
kbtime="🕘"
if weeknum in i[0] and weekday==int(i[1]):
msg=msg+kbtime+i[3]+citywea+i[4]+"\n\n"+"🏠"+i[5]+"🏠\n🚶"+i[6]+"🚶\n-------------------------------------\n"
except:
if weeknum in i[0]:
if msg[-1] !=":":
msg=msg+"🕹实践课:"
if msg[-1] ==":":
msg=msg+i[1]+" "
return msg
接下来是Nonebot2的一些处理,不再进行说明
#各个监听器
kebiao = on_command("kebiao", aliases={"今日课表","课表","课程表"}, priority=5)
tomkebiao = on_command("tomkebiao", aliases={"明日课表"}, priority=5)
weekkebiao = on_command("weekkebiao", aliases={"本周课表"}, priority=5)
nextweekkebiao = on_command("nextweekkebiao", aliases={"下周课表"}, priority=5)
kebiaohelp = on_command("kebiaohelp", aliases={"课表帮助"}, priority=5)
#课表帮助
@kebiaohelp.handle()
async def send_kebiaohelp(matcher: Matcher, args: Message = CommandArg()):
msg="课表,今日课表\n明日课表\n本周课表\n下周课表"
await kebiaohelp.finish(msg)
#今日课表
@kebiao.handle()
async def send_today_kb(event: Event,matcher: Matcher, args: Message = CommandArg()):
try:
qqinfo=str(event)
qq=re.split(' from |@| ',qqinfo)[3]
qqtoinfo=json.load(open(furl,"r",encoding="utf-8"))
y=qqtoinfo[qq]["starttime"][0]
m=qqtoinfo[qq]["starttime"][1]
d=qqtoinfo[qq]["starttime"][2]
try:
city=qqtoinfo[qq]["city"]
except:
city="☀"
todaywea=getwea_toady_and_tom(city=city)[0]
a=judge(gettime_today(startweeknum(y,m,d))[0],gettime_today(startweeknum(y,m,d))[1],handleweeknum(get_kb(qqtoinfo[qq]["kbinfo"])),todaywea)
msg="今日课表📝📝📝:\n\n"+a
await kebiao.finish(msg)
except:
print("未绑定课表信息")
如何使用
前置条件
- 假定你有一定的基础
- 假定你会搭建nonebot2机器人,不会可以百度到官方文档看教程
- 假定你会往机器人里扔插件
- 假定你会一点点抓包
简易抓课表
- 进入你的正方教务课表信息页
- 按F12进入开发者页面
抓课表图1
- 再点一次查询抓一下课表
- 找到课表信息,直接复制
抓课表图2
- 新建一个kebiao.json文件, 把刚刚复制的课表信息粘贴进去
- 配置好在py文件的最开始的两个全局变量,和风天气API可以免费申请
- 在qqinfo.json文件里填入你的相关信息,支持多人
- 扔进去插件运行机器人
- 配置结束,可以食用
附件 - 源代码示例
file.zip
(6.06 KB, 下载次数: 53)