1 Star 2 Fork 2

tlc123ooop / UICtimetableToICS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.py 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
tlc123ooop 提交于 2022-02-13 16:12 . input username and password
from bs4 import BeautifulSoup
from tkinter import filedialog
from datetime import datetime, timedelta
import requests
# config
last = 15 # number of weeks last a semester
remind = 15 # remind number of minutes ahead
firstDayClass = "20220214T000000" # first day of class
username = input("Username: ")
password = input("password: ")
# init
form = '%Y%m%dT%H%M%S'
startDate = datetime.strptime(firstDayClass, form)
endDate = startDate + timedelta(weeks=last, days=-1)
switch = {1: "MO", 2: "TU", 3: "WE", 4: "TH", 5: "FR", 6: "SA", 7: "SU"}
def addCourse(file, title, loca, byDay, time):
dateTimeStart = startDate + timedelta(days=byDay-1,
hours=int(time[0].split(':')[0]),
minutes=int(time[0].split(':')[1]))
dateTimeEnd = startDate + timedelta(days=byDay-1,
hours=int(time[1].split(':')[0]),
minutes=int(time[1].split(':')[1]))
dateTimeStart = datetime.strftime(dateTimeStart, form)
dateTimeEnd = datetime.strftime(dateTimeEnd, form)
file.write("BEGIN:VEVENT\nSUMMARY:" + title + "\nLOCATION:" + loca
+ "\nDTSTART:" + dateTimeStart
+ "\nDTEND:" + dateTimeEnd
+ "\nRRULE:FREQ=WEEKLY;UNTIL=" +
datetime.strftime(endDate, form)
+ "Z;INTERVAL=1;BYDAY=" + switch[byDay]
+ "\nBEGIN:VALARM\nACTION:DISPLAY\nDESCRIPTION:REMINDER\nTRIGGER:-PT"
+ str(remind) + "M\nEND:VALARM\nEND:VEVENT\n")
# aborted
# # open htmlfile
# path = filedialog.askopenfilename(title="", filetypes=[
# ("html files", "*.html"), ("html files", "*.htm")])
# htmlfile = open(path, 'r', encoding='utf-8')
# mis crawler
loginUrl= "https://mis.uic.edu.hk/mis/usr/login.sec"
targetUrl= "https://mis.uic.edu.hk/mis/student/tts/timetable.do"
session = requests.Session()
postdata = {
'j_target_url': targetUrl,
'j_username': username,
'j_password': password,
'usertype': "on",
'x': "",
'y': ""
}
header = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0",
'Referer': loginUrl
}
htmlPost = session.post(loginUrl, postdata, headers=header, verify=False)
if(htmlPost.url[:51]!=targetUrl):
print("login failed")
exit()
# create ics file
path = filedialog.asksaveasfilename(
title="", filetypes=[("ics files", ".ics")])
if (path == ""):
exit()
# write head
file = open(path.split(".")[0]+'.ics', 'w')
file.write('''BEGIN:VCALENDAR
PRODID:UIC timetable
VERSION:2.0
X-WR-TIMEZONE:Asia/Shanghai
''')
# make soup
soup = BeautifulSoup(htmlPost.text, 'html.parser').find(id="mytimetable")
tr = soup.find_all("tr")
del tr[0] # remove head of table
for row in tr:
for byDay, col in enumerate(row.find_all("td"), 1):
strings = list(col.stripped_strings) # remove space and split
if (len(strings) != 0):
addCourse(file, strings[0], strings[1],
byDay, row.th.string.split('-'))
file.write("END:VCALENDAR")
file.close()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tlc123ooop/UICtimeTableToICS.git
git@gitee.com:tlc123ooop/UICtimeTableToICS.git
tlc123ooop
UICtimeTableToICS
UICtimetableToICS
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891