add: schedule for today and tomorrow; update: showing data

This commit is contained in:
mnjx
2023-04-14 20:14:18 +02:00
parent e2e970b7ff
commit 93fe4ff01c
5 changed files with 321 additions and 62 deletions
View File
+34 -28
View File
@@ -2,16 +2,14 @@ from lib import networking as nw
from bs4 import BeautifulSoup
not_login = 0
F_not_anymore = 0
def fetch_locally() -> list:
global not_login, F_not_anymore
global not_login
if not_login == 1 and F_not_anymore == 0:
F_not_anymore = 1
if not_login == 1:
not_login = 0
print("Could not sign in (you may have entered invalid login credentials)")
print("Fetching locally\n")
#print("Could not sign in (you may have entered invalid login credentials)")
#print("Fetching grades locally\n")
grades = grades_from_file()
return grades
@@ -32,31 +30,38 @@ def get_avg(grades: list) -> str:
return avg
def get_grades(url: str):
html = nw.get_html_from(url)
try:
html = nw.get_html_from(url)
soup = BeautifulSoup(html.text, "lxml")
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
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)
grades.append(temp_dict)
return grades
return grades
except AttributeError:
global not_login
not_login = 1
grades = fetch_locally()
return grades
def show_grades():
if nw.is_connected() == True:
@@ -73,6 +78,8 @@ def show_grades():
max_len_of_subject = len(i)
max_len_of_subject += 1
print("")
print("--------")
print(" Známky ")
print("--------\n")
@@ -92,7 +99,6 @@ def show_grades():
print(get_avg(grades[i]["GRADES"]), "|", end=" ")
print("".join([grades[i]["GRADES"][j]+" " for j in range(len(grades[i]["GRADES"]))]), end=" ")
print("")
print("")
def grades_to_file(grades=None): # !!! make sure the format of the grades list is saved correctly !!!
import csv
+268
View File
@@ -0,0 +1,268 @@
# fetch the schedule for today and tomorrow from the landing page of ŠOL
from lib import networking as nw
from bs4 import BeautifulSoup
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 the schedule for today and tomorrow locally\n")
schedule_tdy_tmr = schedule_tdy_tmr_from_file()
return schedule_tdy_tmr
def get_schedule_tdy_tmr(url: str):
try:
html = nw.get_html_from(url)
soup = BeautifulSoup(html.text, "lxml")
table = soup.find("table", class_="DctTable")
rows_odd = table.find_all("tr", class_="RowOdd")
rows_even = table.find_all("tr", class_="RowEven")
rows = rows_odd + rows_even
schedule_tdy_tmr = []
for i in range(len(rows)):
if rows[i].find("td", class_="KuvSuplovanaHodina") is not None and rows[i].find("td", class_="KuvSuplujiciHodina") is None:
continue
subjects = []
wheres = []
cells = rows[i].find_all("td", attrs={"class": "DctCell"})
for j in range(len(cells)):
if cells[j].text == "":
new_span = soup.new_tag("span", attrs={'class': 'KuvBunkaRozvrhNadpis'})
new_span.string = "Volno"
cells[j].append(new_span)
if rows[i].find("td", class_="KuvHeaderNadpis") is not None:
day = rows[i].find("td", class_="KuvHeaderNadpis").text
date = rows[i].find("td", class_="KuvHeaderText").text
subjects_ = rows[i].find_all("span", class_="KuvBunkaRozvrhNadpis")
temp = []
for j in range(len(subjects_)):
temp.append(subjects_[j].text)
subjects = temp
F_con = False
try:
while subjects[-1] == "Volno":
del subjects[-1]
if subjects == []:
F_con = True
except IndexError:
pass
wheres_ = rows[i].find_all("span", class_="KuvBunkaRozvrhText")
temp = []
for j in range(len(wheres_)):
if len(subjects[j]) < 10:
temp.append(wheres_[j].text[4:])
else:
temp.append(" - ")
wheres = temp
for j in range(len(cells)):
if cells[j].get("colspan") is not None:
if int(cells[j].get("colspan")) == 2:
subjects.insert(j, subjects[j])
wheres.insert(j, wheres[j])
for j in range(len(subjects)):
if subjects[j] == "Volno":
wheres.insert(j, " - ")
if F_con == False:
schedule_tdy_tmr_row = {
"DAY": day,
"DATE": date,
"SUBJECTS": subjects,
"WHERES": wheres
}
schedule_tdy_tmr.append(schedule_tdy_tmr_row)
return schedule_tdy_tmr
except AttributeError:
global not_login
not_login = 1
schedule = fetch_locally()
return schedule
def schedule_tdy_tmr_from_file_to_list():
schedule = []
schedule_ = []
try:
with open("src/lib/data/schedule_tdy_tmr.csv") as f:
import csv
reader = csv.reader(f)
for row in reader:
temp = []
for col in row:
temp.append(col.strip())
schedule_.append(temp)
for i in range(0, len(schedule_), 2):
del schedule_[i][0]
temp = []
for i in range(1, len(schedule_), 2):
temp.append(schedule_[i][0])
del schedule_[i][0]
schedule.append(temp)
for i in range(10):
temp = []
for j in range(10):
try:
temp.append(schedule_[j][0])
del schedule_[j][0]
except IndexError:
pass
schedule.append(temp)
except FileNotFoundError:
print("No schedule_tdy_tmr.csv file found\n")
return schedule
def get_whitespace(key: str, idx=0) -> int:
schedule = schedule_tdy_tmr_from_file_to_list()
max_len = 0
if key == "DATE":
try:
max_len = len(max(schedule[0], key=len))
except ValueError:
pass
elif key == "WHERES":
try:
max_len = len(max(schedule[idx], key=len))
except ValueError:
pass
return max_len
def show_schedule_tdy_tmr():
# if there is internet access, load stuff from the web
# if not, load the schedule.csv file
if nw.is_connected() == True:
schedule = get_schedule_tdy_tmr("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
schedule_tdy_tmr_to_file(schedule)
else:
schedule = schedule_tdy_tmr_from_file()
print("")
print("--------------------------")
print(" Dnešní a zítřejší rozvrh ")
print("--------------------------\n")
if schedule == []:
print("N/A\n")
import os
if os.path.exists("src/lib/data/schedule_tdy_tmr.csv"):
os.remove("src/lib/data/schedule_tdy_tmr.csv")
return
for i in range(len(schedule)):
max_len = get_whitespace("DATE")
whitespace = " " * (max_len - len(schedule[i]["DAY"]))
print(schedule[i]["DAY"] + whitespace, end=" ")
for j in range(len(schedule[i]["SUBJECTS"])):
max_len = get_whitespace("WHERES", j+1)
whitespace = " " * (max_len - len(schedule[i]["SUBJECTS"][j]))
print("| " + schedule[i]["SUBJECTS"][j] + whitespace, end=" ")
print("")
max_len = get_whitespace("DATE")
whitespace = " " * (max_len - len(schedule[i]["DATE"]))
print(schedule[i]["DATE"] + whitespace, end=" ")
for j in range(len(schedule[i]["WHERES"])):
max_len = get_whitespace("WHERES", j+1)
whitespace = " " * (max_len - len(schedule[i]["WHERES"][j]))
print("| " + schedule[i]["WHERES"][j] + whitespace, end=" ")
print("")
def schedule_tdy_tmr_to_file(schedule=None):
import csv
if schedule is None:
try:
schedule = get_schedule_tdy_tmr("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
except Exception:
print("An error occured, you probably can't access the internet right now")
return
with open("src/lib/data/schedule_tdy_tmr.csv", mode="w", newline="") as f:
data = []
for i in range(len(schedule)):
temp = []
temp.append(schedule[i]["DAY"])
for j in range(len(schedule[i]["SUBJECTS"])):
temp.append(schedule[i]["SUBJECTS"][j])
data.append(temp)
temp = []
temp.append(schedule[i]["DATE"])
for j in range(len(schedule[i]["WHERES"])):
temp.append(schedule[i]["WHERES"][j])
data.append(temp)
writer = csv.writer(f)
for row in data:
writer.writerow(row)
def schedule_tdy_tmr_from_file() -> list:
import csv
schedule = []
try:
with open("src/lib/data/schedule_tdy_tmr.csv", mode="r") as f:
reader = csv.reader(f)
rows = []
for row in reader:
rows.append(row)
for row in range(0, len(rows), 2):
temp = {}
temp["DAY"] = rows[row][0]
try:
temp["DATE"] = rows[row+1][0]
except IndexError:
pass
subjects = []
for i in range(1, len(rows[row])):
subjects.append(rows[row][i])
temp["SUBJECTS"] = subjects
wheres = []
try:
for i in range(1, len(rows[row+1])):
wheres.append(rows[row+1][i])
except IndexError:
pass
temp["WHERES"] = wheres
schedule.append(temp)
except FileNotFoundError:
print("No schedule_tdy_tmr.csv file found\n")
return schedule
+17 -34
View File
@@ -2,16 +2,14 @@ from lib import networking as nw
from bs4 import BeautifulSoup
not_login = 0
F_not_anymore = 0
def fetch_locally() -> list:
global not_login, F_not_anymore
global not_login
if not_login == 1 and F_not_anymore == 0:
F_not_anymore = 1
if not_login == 1:
not_login = 0
print("Could not sign in (you may have entered invalid login credentials)")
print("Fetching locally\n")
#print("Could not sign in (you may have entered invalid login credentials)")
#print("Fetching the schedule for this week locally\n")
schedule_week = schedule_week_from_file()
return schedule_week
@@ -181,6 +179,8 @@ def show_schedule_week():
else:
schedule = schedule_week_from_file()
print("")
print("----------------")
print(" Týdenní rozvrh ")
print("----------------\n")
@@ -216,7 +216,6 @@ def show_schedule_week():
print("| " + schedule[i]["WHERES"][j] + whitespace, end=" ")
print("")
print("")
def schedule_week_to_file(schedule=None):
import csv
@@ -232,24 +231,16 @@ def schedule_week_to_file(schedule=None):
for i in range(len(schedule)):
temp = []
try:
temp.append(schedule[i]["DAY"])
for j in range(len(schedule[i]["SUBJECTS"])):
temp.append(schedule[i]["SUBJECTS"][j])
data.append(temp)
temp = []
temp.append(schedule[i]["DATE"])
for j in range(len(schedule[i]["WHERES"])):
temp.append(schedule[i]["WHERES"][j])
data.append(temp)
temp = []
for j in range(len(schedule[i]["WHOS"])):
temp.append(schedule[i]["WHOS"][j])
data.append(temp)
except KeyError:
pass
temp.append(schedule[i]["DAY"])
for j in range(len(schedule[i]["SUBJECTS"])):
temp.append(schedule[i]["SUBJECTS"][j])
data.append(temp)
temp = []
temp.append(schedule[i]["DATE"])
for j in range(len(schedule[i]["WHERES"])):
temp.append(schedule[i]["WHERES"][j])
data.append(temp)
writer = csv.writer(f)
for row in data:
@@ -266,7 +257,7 @@ def schedule_week_from_file() -> list:
for row in reader:
rows.append(row)
for row in range(0, len(rows), 3):
for row in range(0, len(rows), 2):
temp = {}
temp["DAY"] = rows[row][0]
@@ -289,14 +280,6 @@ def schedule_week_from_file() -> list:
pass
temp["WHERES"] = wheres
whos = []
try:
for i in range(len(rows[row+2])):
whos.append(rows[row+2][i])
except IndexError:
pass
temp["WHOS"] = whos
schedule.append(temp)
except FileNotFoundError:
print("No schedule_week.csv file found\n")
+2
View File
@@ -1,6 +1,7 @@
from lib import grades as gr
from lib import networking as nw
from lib import schedule_week as sw
from lib import schedule_tdy_tmr as st
if __name__ == "__main__":
user = str(input("Zadejte přihlašovací jméno: "))
@@ -15,3 +16,4 @@ if __name__ == "__main__":
#sw.schedule_week_to_file()
#gr.grades_to_file()
#sw.schedule_week_from_file()
st.show_schedule_tdy_tmr()