Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
feat: add logout
Browse files Browse the repository at this point in the history
  • Loading branch information
nm17 committed Feb 15, 2020
1 parent 49eccda commit ff96c48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
39 changes: 32 additions & 7 deletions netschoolapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import httpx

from netschoolapi.data import Lesson
from netschoolapi.exceptions import WrongCredentialsError, RateLimitingError, UnknownServerError
from netschoolapi.exceptions import (
WrongCredentialsError,
RateLimitingError,
UnknownServerError,
)
from netschoolapi.utils import LoginForm


Expand All @@ -30,7 +34,7 @@ async def login(self, login: str, password: str, school: str):
async with self.session:
await self.session.get(self.url)

resp = await self.session.post(self.url + "/webapi/auth/getdata", data=b' ')
resp = await self.session.post(self.url + "/webapi/auth/getdata", data=b" ")
data = resp.json()
lt, ver, salt = data["lt"], data["ver"], data["salt"]

Expand All @@ -39,7 +43,8 @@ async def login(self, login: str, password: str, school: str):

pw2 = hashlib.new(
"md5",
salt.encode() + hashlib.new("md5", password.encode()).hexdigest().encode(),
salt.encode()
+ hashlib.new("md5", password.encode()).hexdigest().encode(),
).hexdigest()
pw = pw2[: len(password)]

Expand All @@ -62,14 +67,22 @@ async def login(self, login: str, password: str, school: str):
except KeyError as err:
error_message = resp.json()["message"]
# noinspection GrazieInspection
if "Следующая попытка может быть совершена не ранее чем" in error_message:
raise RateLimitingError("Rate limited by the server. Try again later.") from err
if (
"Следующая попытка может быть совершена не ранее чем"
in error_message
):
raise RateLimitingError(
"Rate limited by the server. Try again later."
) from err
elif "Неправильный пароль" in error_message:
raise WrongCredentialsError("Incorrect credentials provided.") from err
raise WrongCredentialsError(
"Incorrect credentials provided."
) from err
else:
raise UnknownServerError("message: " + error_message)
resp = await self.session.post(
self.url + "/angular/school/studentdiary/", data={"AT": self.at, "VER": ver}
self.url + "/angular/school/studentdiary/",
data={"AT": self.at, "VER": ver},
)
self.user_id = (
int(re.search(r'userId = "(\d+)"', resp.text, re.U).group(1)) - 2
Expand Down Expand Up @@ -97,3 +110,15 @@ async def get_diary(
headers={"at": self.at},
)
return resp.json()

async def logout(self):
"""
Выходит из данной сессии
"""
async with self.session:
await self.session.post(
self.url + "/asp/logout.asp",
params={"at": self.at, "VER": int(datetime.now().timestamp()) * 100},
data={},
)
# https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="netschoolapi",
version="1.1.0",
version="1.2.0",
packages=["netschoolapi"],
url="https://github.com/nm17/netschoolapi/",
license="MIT",
Expand Down

0 comments on commit ff96c48

Please sign in to comment.