-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerschool_connector.py
91 lines (80 loc) · 3.31 KB
/
powerschool_connector.py
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
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
# coding=utf-8
import os
import json
import pwinput
from powerschool import PowerSchool, PowerschoolExamRestriction, PowerschoolInvalidLoginInformation
from utils import *
from pathlib import Path
from dotenv import load_dotenv
from loguru import logger
from tqdm import tqdm
logger.remove()
logger.add(
lambda msg: tqdm.write(msg, end=""),
level="INFO",
colorize=True,
)
logger.add("powerschool_connector.log", level="TRACE", rotation="100KB")
load_dotenv()
psUsernameCache: str | None = os.getenv("PS_USERNAME")
psPasswordCache: str | None = os.getenv("PS_PASSWORD")
def requestUserInformation(disableCache=False) -> list:
if psUsernameCache and psPasswordCache and not disableCache:
logger.debug("Using cached user information")
return [psUsernameCache, psPasswordCache]
else:
if disableCache:
logger.debug("User information cache is disabled")
else:
logger.warning("No valid user information cached, please enter again")
newUsername: str = input("Enter your Powerschool Username: ").strip()
newPassword: str = pwinput.pwinput(prompt="Enter your Powerschool Password: ") # 回显*更安全
setEnvVar("PS_USERNAME", newUsername)
setEnvVar("PS_PASSWORD", newPassword)
logger.info("User information is Cached")
return [newUsername, newPassword]
logger.debug("PS Connector starting...")
MAX_ATTEMPTS = 3
powerschool: PowerSchool
for attempt in range(MAX_ATTEMPTS):
if attempt > 0: # if it is not the first attempt, then notify the user
logger.warning(
f"Login Failed (Attempt:[{attempt+1}/{MAX_ATTEMPTS}]), retrying..."
)
disableCache = True # disable cache since cache is obviously wrong
else:
disableCache = False
try:
powerschool = PowerSchool(*requestUserInformation(disableCache))
except PowerschoolExamRestriction as e:
logger.error(f"Failed to login to Powerschool because: {e}")
logger.info("Please try again later during non-exam period, or please contact your school")
except PowerschoolInvalidLoginInformation as e:
logger.error(f"Failed to login to Powerschool because: {e}")
if attempt == MAX_ATTEMPTS - 1:
setEnvVar("PS_USERNAME", "") # clear the cache .env file
setEnvVar("PS_PASSWORD", "")
logger.error("Maximum login attempts, cleaning cache and exit")
exit(2)
except Exception as e:
logger.error(f"Failed to login to Powerschool due to unknown error: {e}")
if attempt == MAX_ATTEMPTS - 1:
setEnvVar("PS_USERNAME", "") # clear the cache .env file
setEnvVar("PS_PASSWORD", "")
logger.error("Maximum login attempts, cleaning cache and exit")
exit(2)
else:
logger.success("Login to Powerschool successfully")
break
try:
scheduleJson = powerschool.getScheduleJsonContent()
except Exception as e:
logger.critical(f"Failed to get schedule: {e}")
exit(3)
with open(Path.cwd() / "schedulePS.json", "w", encoding="utf-8") as f:
f.write(json.dumps(scheduleJson, ensure_ascii=False, indent=4))
logger.success(
'Schedule is generated successfully, please check the "schedule.json" in current directory'
)
logger.success("For ICS file generation, please run main.py")