diff --git a/api/moodle.py b/api/moodle.py index 40a9484..427bbe4 100644 --- a/api/moodle.py +++ b/api/moodle.py @@ -1,17 +1,25 @@ import requests from bs4 import BeautifulSoup -from config import CONFIG +from api.tools import getUrlParam, findAll, find class Moodle(): def __init__(self): + ''' + Create a Moodle object to handle Session + self.session handle cookies + ''' self.session = requests.Session() - - # get login token + # get login token, token is used for self.login() response = self.session.get('https://moodle.ncnu.edu.tw/') - root = BeautifulSoup(response.text, 'html.parser') - self.loginToken = root.find('input', {'name': 'logintoken'}).get('value') + self.loginToken = find(response, 'input', {'name': 'logintoken'}).get('value') def login(self, username, password): + ''' + For login to get Moodle Cookies + self.session handle cookies automatically + + return True if Login Success + ''' response = self.session.post( 'https://moodle.ncnu.edu.tw/login/index.php?authldap_skipntlmsso=1', data={ @@ -23,32 +31,45 @@ class Moodle(): # check whether login success # if it does, it return two 303 status code and redirected to Moodle main page if len(response.history) == 2: + self.sessionKey = getUrlParam( + find(response, 'a', {'data-title': 'logout,moodle'}).get('href'), 'sesskey' + ) + print(self.sessionKey) return True else: return False def getCourses(self, semester): + ''' + Get Courses link in this semester. + Return a list including { + 'id', + 'name' + } + ''' response = self.session.get('https://moodle.ncnu.edu.tw/') - root = BeautifulSoup(response.text, 'html.parser') - courses = root.findAll('ul', {'class': 'dropdown-menu'})[1] \ - .findAll('li') + courses = findAll(response, 'ul', {'class': 'dropdown-menu'})[1] ans = [] for course in courses: if course.text.split('-')[0]==semester: ans.append({ + 'id': getUrlParam(course.find('a').get('href'), 'id'), 'name': course.text, - 'link': course.find('a').get('href') }) return ans - - - -moodle = Moodle() -if moodle.login('學號', '密碼'): - print("登入成功") - courses = moodle.getCourses("1092") - for c in courses: - print(c) -else: - print("登入失敗") - + + def getUpcomingEvents(self): + ''' + Get Upcomming Events + ''' + response = self.session.get('https://moodle.ncnu.edu.tw/') + events = findAll(response, 'div', {'class': 'event'}) + ans = [] + for event in events: + datas = event.findAll('a') + ans.append({ + 'id': datas[0].get('data-event-id'), + 'name': datas[0].text, + 'time': datas[1].text + }) + return ans diff --git a/api/tools.py b/api/tools.py new file mode 100644 index 0000000..9596c56 --- /dev/null +++ b/api/tools.py @@ -0,0 +1,21 @@ +from urllib.parse import parse_qs, urlparse +from bs4 import BeautifulSoup + +def getUrlParam(url, param): + return parse_qs( + urlparse(url).query + )[param][0] + +def findAll(response, tag, param=None): + root = BeautifulSoup(response.text, 'html.parser') + if param: + return root.findAll(tag, param) + else: + return root.findAll(tag) + +def find(response, tag, param=None): + root = BeautifulSoup(response.text, 'html.parser') + if param: + return root.find(tag, param) + else: + return root.find(tag) diff --git a/config.py b/config.py index bfb6921..cd192eb 100644 --- a/config.py +++ b/config.py @@ -1,8 +1,4 @@ import json -class CONFIG(): - @staticmethod - def get(): - with open('config.json') as fp: - data = json.load(fp) - return data \ No newline at end of file +with open('config.json') as fp: + CONFIG = json.load(fp) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..4f78984 --- /dev/null +++ b/main.py @@ -0,0 +1,17 @@ +from config import CONFIG +from api.moodle import Moodle + +moodle = Moodle() + +if moodle.login(CONFIG['moodle']['username'], CONFIG['moodle']['password']): + print("登入成功") + courses = moodle.getCourses("1092") + for c in courses: + print(c) + events = moodle.getUpcomingEvents() + for e in events: + print(e) +else: + print("登入失敗") + +