fix: change Login method
This commit is contained in:
parent
7acf76116d
commit
816e4034c5
@ -3,15 +3,14 @@ from bs4 import BeautifulSoup
|
|||||||
from api.tools import getUrlParam, findAll, find
|
from api.tools import getUrlParam, findAll, find
|
||||||
|
|
||||||
class Moodle():
|
class Moodle():
|
||||||
def __init__(self):
|
def __init__(self, username, password):
|
||||||
'''
|
'''
|
||||||
Create a Moodle object to handle Session
|
Create a Moodle object to handle Session
|
||||||
self.session handle cookies
|
self.session handle cookies
|
||||||
'''
|
'''
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
# get login token, token is used for self.login()
|
self.status = self.login(username, password)
|
||||||
response = self.session.get('https://moodle.ncnu.edu.tw/')
|
|
||||||
self.loginToken = find(response, 'input', {'name': 'logintoken'}).get('value')
|
|
||||||
|
|
||||||
def login(self, username, password):
|
def login(self, username, password):
|
||||||
'''
|
'''
|
||||||
@ -20,10 +19,14 @@ class Moodle():
|
|||||||
|
|
||||||
return True if Login Success
|
return True if Login Success
|
||||||
'''
|
'''
|
||||||
|
# get login token
|
||||||
|
response = self.session.get('https://moodle.ncnu.edu.tw/')
|
||||||
|
loginToken = find(response, 'input', {'name': 'logintoken'}).get('value')
|
||||||
|
|
||||||
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={
|
||||||
'logintoken': self.loginToken,
|
'logintoken': loginToken,
|
||||||
'username': username,
|
'username': username,
|
||||||
'password': password
|
'password': password
|
||||||
}
|
}
|
||||||
@ -34,7 +37,6 @@ class Moodle():
|
|||||||
self.sessionKey = getUrlParam(
|
self.sessionKey = getUrlParam(
|
||||||
find(response, 'a', {'data-title': 'logout,moodle'}).get('href'), 'sesskey'
|
find(response, 'a', {'data-title': 'logout,moodle'}).get('href'), 'sesskey'
|
||||||
)
|
)
|
||||||
print(self.sessionKey)
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|||||||
16
main.py
16
main.py
@ -1,17 +1,9 @@
|
|||||||
from config import CONFIG
|
from config import CONFIG
|
||||||
from api.moodle import Moodle
|
from api.moodle import Moodle
|
||||||
|
|
||||||
moodle = Moodle()
|
moodle = Moodle(CONFIG['moodle']['username'], CONFIG['moodle']['password'])
|
||||||
|
if moodle.status:
|
||||||
if moodle.login(CONFIG['moodle']['username'], CONFIG['moodle']['password']):
|
for c in moodle.getCourses("1092"):
|
||||||
print("登入成功")
|
|
||||||
courses = moodle.getCourses("1092")
|
|
||||||
for c in courses:
|
|
||||||
print(c)
|
print(c)
|
||||||
events = moodle.getUpcomingEvents()
|
for e in moodle.getUpcomingEvents():
|
||||||
for e in events:
|
|
||||||
print(e)
|
print(e)
|
||||||
else:
|
|
||||||
print("登入失敗")
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user