Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token fix. Remove selenium dep #6

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bonchapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .bonchapi import BonchAPI
from .schemas import Lesson
from .__meta__ import __version__
42 changes: 24 additions & 18 deletions bonchapi/bonchapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ def __init__(self, message=None):
class BonchAPI:
@staticmethod
async def get_token() -> str:
URL = "https://lk.sut.ru/cabinet"

async with aiohttp.ClientSession() as session:
async with session.get("https://lk.sut.ru/cabinet/?") as resp:
async with session.get(URL) as resp:
resp.raise_for_status()
token = (
str(resp.cookies.get("miden"))
.split(":")[1]
Expand All @@ -26,22 +29,28 @@ async def get_token() -> str:
)
return token

async def login(self, mail: str, password: str, from_browser=False) -> bool:
URL = "https://lk.sut.ru/cabinet/lib/autentificationok.php"

token = await self.get_token()
async def login(self, mail: str, password: str) -> bool:
AUTH = f"https://lk.sut.ru/cabinet/lib/autentificationok.php?users={mail}&parole={password}"
CABINET = "https://lk.sut.ru/cabinet/"

self.token = await self.get_token()
self.mail = mail
self.password = password
self.cookies = {"miden": token}
payload = {"users": mail, "parole": password}

if from_browser:
pass
else:
async with aiohttp.ClientSession() as session:
async with session.post(URL, cookies=self.cookies, data=payload) as resp:
if await resp.text() == "1":
return True
self.cookies = {"miden": self.token}

async with aiohttp.ClientSession() as session:
async with session.get(CABINET) as resp:
resp.raise_for_status()

self.cookies = resp.cookies
self.cookies["miden"] = self.token

async with session.post(AUTH) as resp:
resp.raise_for_status()
text = await resp.text()
if text == '1':
async with session.get(CABINET) as resp:
return True
else:
raise AuthError

Expand All @@ -52,9 +61,6 @@ async def get_raw_timetable(self, week_number: int = False) -> str:

async with aiohttp.ClientSession() as session:
async with session.post(URL, cookies=self.cookies) as resp:
# print(week_number)
# print(URL)
# print(await resp.text())
bonch_acess_error_msg = "У Вас нет прав доступа. Или необходимо перезагрузить приложение.."
if await resp.text() == bonch_acess_error_msg:
await self.login(self.mail, self.password)
Expand Down
35 changes: 6 additions & 29 deletions examples/get_timetable.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import asyncio
import argparse
import os

from bonchapi import BonchAPI

from bonchapi.bonchapi import AuthError
from bonchapi.schemas import Lesson
from get_token_from_browser import Brow

import dotenv


dotenv.load_dotenv(dotenv_path="./examples/autoclick")
dotenv.load_dotenv(dotenv_path="./examples/autoclick/.env")


parser = argparse.ArgumentParser(
Expand All @@ -21,37 +18,17 @@
parser.add_argument('week_offset', nargs="?", type=int, default=0)
args = parser.parse_args()


async def main():
api = BonchAPI()

api.cookies = {}

def write_brow_token():
browser = Brow()
token = browser.get_token()
setattr(api, "cookies", {})
with open("/tmp/bonch-token", "w") as file:
file.write(token)
api.cookies["miden"] = token

def read_token_from_cache():
with open("/tmp/bonch-token", "r") as file:
token = file.readline()
api.cookies["miden"] = token

# write_brow_token()

try:
read_token_from_cache()
except FileNotFoundError:
write_brow_token()
mail = str(os.environ.get("mail"))
password = str(os.environ.get("password"))

try:
rsp = await api.get_timetable(week_offset=args.week_offset)
except AuthError:
write_brow_token()
await api.login(mail, password)


rsp = await api.get_timetable(week_offset=args.week_offset)

week: list[str] = []
Expand Down
22 changes: 0 additions & 22 deletions get-ids.py

This file was deleted.

6 changes: 0 additions & 6 deletions get-ids.sh

This file was deleted.

35 changes: 6 additions & 29 deletions get_timetable.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import asyncio
import argparse
import os

from bonchapi import BonchAPI

from bonchapi.bonchapi import AuthError
from bonchapi.schemas import Lesson
from get_token_from_browser import Brow

import dotenv


dotenv.load_dotenv(dotenv_path="./examples/autoclick")
dotenv.load_dotenv(dotenv_path="./examples/autoclick/.env")


parser = argparse.ArgumentParser(
Expand All @@ -21,37 +18,17 @@
parser.add_argument('week_offset', nargs="?", type=int, default=0)
args = parser.parse_args()


async def main():
api = BonchAPI()

api.cookies = {}

def write_brow_token():
browser = Brow()
token = browser.get_token()
setattr(api, "cookies", {})
with open("/tmp/bonch-token", "w") as file:
file.write(token)
api.cookies["miden"] = token

def read_token_from_cache():
with open("/tmp/bonch-token", "r") as file:
token = file.readline()
api.cookies["miden"] = token

# write_brow_token()

try:
read_token_from_cache()
except FileNotFoundError:
write_brow_token()
mail = str(os.environ.get("mail"))
password = str(os.environ.get("password"))

try:
rsp = await api.get_timetable(week_offset=args.week_offset)
except AuthError:
write_brow_token()
await api.login(mail, password)


rsp = await api.get_timetable(week_offset=args.week_offset)

week: list[str] = []
Expand Down
49 changes: 0 additions & 49 deletions get_token_from_browser.py

This file was deleted.

3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ Documentation = "https://github.com/KarimullinArthur/BonchAPI#readme"
Issues = "https://github.com/KarimullinArthur/BonchAPI/issues"
Source = "https://github.com/KarimullinArthur/BonchAPI"

[tool.hatch.version]
path = "bonchapi/__meta__.py"

[tool.hatch.envs.types]
extra-dependencies = [
"mypy>=1.0.0",
Expand Down