feat: 教務系統登入 & 下載課表
This commit is contained in:
parent
6a07008303
commit
476bb21bb3
49
api/ncnu.py
Normal file
49
api/ncnu.py
Normal file
@ -0,0 +1,49 @@
|
||||
import requests
|
||||
from api.tools import *
|
||||
|
||||
class NCNU():
|
||||
def __init__(self, username, password):
|
||||
self.session = requests.Session()
|
||||
self.status = self.login(username, password)
|
||||
|
||||
def login(self, username, password):
|
||||
# get login token
|
||||
response = self.session.get('https://ccweb.ncnu.edu.tw/student/login.php')
|
||||
loginToken = find(response, 'input', param={'name': 'token'}).get('value')
|
||||
|
||||
response = self.session.post(
|
||||
"https://ccweb.ncnu.edu.tw/student/login.php",
|
||||
data={
|
||||
'token': loginToken,
|
||||
'modal': '0',
|
||||
'username': username,
|
||||
'password': password,
|
||||
'type': 'a'
|
||||
}
|
||||
)
|
||||
if len(response.history)!=0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def getCourseTable(self, semester):
|
||||
url = "https://ccweb.ncnu.edu.tw/student/print_semester_course_list.php"
|
||||
|
||||
# get token
|
||||
response = self.session.get(url)
|
||||
token = find(response, 'input', param={'name': 'token'}).get('value')
|
||||
response = self.session.post(
|
||||
url,
|
||||
data={
|
||||
'year': semester,
|
||||
'font': 'microsoftjhenghei',
|
||||
'token': token,
|
||||
}
|
||||
)
|
||||
if response.status_code == 200:
|
||||
with open('{}課表.pdf'.format(semester), 'wb') as fp:
|
||||
fp.write(response.content)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
@ -3,5 +3,9 @@
|
||||
"moodle": {
|
||||
"username": "學號",
|
||||
"password": "密碼"
|
||||
},
|
||||
"NCNU": {
|
||||
"username": "學號",
|
||||
"password": "教務系統密碼"
|
||||
}
|
||||
}
|
||||
13
main.py
13
main.py
@ -1,6 +1,7 @@
|
||||
from config import CONFIG
|
||||
from api.moodle import Moodle
|
||||
from api.ncnuMain import NcnuMain
|
||||
from api.ncnu import NCNU
|
||||
|
||||
def space():
|
||||
print("\n" + "="*20 + "\n")
|
||||
@ -19,7 +20,19 @@ if moodle.status:
|
||||
for anno in moodle.getAnnoInCourse('47555'):
|
||||
print(anno)
|
||||
space()
|
||||
else:
|
||||
print("Moodle 登入失敗")
|
||||
|
||||
main = NcnuMain()
|
||||
for anno in main.getAnno():
|
||||
print(anno)
|
||||
space()
|
||||
|
||||
ncnu = NCNU(CONFIG['NCNU']['username'], CONFIG['NCNU']['password'])
|
||||
if ncnu.status:
|
||||
if ncnu.getCourseTable('1092'):
|
||||
print("1092 課表已經儲存到 ./1092課表.pdf")
|
||||
else:
|
||||
print("無法存取 1092 課表")
|
||||
else:
|
||||
print("NCNU 教務系統登入失敗")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user