fix: general bugfixes, display errors

This commit is contained in:
mnjx
2023-04-23 20:05:21 +02:00
parent 93fe4ff01c
commit d9c1b6fc92
6 changed files with 532 additions and 453 deletions
+1
View File
@@ -0,0 +1 @@
1
1
+1
View File
@@ -0,0 +1 @@
1
1
+1
View File
@@ -0,0 +1 @@
1
1
+241 -204
View File
@@ -1,268 +1,305 @@
# fetch the schedule for today and tomorrow from the landing page of ŠOL
from lib import networking as nw from lib import networking as nw
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
not_login = 0 not_login = 0
def fetch_locally() -> list: def fetch_locally() -> list:
global not_login global not_login
if not_login == 1: if not_login == 1:
not_login = 0 not_login = 0
#print("Could not sign in (you may have entered invalid login credentials)") #print("Could not sign in (you may have entered invalid login credentials)")
#print("Fetching the schedule for today and tomorrow locally\n") #print("Fetching the schedule for this week locally\n")
schedule_tdy_tmr = schedule_tdy_tmr_from_file() schedule_tdy_tmr = schedule_tdy_tmr_from_file()
return schedule_tdy_tmr return schedule_tdy_tmr
def get_schedule_tdy_tmr(url: str): def get_schedule_tdy_tmr(url: str):
try: try:
html = nw.get_html_from(url) html = nw.get_html_from(url)
soup = BeautifulSoup(html.text, "lxml") soup = BeautifulSoup(html.text, "lxml")
table = soup.find("table", class_="DctTable") table = soup.find("table", id="ctl00_main_boxKalendar_ctl00_ctl04")
rows_odd = table.find_all("tr", class_="RowOdd") rows_odd = table.find_all("tr", class_="RowOdd")
rows_even = table.find_all("tr", class_="RowEven") rows_even = table.find_all("tr", class_="RowEven")
rows = rows_odd + rows_even rows = rows_odd + rows_even
schedule_tdy_tmr = [] schedule_tdy_tmr = []
for i in range(len(rows)): 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: if rows[i].find("td", class_="KuvSuplovanaHodina") is not None and rows[i].find("td", class_="KuvSuplujiciHodina") is None and rows[i].find("td", class_="KuvSkolniAkceHodina") is None and rows[i].find("td", class_="DctInnerTableType10DataTD") is None:
continue continue
subjects = [] subjects = []
wheres = [] wheres = []
cells = rows[i].find_all("td", attrs={"class": "DctCell"}) 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: header_ = table.find("tr").find_all("td", class_="KuvHeaderNadpis")
day = rows[i].find("td", class_="KuvHeaderNadpis").text header = []
for val in header_:
header.append(val.text)
date = rows[i].find("td", class_="KuvHeaderText").text for j in range(len(cells)):
if cells[j].text == "" and header[j] != " - ":
new_span = soup.new_tag("span", attrs={'class': 'KuvBunkaRozvrhNadpis'})
new_span.string = "Volno"
cells[j].append(new_span)
subjects_ = rows[i].find_all("span", class_="KuvBunkaRozvrhNadpis") if rows[i].find("td", class_="KuvHeaderNadpis") is not None:
temp = [] day = rows[i].find("td", class_="KuvHeaderNadpis").text
for j in range(len(subjects_)):
temp.append(subjects_[j].text)
subjects = temp
F_con = False date = rows[i].find("td", class_="KuvHeaderText").text
try:
while subjects[-1] == "Volno":
del subjects[-1]
if subjects == []:
F_con = True
except IndexError:
pass
wheres_ = rows[i].find_all("span", class_="KuvBunkaRozvrhText") subjects_ = rows[i].find_all("span", class_="KuvBunkaRozvrhNadpis")
temp = [] temp = []
for j in range(len(wheres_)): for j in range(len(subjects_)):
if len(subjects[j]) < 10: if "KuvSuplovanaHodina" not in subjects_[j].parent["class"]:
temp.append(wheres_[j].text[4:]) temp.append(subjects_[j].text)
else: else:
temp.append(" - ") temp.append("Volno")
wheres = temp subjects = temp
for j in range(len(cells)): F_continue = False
if cells[j].get("colspan") is not None: try:
if int(cells[j].get("colspan")) == 2: while subjects[-1] == "Volno":
subjects.insert(j, subjects[j]) del subjects[-1]
wheres.insert(j, wheres[j]) if subjects == []:
for j in range(len(subjects)): F_continue = True
if subjects[j] == "Volno": except IndexError:
wheres.insert(j, " - ") pass
if F_con == False: if subjects == []:
schedule_tdy_tmr_row = { F_continue = True
"DAY": day,
"DATE": date,
"SUBJECTS": subjects,
"WHERES": wheres
}
schedule_tdy_tmr.append(schedule_tdy_tmr_row) wheres_ = rows[i].find_all("span", class_="KuvBunkaRozvrhText")
temp = []
for j in range(len(wheres_)):
if len(subjects[j]) < 10:
if len(wheres_[j].text[4:]) > 10:
temp.append(" - ")
elif "KuvSuplovanaHodina" not in wheres_[j].parent["class"]:
temp.append(wheres_[j].text[4:])
else:
temp.append(" - ")
wheres = temp
return schedule_tdy_tmr for j in range(len(cells)):
except AttributeError: if cells[j].get("colspan") is not None:
global not_login if int(cells[j].get("colspan")) == 2:
try:
subjects.insert(j, subjects[j])
wheres.insert(j, wheres[j])
except IndexError:
pass
for j in range(len(subjects)):
if subjects[j] == "Volno":
wheres.insert(j, " - ")
not_login = 1 while True:
schedule = fetch_locally() if len(subjects) > len(wheres):
return schedule del subjects[-1]
elif len(subjects) < len(wheres):
del wheres[-1]
else:
break
if F_continue == 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(): def schedule_tdy_tmr_from_file_to_list():
schedule = [] schedule = []
schedule_ = [] schedule_ = []
try: try:
with open("src/lib/data/schedule_tdy_tmr.csv") as f: with open("src/lib/data/schedule_tdy_tmr.csv") as f:
import csv import csv
reader = csv.reader(f) reader = csv.reader(f)
for row in reader: for row in reader:
temp = [] temp = []
for col in row: for col in row:
temp.append(col.strip()) temp.append(col.strip())
schedule_.append(temp) schedule_.append(temp)
for i in range(0, len(schedule_), 2): for i in range(0, len(schedule_), 2):
del schedule_[i][0] del schedule_[i][0]
temp = [] temp = []
for i in range(1, len(schedule_), 2): for i in range(1, len(schedule_), 2):
temp.append(schedule_[i][0]) temp.append(schedule_[i][0])
del schedule_[i][0] del schedule_[i][0]
schedule.append(temp) schedule.append(temp)
for i in range(10): for i in range(10):
temp = [] temp = []
for j in range(10): for j in range(10):
try: try:
temp.append(schedule_[j][0]) temp.append(schedule_[j][0])
del schedule_[j][0] del schedule_[j][0]
except IndexError: except IndexError:
pass pass
schedule.append(temp) schedule.append(temp)
except FileNotFoundError: except FileNotFoundError:
print("No schedule_tdy_tmr.csv file found\n") print("No schedule_tdy_tmr.csv file found\n")
return schedule return schedule
def get_whitespace(key: str, idx=0) -> int: def get_whitespace(key: str, idx=0) -> int:
schedule = schedule_tdy_tmr_from_file_to_list() schedule = schedule_tdy_tmr_from_file_to_list()
max_len = 0 max_len = 0
if key == "DATE": if key == "DATE":
try: try:
max_len = len(max(schedule[0], key=len)) max_len = len(max(schedule[0], key=len))
except ValueError: except ValueError:
pass pass
elif key == "WHERES": elif key == "WHERES":
try: try:
max_len = len(max(schedule[idx], key=len)) max_len = len(max(schedule[idx], key=len))
except ValueError: except ValueError:
pass pass
return max_len return max_len
def show_schedule_tdy_tmr(): def show_schedule_tdy_tmr():
# if there is internet access, load stuff from the web # if there is internet access, load stuff from the web
# if not, load the schedule.csv file # if not, load the schedule.csv file
if nw.is_connected() == True: if nw.is_connected() == True:
schedule = get_schedule_tdy_tmr("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx") schedule = get_schedule_tdy_tmr("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
schedule_tdy_tmr_to_file(schedule) schedule_tdy_tmr_to_file(schedule)
else: else:
schedule = schedule_tdy_tmr_from_file() schedule = schedule_tdy_tmr_from_file()
print("") print("")
print("--------------------------") print("--------------------------")
print(" Dnešní a zítřejší rozvrh ") print(" Dnesni a zitrejsi rozvrh ")
print("--------------------------\n") print("--------------------------\n")
if schedule == []: if schedule == []:
print("N/A\n") print("N/A\n")
import os import os
if os.path.exists("src/lib/data/schedule_tdy_tmr.csv"): if os.path.exists("src/lib/data/schedule_tdy_tmr.csv"):
os.remove("src/lib/data/schedule_tdy_tmr.csv") os.remove("src/lib/data/schedule_tdy_tmr.csv")
return return
for i in range(len(schedule)): for i in range(len(schedule)):
max_len = get_whitespace("DATE") max_len = get_whitespace("DATE")
whitespace = " " * (max_len - len(schedule[i]["DAY"])) whitespace = " " * (max_len - len(schedule[i]["DAY"]))
print(schedule[i]["DAY"] + whitespace, end=" ") print(schedule[i]["DAY"] + whitespace, end=" ")
for j in range(len(schedule[i]["SUBJECTS"])): for j in range(len(schedule[i]["SUBJECTS"])):
max_len = get_whitespace("WHERES", j+1) max_len = get_whitespace("WHERES", j+1)
whitespace = " " * (max_len - len(schedule[i]["SUBJECTS"][j])) whitespace = " " * (max_len - len(schedule[i]["SUBJECTS"][j]))
print("| " + schedule[i]["SUBJECTS"][j] + whitespace, end=" ") print("| " + schedule[i]["SUBJECTS"][j] + whitespace, end=" ")
print("") print("")
max_len = get_whitespace("DATE") max_len = get_whitespace("DATE")
whitespace = " " * (max_len - len(schedule[i]["DATE"])) whitespace = " " * (max_len - len(schedule[i]["DATE"]))
print(schedule[i]["DATE"] + whitespace, end=" ") print(schedule[i]["DATE"] + whitespace, end=" ")
for j in range(len(schedule[i]["WHERES"])): for j in range(len(schedule[i]["WHERES"])):
max_len = get_whitespace("WHERES", j+1) max_len = get_whitespace("WHERES", j+1)
whitespace = " " * (max_len - len(schedule[i]["WHERES"][j])) whitespace = " " * (max_len - len(schedule[i]["WHERES"][j]))
print("| " + schedule[i]["WHERES"][j] + whitespace, end=" ") print("| " + schedule[i]["WHERES"][j] + whitespace, end=" ")
print("")
print("")
def schedule_tdy_tmr_to_file(schedule=None): def schedule_tdy_tmr_to_file(schedule=None):
import csv import csv
if schedule is None: if schedule is None:
try: try:
schedule = get_schedule_tdy_tmr("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx") schedule = get_schedule_tdy_tmr("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
except Exception: except Exception:
print("An error occured, you probably can't access the internet right now") print("An error occured, you probably can't access the internet right now")
return return
with open("src/lib/data/schedule_tdy_tmr.csv", mode="w", newline="") as f: with open("src/lib/data/schedule_tdy_tmr.csv", mode="w", newline="") as f:
data = [] data = []
for i in range(len(schedule)): for i in range(len(schedule)):
temp = [] temp = []
temp.append(schedule[i]["DAY"]) try:
for j in range(len(schedule[i]["SUBJECTS"])): temp.append(schedule[i]["DAY"])
temp.append(schedule[i]["SUBJECTS"][j]) except KeyError:
data.append(temp) temp.append("N/A")
for j in range(len(schedule[i]["SUBJECTS"])):
try:
temp.append(schedule[i]["SUBJECTS"][j])
except KeyError:
temp.append("N/A")
data.append(temp)
temp = [] temp = []
temp.append(schedule[i]["DATE"]) try:
for j in range(len(schedule[i]["WHERES"])): temp.append(schedule[i]["DATE"])
temp.append(schedule[i]["WHERES"][j]) except KeyError:
data.append(temp) temp.append("N/A")
for j in range(len(schedule[i]["WHERES"])):
try:
temp.append(schedule[i]["WHERES"][j])
except KeyError:
temp.append("N/A")
data.append(temp)
writer = csv.writer(f) writer = csv.writer(f)
for row in data: for row in data:
writer.writerow(row) writer.writerow(row)
def schedule_tdy_tmr_from_file() -> list: def schedule_tdy_tmr_from_file() -> list:
import csv import csv
schedule = [] schedule = []
try: try:
with open("src/lib/data/schedule_tdy_tmr.csv", mode="r") as f: with open("src/lib/data/schedule_tdy_tmr.csv", mode="r") as f:
reader = csv.reader(f) reader = csv.reader(f)
rows = [] rows = []
for row in reader: for row in reader:
rows.append(row) rows.append(row)
for row in range(0, len(rows), 2): for row in range(0, len(rows), 2):
temp = {} temp = {}
temp["DAY"] = rows[row][0] temp["DAY"] = rows[row][0]
try: try:
temp["DATE"] = rows[row+1][0] temp["DATE"] = rows[row+1][0]
except IndexError: except IndexError:
pass pass
subjects = [] subjects = []
for i in range(1, len(rows[row])): for i in range(1, len(rows[row])):
subjects.append(rows[row][i]) subjects.append(rows[row][i])
temp["SUBJECTS"] = subjects temp["SUBJECTS"] = subjects
wheres = [] wheres = []
try: try:
for i in range(1, len(rows[row+1])): for i in range(1, len(rows[row+1])):
wheres.append(rows[row+1][i]) wheres.append(rows[row+1][i])
except IndexError: except IndexError:
pass pass
temp["WHERES"] = wheres temp["WHERES"] = wheres
schedule.append(temp) schedule.append(temp)
except FileNotFoundError: except FileNotFoundError:
print("No schedule_tdy_tmr.csv file found\n") print("No schedule_tdy_tmr.csv file found\n")
return schedule return schedule
+244 -216
View File
@@ -4,284 +4,312 @@ from bs4 import BeautifulSoup
not_login = 0 not_login = 0
def fetch_locally() -> list: def fetch_locally() -> list:
global not_login global not_login
if not_login == 1: if not_login == 1:
not_login = 0 not_login = 0
#print("Could not sign in (you may have entered invalid login credentials)") #print("Could not sign in (you may have entered invalid login credentials)")
#print("Fetching the schedule for this week locally\n") #print("Fetching the schedule for this week locally\n")
schedule_week = schedule_week_from_file() schedule_week = schedule_week_from_file()
return schedule_week return schedule_week
def sort_schedule_week(schedule_week: list[dict]) -> list[dict]: def sort_schedule_week(schedule_week: list[dict]) -> list[dict]:
sorted_schedule_week: list[dict] = [{}, {}, {}, {}, {}] sorted_schedule_week: list[dict] = [{}, {}, {}, {}, {}]
for i in range(len(schedule_week)): for i in range(len(schedule_week)):
if schedule_week[i]["DAY"] == "Po": if schedule_week[i]["DAY"] == "Po":
sorted_schedule_week[0] = schedule_week[i] sorted_schedule_week[0] = schedule_week[i]
if schedule_week[i]["DAY"] == "Út": if schedule_week[i]["DAY"] == "Út":
sorted_schedule_week[1] = schedule_week[i] sorted_schedule_week[1] = schedule_week[i]
if schedule_week[i]["DAY"] == "St": if schedule_week[i]["DAY"] == "St":
sorted_schedule_week[2] = schedule_week[i] sorted_schedule_week[2] = schedule_week[i]
if schedule_week[i]["DAY"] == "Čt": if schedule_week[i]["DAY"] == "Čt":
sorted_schedule_week[3] = schedule_week[i] sorted_schedule_week[3] = schedule_week[i]
if schedule_week[i]["DAY"] == "": if schedule_week[i]["DAY"] == "":
sorted_schedule_week[4] = schedule_week[i] sorted_schedule_week[4] = schedule_week[i]
return sorted_schedule_week return sorted_schedule_week
def get_schedule_week(url: str): def get_schedule_week(url: str):
try: try:
html = nw.get_html_from(url) html = nw.get_html_from(url)
soup = BeautifulSoup(html.text, "lxml") soup = BeautifulSoup(html.text, "lxml")
table = soup.find("table", id="CCADynamicCalendarTable") table = soup.find("table", id="CCADynamicCalendarTable")
rows_odd = table.find_all("tr", class_="RowOdd") rows_odd = table.find_all("tr", class_="RowOdd")
rows_even = table.find_all("tr", class_="RowEven") rows_even = table.find_all("tr", class_="RowEven")
rows = rows_odd + rows_even rows = rows_odd + rows_even
schedule_week = [] schedule_week = []
for i in range(len(rows)): 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: if rows[i].find("td", class_="KuvSuplovanaHodina") is not None and rows[i].find("td", class_="KuvSuplujiciHodina") is None and rows[i].find("td", class_="KuvSkolniAkceHodina") is None:
continue continue
subjects = [] subjects = []
wheres = [] wheres = []
cells = rows[i].find_all("td", attrs={"class": "DctCell"}) 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: header_ = table.find("tr").find_all("td", class_="KuvHeaderNadpis")
day = rows[i].find("td", class_="KuvHeaderNadpis").text header = []
for val in header_:
header.append(val.text)
date = rows[i].find("td", class_="KuvHeaderText").text for j in range(len(cells)):
if cells[j].text == "" and header[j] != " - ":
new_span = soup.new_tag("span", attrs={'class': 'KuvBunkaRozvrhNadpis'})
new_span.string = "Volno"
cells[j].append(new_span)
subjects_ = rows[i].find_all("span", class_="KuvBunkaRozvrhNadpis") if rows[i].find("td", class_="KuvHeaderNadpis") is not None:
temp = [] day = rows[i].find("td", class_="KuvHeaderNadpis").text
for j in range(len(subjects_)):
temp.append(subjects_[j].text)
subjects = temp
F_con = False date = rows[i].find("td", class_="KuvHeaderText").text
try:
while subjects[-1] == "Volno":
del subjects[-1]
if subjects == []:
F_con = True
except IndexError:
pass
wheres_ = rows[i].find_all("span", class_="KuvBunkaRozvrhText") subjects_ = rows[i].find_all("span", class_="KuvBunkaRozvrhNadpis")
temp = [] temp = []
for j in range(len(wheres_)): for j in range(len(subjects_)):
if len(subjects[j]) < 10: if "KuvSuplovanaHodina" not in subjects_[j].parent["class"]:
temp.append(wheres_[j].text[4:]) temp.append(subjects_[j].text)
else: subjects = temp
temp.append(" - ")
wheres = temp
for j in range(len(cells)): F_continue = False
if cells[j].get("colspan") is not None: try:
if int(cells[j].get("colspan")) == 2: while subjects[-1] == "Volno":
subjects.insert(j, subjects[j]) del subjects[-1]
wheres.insert(j, wheres[j]) if subjects == []:
for j in range(len(subjects)): F_continue = True
if subjects[j] == "Volno": except IndexError:
wheres.insert(j, " - ") pass
if F_con == False: if subjects == []:
schedule_week_row = { F_continue = True
"DAY": day,
"DATE": date,
"SUBJECTS": subjects,
"WHERES": wheres
}
schedule_week.append(schedule_week_row) wheres_ = rows[i].find_all("span", class_="KuvBunkaRozvrhText")
temp = []
for j in range(len(wheres_)):
try:
if len(subjects[j]) < 10:
if len(wheres_[j].text[4:]) > 10:
temp.append(" - ")
elif "KuvSuplovanaHodina" not in wheres_[j].parent["class"]:
temp.append(wheres_[j].text[4:])
else:
temp.append(" - ")
except IndexError:
pass
wheres = temp
sorted_schedule_week = sort_schedule_week(schedule_week) 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-1])
wheres.insert(j, wheres[j-1])
for j in range(len(subjects)):
if subjects[j] == "Volno":
wheres.insert(j, " - ")
return sorted_schedule_week if F_continue == False:
except AttributeError: schedule_week_row = {
global not_login "DAY": day,
"DATE": date,
"SUBJECTS": subjects,
"WHERES": wheres
}
not_login = 1 schedule_week.append(schedule_week_row)
schedule = fetch_locally()
return schedule sorted_schedule_week = sort_schedule_week(schedule_week)
return sorted_schedule_week
except AttributeError:
global not_login
not_login = 1
schedule = fetch_locally()
return schedule
def schedule_week_from_file_to_list(): def schedule_week_from_file_to_list():
schedule = [] schedule = []
schedule_ = [] schedule_ = []
try: try:
with open("src/lib/data/schedule_week.csv") as f: with open("src/lib/data/schedule_week.csv") as f:
import csv import csv
reader = csv.reader(f) reader = csv.reader(f)
for row in reader: for row in reader:
temp = [] temp = []
for col in row: for col in row:
temp.append(col.strip()) temp.append(col.strip())
schedule_.append(temp) schedule_.append(temp)
for i in range(0, len(schedule_), 2): for i in range(0, len(schedule_), 2):
del schedule_[i][0] del schedule_[i][0]
temp = [] temp = []
for i in range(1, len(schedule_), 2): for i in range(1, len(schedule_), 2):
temp.append(schedule_[i][0]) temp.append(schedule_[i][0])
del schedule_[i][0] del schedule_[i][0]
schedule.append(temp) schedule.append(temp)
for i in range(10): for i in range(10):
temp = [] temp = []
for j in range(10): for j in range(10):
try: try:
temp.append(schedule_[j][0]) temp.append(schedule_[j][0])
del schedule_[j][0] del schedule_[j][0]
except IndexError: except IndexError:
pass pass
schedule.append(temp) schedule.append(temp)
except FileNotFoundError: except FileNotFoundError:
print("No schedule_week.csv file found\n") print("No schedule_week.csv file found\n")
return schedule return schedule
def get_whitespace(key: str, idx=0) -> int: def get_whitespace(key: str, idx=0) -> int:
schedule = schedule_week_from_file_to_list() schedule = schedule_week_from_file_to_list()
max_len = 0 max_len = 0
if key == "DATE": if key == "DATE":
try: try:
max_len = len(max(schedule[0], key=len)) max_len = len(max(schedule[0], key=len))
except ValueError: except ValueError:
pass pass
elif key == "WHERES": elif key == "WHERES":
try: try:
max_len = len(max(schedule[idx], key=len)) max_len = len(max(schedule[idx], key=len))
except ValueError: except ValueError:
pass pass
return max_len return max_len
def show_schedule_week(): def show_schedule_week():
# if there is internet access, load stuff from the web # if there is internet access, load stuff from the web
# if not, load the schedule.csv file # if not, load the schedule.csv file
if nw.is_connected() == True: if nw.is_connected() == True:
schedule = get_schedule_week("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx") schedule = get_schedule_week("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
schedule_week_to_file(schedule) schedule_week_to_file(schedule)
else: else:
schedule = schedule_week_from_file() schedule = schedule_week_from_file()
print("") print("")
print("----------------") print("----------------")
print(" Týdenní rozvrh ") print(" Týdenní rozvrh ")
print("----------------\n") print("----------------\n")
if schedule == []: if schedule == []:
print("N/A\n") print("N/A\n")
import os import os
if os.path.exists("src/lib/data/schedule_week.csv"): if os.path.exists("src/lib/data/schedule_week.csv"):
os.remove("src/lib/data/schedule_week.csv") os.remove("src/lib/data/schedule_week.csv")
return return
for i in range(len(schedule)): for i in range(len(schedule)):
max_len = get_whitespace("DATE") max_len = get_whitespace("DATE")
whitespace = " " * (max_len - len(schedule[i]["DAY"])) whitespace = " " * (max_len - len(schedule[i]["DAY"]))
print(schedule[i]["DAY"] + whitespace, end=" ") print(schedule[i]["DAY"] + whitespace, end=" ")
for j in range(len(schedule[i]["SUBJECTS"])): for j in range(len(schedule[i]["SUBJECTS"])):
max_len = get_whitespace("WHERES", j+1) max_len = get_whitespace("WHERES", j+1)
whitespace = " " * (max_len - len(schedule[i]["SUBJECTS"][j])) whitespace = " " * (max_len - len(schedule[i]["SUBJECTS"][j]))
print("| " + schedule[i]["SUBJECTS"][j] + whitespace, end=" ") print("| " + schedule[i]["SUBJECTS"][j] + whitespace, end=" ")
print("") print("")
max_len = get_whitespace("DATE") max_len = get_whitespace("DATE")
whitespace = " " * (max_len - len(schedule[i]["DATE"])) whitespace = " " * (max_len - len(schedule[i]["DATE"]))
print(schedule[i]["DATE"] + whitespace, end=" ") print(schedule[i]["DATE"] + whitespace, end=" ")
for j in range(len(schedule[i]["WHERES"])): for j in range(len(schedule[i]["WHERES"])):
max_len = get_whitespace("WHERES", j+1) max_len = get_whitespace("WHERES", j+1)
whitespace = " " * (max_len - len(schedule[i]["WHERES"][j])) whitespace = " " * (max_len - len(schedule[i]["WHERES"][j]))
print("| " + schedule[i]["WHERES"][j] + whitespace, end=" ") print("| " + schedule[i]["WHERES"][j] + whitespace, end=" ")
print("") print("")
def schedule_week_to_file(schedule=None): def schedule_week_to_file(schedule=None):
import csv import csv
if schedule is None: if schedule is None:
try: try:
schedule = get_schedule_week("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx") schedule = get_schedule_week("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
except Exception: except Exception:
print("An error occured, you probably can't access the internet right now") print("An error occured, you probably can't access the internet right now")
return return
with open("src/lib/data/schedule_week.csv", mode="w", newline="") as f: with open("src/lib/data/schedule_week.csv", mode="w", newline="") as f:
data = [] data = []
for i in range(len(schedule)): for i in range(len(schedule)):
temp = [] temp = []
temp.append(schedule[i]["DAY"]) try:
for j in range(len(schedule[i]["SUBJECTS"])): temp.append(schedule[i]["DAY"])
temp.append(schedule[i]["SUBJECTS"][j]) except KeyError:
data.append(temp) temp.append("N/A")
for j in range(len(schedule[i]["SUBJECTS"])):
try:
temp.append(schedule[i]["SUBJECTS"][j])
except KeyError:
temp.append("N/A")
data.append(temp)
temp = [] temp = []
temp.append(schedule[i]["DATE"]) try:
for j in range(len(schedule[i]["WHERES"])): temp.append(schedule[i]["DATE"])
temp.append(schedule[i]["WHERES"][j]) except KeyError:
data.append(temp) temp.append("N/A")
for j in range(len(schedule[i]["WHERES"])):
try:
temp.append(schedule[i]["WHERES"][j])
except KeyError:
temp.append("N/A")
data.append(temp)
writer = csv.writer(f) writer = csv.writer(f)
for row in data: for row in data:
writer.writerow(row) writer.writerow(row)
def schedule_week_from_file() -> list: def schedule_week_from_file() -> list:
import csv import csv
schedule = [] schedule = []
try: try:
with open("src/lib/data/schedule_week.csv", mode="r") as f: with open("src/lib/data/schedule_week.csv", mode="r") as f:
reader = csv.reader(f) reader = csv.reader(f)
rows = [] rows = []
for row in reader: for row in reader:
rows.append(row) rows.append(row)
for row in range(0, len(rows), 2): for row in range(0, len(rows), 2):
temp = {} temp = {}
temp["DAY"] = rows[row][0] temp["DAY"] = rows[row][0]
try: try:
temp["DATE"] = rows[row+1][0] temp["DATE"] = rows[row+1][0]
except IndexError: except IndexError:
pass pass
subjects = [] subjects = []
for i in range(1, len(rows[row])): for i in range(1, len(rows[row])):
subjects.append(rows[row][i]) subjects.append(rows[row][i])
temp["SUBJECTS"] = subjects temp["SUBJECTS"] = subjects
wheres = [] wheres = []
try: try:
for i in range(1, len(rows[row+1])): for i in range(1, len(rows[row+1])):
wheres.append(rows[row+1][i]) wheres.append(rows[row+1][i])
except IndexError: except IndexError:
pass pass
temp["WHERES"] = wheres temp["WHERES"] = wheres
schedule.append(temp) schedule.append(temp)
except FileNotFoundError: except FileNotFoundError:
print("No schedule_week.csv file found\n") print("No schedule_week.csv file found\n")
return schedule return schedule
+23 -12
View File
@@ -4,16 +4,27 @@ from lib import schedule_week as sw
from lib import schedule_tdy_tmr as st from lib import schedule_tdy_tmr as st
if __name__ == "__main__": if __name__ == "__main__":
user = str(input("Zadejte přihlašovací jméno: ")) user = str(input("Zadejte přihlašovací jméno: "))
pwd = str(input("Zadejte heslo: ")) pwd = str(input("Zadejte heslo: "))
if nw.is_connected() == True:
nw.login(user, pwd) if nw.is_connected() == True:
#sw.get_schedule_week("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx") nw.login(user, pwd)
sw.show_schedule_week()
#gr.get_grades("/SOL/App/Hodnoceni/KZH001_HodnVypisStud.aspx")
gr.show_grades() #sw.get_schedule_week("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
#sw.schedule_week_to_file() #sw.schedule_week_to_file()
#gr.grades_to_file() #sw.schedule_week_from_file()
#sw.schedule_week_from_file() sw.show_schedule_week()
st.show_schedule_tdy_tmr()
#gr.get_grades("/SOL/App/Hodnoceni/KZH001_HodnVypisStud.aspx")
#gr.grades_to_file()
#gr.grades_from_file()
gr.show_grades()
#st.get_schedule("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
#st.schedule_tdy_tmr_to_file()
#st.schedule_tdy_tmr_from_file()
st.show_schedule_tdy_tmr()