diff --git a/API_Usage.py b/API_Usage.py index 2916716..73d3005 100644 --- a/API_Usage.py +++ b/API_Usage.py @@ -1,7 +1,8 @@ from config import CONFIG -from api.moodle import Moodle -from api.ncnuMain import NcnuMain -from api.ncnu import NCNU +from api.moodle import MoodleAPI +from api.ncnuMain import NcnuMainAPI +from api.ncnu import NcnuAPI +from api.eventRigestry import EventRegistry def space(): print("\n" + "="*20 + "\n") @@ -9,7 +10,7 @@ def space(): # =================== TEST Moodle API ========================== # ===== Test 登入 ===== -moodle = Moodle(CONFIG['moodle']['username'], CONFIG['moodle']['password']) +moodle = MoodleAPI(CONFIG['moodle']['username'], CONFIG['moodle']['password']) if moodle.status: # ===== Test 取得該學期課程資料 ===== @@ -40,7 +41,7 @@ else: # =================== Test 暨大官網 API ========================== # ===== Test 取得暨大官網最新消息 ===== -main = NcnuMain() +main = NcnuMainAPI() for anno in main.getAnno(): print(anno) space() @@ -50,7 +51,7 @@ space() # =================== Test 暨大教務系統 API ========================== # ===== Test 登入 ===== -ncnu = NCNU(CONFIG['NCNU']['username'], CONFIG['NCNU']['password']) +ncnu = NcnuAPI(CONFIG['NCNU']['username'], CONFIG['NCNU']['password']) if ncnu.status: # ===== Test 下載課表 ===== @@ -99,3 +100,16 @@ if ncnu.status: space() else: print("NCNU 教務系統登入失敗") + + + + +# =================== Test 暨大活動報名系統 API ========================== + +# ===== Test 登入 ===== +eventReg = EventRegistry(CONFIG['NCNU']['username'], CONFIG['NCNU']['password']) +if eventReg.status: + print("登入成功") + space() +else: + print("登入失敗") diff --git a/api/eventRigestry.py b/api/eventRigestry.py new file mode 100644 index 0000000..65c418d --- /dev/null +++ b/api/eventRigestry.py @@ -0,0 +1,38 @@ +import requests +from api.tools import * + +class EventRegistry(): + def __init__(self, username, password): + ''' + initial 就登入 + 根據 self.status 判斷成功與否 + ''' + self.username = username # 學號 + self.session = requests.Session() + self.status = self.login(username, password) + + def login(self, username, password): + ''' + 登入活動報名系統 + return bool + ''' + # get login token + response = self.session.get('https://ccweb.ncnu.edu.tw/SLLL/login.asp') + loginToken = find(response, 'input', param={'name': 'token'}).get('value') + + # request login page + response = self.session.post( + "https://ccweb.ncnu.edu.tw/SLLL/login.asp", + data={ + 'token': loginToken, + 'username': username, + 'password': password, + 'type': '' + } + ) + + # 成功的話 return http 302, redirect + if len(response.history)!=0: + return True + else: + return False \ No newline at end of file diff --git a/api/moodle.py b/api/moodle.py index 5d8b098..f4104e6 100644 --- a/api/moodle.py +++ b/api/moodle.py @@ -4,7 +4,7 @@ from bs4 import BeautifulSoup from api.tools import * import json -class Moodle(): +class MoodleAPI(): def __init__(self, username, password): ''' Create a Moodle object to handle Session diff --git a/api/ncnu.py b/api/ncnu.py index 85fd2ba..5aa06ba 100644 --- a/api/ncnu.py +++ b/api/ncnu.py @@ -1,7 +1,7 @@ import requests from api.tools import * -class NCNU(): +class NcnuAPI(): def __init__(self, username, password): ''' initial 就登入 diff --git a/api/ncnuMain.py b/api/ncnuMain.py index 7b1ea10..84fb0c1 100644 --- a/api/ncnuMain.py +++ b/api/ncnuMain.py @@ -2,7 +2,7 @@ from api.tools import * import requests from bs4 import BeautifulSoup -class NcnuMain(): +class NcnuMainAPI(): def getAnno(self): response = requests.get('https://www.ncnu.edu.tw/ncnuweb/ann/tabs.aspx?homeType=ncnu&unit=ncnu') block = find(response, 'div', param={'id': 'annNews'})