Skip to content

Commit

Permalink
Refactor Login.login() method
Browse files Browse the repository at this point in the history
  • Loading branch information
BelKed committed Feb 15, 2024
1 parent 6ecc342 commit 63dea22
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 45 deletions.
2 changes: 1 addition & 1 deletion edupage_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def login_auto(self, username: str, password: str):
password (str): Your password.
"""

Login(self).login_auto(username, password)
Login(self).login(username, password)

def get_students(self) -> Optional[list[EduStudent]]:
"""Get list of all students in your class.
Expand Down
64 changes: 20 additions & 44 deletions edupage_api/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,22 @@

class Login(Module):
def __parse_login_data(self, data):
json_string = (data.split("userhome(", 1)[1]
.rsplit(");", 2)[0]
.replace("\t", "")
.replace("\n", "")
.replace("\r", ""))
json_string = (
data.split("userhome(", 1)[1]
.rsplit(");", 2)[0]
.replace("\t", "")
.replace("\n", "")
.replace("\r", "")
)

self.edupage.data = json.loads(json_string)
self.edupage.is_logged_in = True

self.edupage.gsec_hash = data.split("ASC.gsechash=\"")[1].split("\"")[0]
self.edupage.gsec_hash = data.split('ASC.gsechash="')[1].split('"')[0]

def login_auto(self, username: str, password: str):
"""Login using https://portal.edupage.org. If this doesn't work, please use `Login.login`.
Args:
username (str): Your username.
password (str): Your password.
Raises:
BadCredentialsException: Your credentials are invalid.
"""

response = self.edupage.session.get("https://login1.edupage.org")
data = response.content.decode()
csrf_token = data.split('name="csrfauth" value="')[1].split('"')[0]

parameters = {
"csrfauth": csrf_token,
"username": username,
"password": password,
}

request_url = "https://login1.edupage.org/login/edubarLogin.php"
response = self.edupage.session.post(request_url, parameters)
data = response.content.decode()

if "bad=1" in response.url:
raise BadCredentialsException()

self.__parse_login_data(data)
self.edupage.subdomain = data.split("-->")[0].split(" ")[-1]
self.edupage.username = username

def login(self, username: str, password: str, subdomain: str):
"""Login while specifying the subdomain to log into.
def login(self, username: str, password: str, subdomain: str = "login1"):
"""Login to your school's Edupage account.
If the subdomain is not specified, the https://login1.edupage.org is used.
Args:
username (str): Your username.
Expand All @@ -64,24 +35,29 @@ def login(self, username: str, password: str, subdomain: str):

request_url = f"https://{subdomain}.edupage.org/login/index.php"

csrf_response = self.edupage.session.get(request_url).content.decode()
response = self.edupage.session.get(request_url)
data = response.content.decode()

csrf_token = csrf_response.split("name=\"csrfauth\" value=\"")[1].split("\"")[0]
csrf_token = data.split('name="csrfauth" value="')[1].split('"')[0]

parameters = {
"csrfauth": csrf_token,
"username": username,
"password": password,
"csrfauth": csrf_token
}

request_url = f"https://{subdomain}.edupage.org/login/edubarLogin.php"

response = self.edupage.session.post(request_url, parameters)
data = response.content.decode()

if "bad=1" in response.url:
raise BadCredentialsException()

self.__parse_login_data(response.content.decode())
if subdomain == "login1":
subdomain = data.split("-->")[0].split(" ")[-1]

self.__parse_login_data(data)
self.edupage.subdomain = subdomain
self.edupage.username = username

Expand Down

0 comments on commit 63dea22

Please sign in to comment.