diff --git a/api/ncnu.py b/api/ncnu.py new file mode 100644 index 0000000..eee08a8 --- /dev/null +++ b/api/ncnu.py @@ -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 + diff --git a/config.json.example b/config.json.example index 671d35f..c12f669 100644 --- a/config.json.example +++ b/config.json.example @@ -3,5 +3,9 @@ "moodle": { "username": "學號", "password": "密碼" + }, + "NCNU": { + "username": "學號", + "password": "教務系統密碼" } } \ No newline at end of file diff --git a/main.py b/main.py index 032923f..f6797ff 100644 --- a/main.py +++ b/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) \ No newline at end of file + 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 教務系統登入失敗")