This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from nm17/v2.0.0
V2.0.0
- Loading branch information
Showing
9 changed files
with
176 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ serialize = | |
{major}.{minor}.{patch} | ||
|
||
[bumpversion:file:setup.py] | ||
[bumpversion:file:netschoolapi/__init__.py] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from netschoolapi.login_form import LoginForm | ||
import asyncio | ||
|
||
# Тест нового /webapi/loginform , спасибо dsolmann за то что помог мне разобраться с ним. | ||
|
||
|
||
async def main(): | ||
url = "https://edu.admoblkaluga.ru:444/" | ||
|
||
lf = LoginForm(url) | ||
|
||
await lf.get_login_form( | ||
state="Калужская обл", | ||
province="Людиновский район", | ||
city="Букань, с.", | ||
func="Общеобразовательная", | ||
) | ||
|
||
assert lf.request_params == { | ||
"CID": 2, | ||
"SID": 122, | ||
"PID": 36, | ||
"CN": 2025, | ||
"SFT": 2, | ||
"SCID": 149, | ||
} | ||
print(lf.request_params) | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import trio | ||
import asyncio | ||
|
||
from netschoolapi import NetSchoolAPI | ||
|
||
|
||
async def main(): | ||
api = NetSchoolAPI("http://sgo.cit73.ru/") | ||
await api.login("Иван", "Иван555", "МАОУ многопрофильный лицей №20") | ||
await api.login("Иван", "Иван555", school="МАОУ многопрофильный лицей №20") | ||
print(await api.get_announcements()) | ||
|
||
trio.run(main) | ||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
from netschoolapi.client import NetSchoolAPI | ||
from netschoolapi.login_form import LoginForm | ||
|
||
__version__ = "1.5.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from typing import Optional | ||
|
||
import httpx | ||
import random | ||
|
||
ALL_LOGIN_KWARGS = ["country", "state", "province", "city", "func", "school"] | ||
LOGIN_FORM_QUEUE = { | ||
"countries": "CID", | ||
"states": "SID", | ||
"provinces": "PID", | ||
"cities": "CN", | ||
"funcs": "SFT", | ||
"schools": "SCID", | ||
} | ||
|
||
|
||
class LoginForm: | ||
SID: int | ||
PID: int | ||
CN: int | ||
SFT: int | ||
CID: int | ||
SCID: int | ||
ECardID = "" # TODO: Remove in later versions? | ||
|
||
def __init__(self, url: Optional[str] = None): | ||
self.__url = url | ||
self.__client = None | ||
|
||
async def get_prepare_form_data(self) -> dict: | ||
self.__client = self.__client or httpx.AsyncClient() | ||
async with self.__client as client: | ||
resp = await client.get(self.__url.rstrip("/") + "/webapi/prepareloginform") | ||
assert resp.status_code == 200 | ||
|
||
return resp.json() | ||
|
||
async def get_login_data(self, **request_params): | ||
self.__client = self.__client or httpx.AsyncClient() | ||
|
||
items = list(LOGIN_FORM_QUEUE.values()) | ||
last_name = items[ | ||
max(map(lambda a: items.index(a.upper()), request_params.keys())) | ||
].lower() | ||
request_params["cacheVer"] = random.randint(1000000, 100000000) | ||
request_params["LASTNAME"] = last_name | ||
self.__client = self.__client or httpx.AsyncClient() | ||
async with self.__client as client: | ||
resp = await client.get( | ||
self.__url.rstrip("/") + "/webapi/loginform", params=request_params | ||
) | ||
|
||
return last_name, resp.json()["items"] | ||
|
||
# noinspection PyTypeChecker | ||
@property | ||
def request_params(self): | ||
return dict(filter(lambda a: not a[0].startswith("_"), self.__dict__.items())) | ||
|
||
async def get_login_form(self, **login_kwargs): | ||
# TODO: Reorder everything and make it not look ugly without pulling in | ||
# TODO: other libs | ||
|
||
prepare_data = await self.get_prepare_form_data() | ||
|
||
item_reordered = { | ||
item["name"].strip(): item["id"] | ||
for item in prepare_data[list(LOGIN_FORM_QUEUE.keys())[0]] | ||
} | ||
|
||
first_name = list(LOGIN_FORM_QUEUE.values())[0] | ||
|
||
setattr( | ||
self, | ||
first_name, # class attribute name | ||
prepare_data[first_name.lower()] # default | ||
if login_kwargs.get(ALL_LOGIN_KWARGS[0], None) | ||
is None # use default if param is none | ||
else item_reordered[login_kwargs[ALL_LOGIN_KWARGS[0]]], | ||
) | ||
|
||
for login_arg in ALL_LOGIN_KWARGS[1:]: | ||
last_name, items = await self.get_login_data(**self.request_params) | ||
|
||
items = {item["name"].strip(): item["id"] for item in items} | ||
|
||
next_name = list(LOGIN_FORM_QUEUE.values())[ | ||
list(LOGIN_FORM_QUEUE.values()).index(last_name.upper()) + 1 | ||
] | ||
|
||
setattr( | ||
self, | ||
next_name, # class attribute name | ||
list(items.values())[0] # default | ||
if login_kwargs.get(login_arg, None) | ||
is None # use default if param is none | ||
else items[login_kwargs[login_arg]], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,8 @@ | ||
from typing import Optional | ||
import pkg_resources | ||
import netschoolapi | ||
|
||
import httpx | ||
|
||
|
||
class LoginForm: | ||
CID: int | ||
SID: int | ||
PID: int | ||
CN: int | ||
SFT: int | ||
SCID: int | ||
ECardID = "" | ||
|
||
def __init__(self, url): | ||
self.__url = url | ||
|
||
@property | ||
async def login_form_data(self) -> dict: | ||
async with httpx.AsyncClient() as client: | ||
resp = await client.get(self.__url.rstrip("/") + "/webapi/prepareloginform") | ||
assert resp.status_code == 200 | ||
|
||
return resp.json() | ||
|
||
async def get_login_data( | ||
self, | ||
school: str, | ||
country: Optional[str] = None, | ||
func: Optional[str] = None, | ||
province: Optional[str] = None, | ||
state: Optional[str] = None, | ||
city: Optional[str] = None, | ||
): | ||
# TODO: Reorder everything | ||
data = await self.login_form_data | ||
|
||
countries = {item["name"].strip(): item["id"] for item in data["countries"]} | ||
cities = {item["name"].strip(): item["id"] for item in data["cities"]} | ||
funcs = {item["name"].strip(): item["id"] for item in data["funcs"]} | ||
provinces = {item["name"].strip(): item["id"] for item in data["provinces"]} | ||
schools = {item["name"].strip(): item["id"] for item in data["schools"]} | ||
|
||
states = {item["name"].strip(): item["id"] for item in data["states"]} | ||
|
||
self.CID = data["cid"] if country is None else countries[country] | ||
self.CN = data["cn"] if city is None else cities[city] | ||
self.PID = data["pid"] if province is None else provinces[province] | ||
self.SFT = data["sft"] if func is None else funcs[func] | ||
self.SID = data["sid"] if state is None else states[state] | ||
self.SCID = data["scid"] if school is None else schools[school] | ||
def get_user_agent(): | ||
httpx_version = pkg_resources.get_distribution("httpx").version | ||
api_version = netschoolapi.__version__ | ||
return f"httpx/{httpx_version} (NetSchoolAPI/{api_version}; +https://github.com/nm17/netschoolapi)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
author_email="[email protected]", | ||
description="A fully asynchronous API wrapper for NetSchool written in Python", | ||
long_description=open("README.md").read(), | ||
install_requires=["httpx", "trio", "python-dateutil", 'dacite'], | ||
install_requires=["httpx", "python-dateutil", 'dacite'], | ||
extras_require={"tables": ["pandas"]}, | ||
python_requires=">=3.6", | ||
long_description_content_type='text/markdown', | ||
|