Refactor & Feat: Add Upcomming Events
This commit is contained in:
parent
fedea086d0
commit
7acf76116d
@ -1,17 +1,25 @@
|
|||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from config import CONFIG
|
from api.tools import getUrlParam, findAll, find
|
||||||
|
|
||||||
class Moodle():
|
class Moodle():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
'''
|
||||||
|
Create a Moodle object to handle Session
|
||||||
|
self.session handle cookies
|
||||||
|
'''
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
|
# get login token, token is used for self.login()
|
||||||
# get login token
|
|
||||||
response = self.session.get('https://moodle.ncnu.edu.tw/')
|
response = self.session.get('https://moodle.ncnu.edu.tw/')
|
||||||
root = BeautifulSoup(response.text, 'html.parser')
|
self.loginToken = find(response, 'input', {'name': 'logintoken'}).get('value')
|
||||||
self.loginToken = root.find('input', {'name': 'logintoken'}).get('value')
|
|
||||||
|
|
||||||
def login(self, username, password):
|
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(
|
response = self.session.post(
|
||||||
'https://moodle.ncnu.edu.tw/login/index.php?authldap_skipntlmsso=1',
|
'https://moodle.ncnu.edu.tw/login/index.php?authldap_skipntlmsso=1',
|
||||||
data={
|
data={
|
||||||
@ -23,32 +31,45 @@ class Moodle():
|
|||||||
# check whether login success
|
# check whether login success
|
||||||
# if it does, it return two 303 status code and redirected to Moodle main page
|
# if it does, it return two 303 status code and redirected to Moodle main page
|
||||||
if len(response.history) == 2:
|
if len(response.history) == 2:
|
||||||
|
self.sessionKey = getUrlParam(
|
||||||
|
find(response, 'a', {'data-title': 'logout,moodle'}).get('href'), 'sesskey'
|
||||||
|
)
|
||||||
|
print(self.sessionKey)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def getCourses(self, semester):
|
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/')
|
response = self.session.get('https://moodle.ncnu.edu.tw/')
|
||||||
root = BeautifulSoup(response.text, 'html.parser')
|
courses = findAll(response, 'ul', {'class': 'dropdown-menu'})[1]
|
||||||
courses = root.findAll('ul', {'class': 'dropdown-menu'})[1] \
|
|
||||||
.findAll('li')
|
|
||||||
ans = []
|
ans = []
|
||||||
for course in courses:
|
for course in courses:
|
||||||
if course.text.split('-')[0]==semester:
|
if course.text.split('-')[0]==semester:
|
||||||
ans.append({
|
ans.append({
|
||||||
|
'id': getUrlParam(course.find('a').get('href'), 'id'),
|
||||||
'name': course.text,
|
'name': course.text,
|
||||||
'link': course.find('a').get('href')
|
|
||||||
})
|
})
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
def getUpcomingEvents(self):
|
||||||
|
'''
|
||||||
moodle = Moodle()
|
Get Upcomming Events
|
||||||
if moodle.login('學號', '密碼'):
|
'''
|
||||||
print("登入成功")
|
response = self.session.get('https://moodle.ncnu.edu.tw/')
|
||||||
courses = moodle.getCourses("1092")
|
events = findAll(response, 'div', {'class': 'event'})
|
||||||
for c in courses:
|
ans = []
|
||||||
print(c)
|
for event in events:
|
||||||
else:
|
datas = event.findAll('a')
|
||||||
print("登入失敗")
|
ans.append({
|
||||||
|
'id': datas[0].get('data-event-id'),
|
||||||
|
'name': datas[0].text,
|
||||||
|
'time': datas[1].text
|
||||||
|
})
|
||||||
|
return ans
|
||||||
|
|||||||
21
api/tools.py
Normal file
21
api/tools.py
Normal file
@ -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)
|
||||||
@ -1,8 +1,4 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
class CONFIG():
|
with open('config.json') as fp:
|
||||||
@staticmethod
|
CONFIG = json.load(fp)
|
||||||
def get():
|
|
||||||
with open('config.json') as fp:
|
|
||||||
data = json.load(fp)
|
|
||||||
return data
|
|
||||||
17
main.py
Normal file
17
main.py
Normal file
@ -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("登入失敗")
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user