159 lines
3.7 KiB
Python
Executable File
159 lines
3.7 KiB
Python
Executable File
from solenlib import networking as nw
|
|
from bs4 import BeautifulSoup
|
|
import os
|
|
|
|
not_login = 0
|
|
|
|
def fetch_locally() -> list:
|
|
global not_login
|
|
|
|
if not_login == 1:
|
|
not_login = 0
|
|
#print("Could not sign in (you may have entered invalid login credentials)")
|
|
#print("Fetching grades locally\n")
|
|
grades = grades_from_file()
|
|
return grades
|
|
|
|
|
|
def get_avg(grades: list) -> str:
|
|
if len(grades) < 1:
|
|
return ""
|
|
|
|
sum_ = 0
|
|
for i in grades:
|
|
sum_ += int(i)
|
|
avg = round(sum_ / len(grades), 2)
|
|
|
|
avg = str(avg)
|
|
if len(avg) == 3:
|
|
avg += "0"
|
|
|
|
return avg
|
|
|
|
def get_grades(url: str):
|
|
try:
|
|
html = nw.get_html_from(url)
|
|
|
|
soup = BeautifulSoup(html.text, "lxml")
|
|
|
|
rows = soup.find("table", id="G_ctl00xmainxCCADynamicUWGrid").find_all("tr")
|
|
for i in range(3):
|
|
r = soup.find("table", id="G_ctl00xmainxCCADynamicUWGrid").find("tr")
|
|
r.decompose()
|
|
|
|
grades = []
|
|
|
|
for i in range(1, len(rows)):
|
|
if rows[i].find("td") is not None:
|
|
subject = rows[i].find("td").text
|
|
temp_dict = {}
|
|
temp_dict["SUBJECT"] = subject
|
|
cell = rows[i].find_all("span", class_="VHVysl")
|
|
temp = []
|
|
for j in range(len(cell)):
|
|
temp.append(cell[j].text)
|
|
temp_dict["GRADES"] = temp
|
|
|
|
grades.append(temp_dict)
|
|
|
|
return grades
|
|
except AttributeError:
|
|
global not_login
|
|
|
|
not_login = 1
|
|
grades = fetch_locally()
|
|
return grades
|
|
|
|
def show_grades():
|
|
if nw.is_connected() == True:
|
|
grades = get_grades("/SOL/App/Hodnoceni/KZH001_HodnVypisStud.aspx")
|
|
grades_to_file(grades)
|
|
else:
|
|
grades = grades_from_file()
|
|
|
|
whitespace = ""
|
|
max_len_of_subject = 0
|
|
subjects = [grades[j]["SUBJECT"] for j in range(len(grades))]
|
|
for i in subjects:
|
|
if len(i) > max_len_of_subject:
|
|
max_len_of_subject = len(i)
|
|
max_len_of_subject += 1
|
|
|
|
print("")
|
|
|
|
print("--------")
|
|
print(" Známky ")
|
|
print("--------\n")
|
|
|
|
if grades == []:
|
|
print("N/A\n")
|
|
import os
|
|
|
|
if os.path.exists(f"{os.environ['HOME']}/.local/lib/solendata/grades.csv"):
|
|
os.remove(f"{os.environ['HOME']}/.local/lib/solendata/grades.csv")
|
|
return
|
|
|
|
for i in range(len(grades)):
|
|
if grades[i]["GRADES"] != []:
|
|
whitespace = " " * (max_len_of_subject - len(grades[i]["SUBJECT"]))
|
|
print(grades[i]["SUBJECT"] + whitespace + "|", end=" ")
|
|
print(get_avg(grades[i]["GRADES"]), "|", end=" ")
|
|
print("".join([grades[i]["GRADES"][j]+" " for j in range(len(grades[i]["GRADES"]))]), end=" ")
|
|
print("")
|
|
|
|
def grades_to_file(grades=None): # !!! make sure the format of the grades list is saved correctly !!!
|
|
import csv
|
|
if grades is None:
|
|
try:
|
|
grades = get_grades("/SOL/App/Hodnoceni/KZH001_HodnVypisStud.aspx")
|
|
except Exception:
|
|
print("An error occured, you probably can't access the internet right now")
|
|
return
|
|
|
|
data = []
|
|
|
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/grades.csv", mode="w", newline="") as f:
|
|
for i in range(len(grades)):
|
|
temp = {}
|
|
temp["SUBJECT"] = grades[i]["SUBJECT"]
|
|
temp_grades = []
|
|
for j in range(len(grades[i]["GRADES"])):
|
|
temp_grades.append(grades[i]["GRADES"][j])
|
|
temp["GRADES"] = temp_grades
|
|
data.append(temp)
|
|
|
|
writer = csv.writer(f)
|
|
for row in data:
|
|
_row = []
|
|
_row.append(row["SUBJECT"])
|
|
for i in row["GRADES"]:
|
|
_row.append(i)
|
|
writer.writerow(_row)
|
|
|
|
def grades_from_file() -> list:
|
|
import csv
|
|
grades = []
|
|
|
|
try:
|
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/grades.csv", mode="r") as f:
|
|
reader = csv.reader(f)
|
|
rows = []
|
|
for row in reader:
|
|
rows.append(row)
|
|
|
|
for row in range(len(rows)):
|
|
temp = {}
|
|
|
|
temp["SUBJECT"] = rows[row][0]
|
|
|
|
temp_lst = []
|
|
for i in range(1, len(rows[row])):
|
|
temp_lst.append(rows[row][i])
|
|
temp["GRADES"] = temp_lst
|
|
|
|
grades.append(temp)
|
|
except FileNotFoundError:
|
|
print("No grades.csv file found\n")
|
|
|
|
return grades
|