-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcore.py
More file actions
82 lines (63 loc) · 2.51 KB
/
core.py
File metadata and controls
82 lines (63 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Functions to enable interaction with MSL based SU websites."""
import datetime
import logging
from typing import TYPE_CHECKING
import aiohttp
from bs4 import BeautifulSoup
from config import settings
from utils import GLOBAL_SSL_CONTEXT
if TYPE_CHECKING:
from collections.abc import Mapping, Sequence
from http.cookies import Morsel
from logging import Logger
from typing import Final
__all__: "Sequence[str]" = ()
logger: "Final[Logger]" = logging.getLogger("TeX-Bot")
DEFAULT_TIMEZONE: "Final[datetime.timezone]" = datetime.UTC
TODAYS_DATE: "Final[datetime.datetime]" = datetime.datetime.now(tz=DEFAULT_TIMEZONE)
CURRENT_YEAR_START_DATE: "Final[datetime.datetime]" = datetime.datetime(
year=TODAYS_DATE.year if TODAYS_DATE.month >= 7 else TODAYS_DATE.year - 1,
month=7,
day=1,
tzinfo=DEFAULT_TIMEZONE,
)
CURRENT_YEAR_END_DATE: "Final[datetime.datetime]" = datetime.datetime(
year=TODAYS_DATE.year if TODAYS_DATE.month >= 7 else TODAYS_DATE.year - 1,
month=6,
day=30,
tzinfo=DEFAULT_TIMEZONE,
)
BASE_HEADERS: "Final[Mapping[str, str]]" = {
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"Expires": "0",
}
BASE_COOKIES: "Final[Mapping[str, str]]" = {
".ASPXAUTH": settings["SU_PLATFORM_ACCESS_COOKIE"],
}
ORGANISATION_ID: "Final[str]" = settings["ORGANISATION_ID"]
ORGANISATION_ADMIN_URL: "Final[str]" = (
f"https://www.guildofstudents.com/organisation/admin/{ORGANISATION_ID}/"
)
async def get_msl_context(url: str) -> tuple[dict[str, str], dict[str, str]]:
"""Get the required context headers, data and cookies to make a request to MSL."""
http_session: aiohttp.ClientSession = aiohttp.ClientSession(
headers=BASE_HEADERS,
cookies=BASE_COOKIES,
)
data_fields: dict[str, str] = {}
cookies: dict[str, str] = {}
async with http_session, http_session.get(url=url, ssl=GLOBAL_SSL_CONTEXT) as field_data:
data_response = BeautifulSoup(
markup=await field_data.text(),
features="html.parser",
)
for field in data_response.find_all(name="input"):
if field.get("name") and field.get("value"):
data_fields[field.get("name")] = field.get("value")
for cookie in field_data.cookies:
cookie_morsel: Morsel[str] | None = field_data.cookies.get(cookie)
if cookie_morsel is not None:
cookies[cookie] = cookie_morsel.value
cookies[".ASPXAUTH"] = settings["MEMBERS_LIST_AUTH_SESSION_COOKIE"]
return data_fields, cookies