Mon Jun 26 15:25:26 CEST 2023
This commit is contained in:
+1
-1
@@ -2,4 +2,4 @@
|
|||||||
src/lib/data/*.csv
|
src/lib/data/*.csv
|
||||||
src/lib/shadow
|
src/lib/shadow
|
||||||
TODO.md
|
TODO.md
|
||||||
|
*.swp
|
||||||
|
|||||||
+15
-6
@@ -49,9 +49,13 @@ def encrypt_and_write_to_file(password: str, strdata: str, file_path: str) -> bo
|
|||||||
f = Fernet(key)
|
f = Fernet(key)
|
||||||
|
|
||||||
encrypted = f.encrypt(strdata.encode())
|
encrypted = f.encrypt(strdata.encode())
|
||||||
|
if os.path.exists(file_path):
|
||||||
with open(file_path, "wb") as ef:
|
with open(file_path, "wb") as ef:
|
||||||
ef.write(encrypted)
|
ef.write(encrypted)
|
||||||
|
else:
|
||||||
|
os.system(f"touch {file_path}")
|
||||||
|
with open(file_path, "wb") as ef:
|
||||||
|
ef.write(encrypted)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
except:
|
except:
|
||||||
@@ -68,9 +72,14 @@ def read_from_file_and_decrypt(password: str, file_path: str) -> str:
|
|||||||
|
|
||||||
f = Fernet(key)
|
f = Fernet(key)
|
||||||
|
|
||||||
with open(file_path, "rb") as ef:
|
if os.path.exists(file_path):
|
||||||
decrypted = f.decrypt(ef.read())
|
with open(file_path, "rb") as ef:
|
||||||
|
decrypted = f.decrypt(ef.read())
|
||||||
|
else:
|
||||||
|
os.system(f"touch {file_path}")
|
||||||
|
with open(file_path, "rb") as ef:
|
||||||
|
decrypted = f.decrypt(ef.read())
|
||||||
|
|
||||||
return decrypted.decode()
|
return decrypted.decode()
|
||||||
except:
|
except:
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
+3
-7
@@ -106,10 +106,7 @@ def show_grades(password, username, local=False):
|
|||||||
|
|
||||||
if grades == []:
|
if grades == []:
|
||||||
print("N/A\n")
|
print("N/A\n")
|
||||||
import os
|
|
||||||
|
|
||||||
if os.path.exists(f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-encrypted_grades"):
|
|
||||||
os.remove(f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-encrypted_grades")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
for i in range(len(grades)):
|
for i in range(len(grades)):
|
||||||
@@ -131,7 +128,6 @@ def grades_to_file(password, username, grades=None):
|
|||||||
for later use and extraction.
|
for later use and extraction.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import csv
|
|
||||||
if grades is None:
|
if grades is None:
|
||||||
try:
|
try:
|
||||||
grades = get_grades(password, username, "/SOL/App/Hodnoceni/KZH001_HodnVypisStud.aspx")
|
grades = get_grades(password, username, "/SOL/App/Hodnoceni/KZH001_HodnVypisStud.aspx")
|
||||||
@@ -168,7 +164,7 @@ def grades_to_file(password, username, grades=None):
|
|||||||
|
|
||||||
strdata = "".join(lstrdata)
|
strdata = "".join(lstrdata)
|
||||||
|
|
||||||
cr.encrypt_and_write_to_file(password, strdata, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-encrypted_grades")
|
cr.encrypt_and_write_to_file(password, strdata, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-grades")
|
||||||
|
|
||||||
def grades_from_file(password, username) -> list:
|
def grades_from_file(password, username) -> list:
|
||||||
"""
|
"""
|
||||||
@@ -179,7 +175,7 @@ def grades_from_file(password, username) -> list:
|
|||||||
import csv
|
import csv
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
try:
|
try:
|
||||||
strdata = cr.read_from_file_and_decrypt(password, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-encrypted_grades")
|
strdata = cr.read_from_file_and_decrypt(password, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-grades")
|
||||||
except:
|
except:
|
||||||
print("Could not read the file")
|
print("Could not read the file")
|
||||||
return
|
return
|
||||||
|
|||||||
+14
-7
@@ -18,9 +18,15 @@ def _read_shadow() -> str:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
result = ""
|
result = ""
|
||||||
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "r") as f:
|
try:
|
||||||
for row in f:
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "r") as f:
|
||||||
result += row+";"
|
for row in f:
|
||||||
|
result += row+";"
|
||||||
|
except FileNotFoundError:
|
||||||
|
os.system(f"touch {os.environ['HOME']}/.local/lib/solendata/shadow")
|
||||||
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "r") as f:
|
||||||
|
for row in f:
|
||||||
|
result += row+";"
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -30,14 +36,17 @@ def _save_creds(user: str, pwd: str):
|
|||||||
by appending them.
|
by appending them.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
creds = _get_creds_hash(user, pwd)
|
creds = ";" + _get_creds_hash(user, pwd)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "a+") as f:
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "a+") as f:
|
||||||
if not creds in _read_shadow():
|
if not creds in _read_shadow():
|
||||||
f.write(creds)
|
f.write(creds)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("No shadow file found")
|
os.system(f"touch {os.environ['HOME']}/.local/lib/solendata/shadow")
|
||||||
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "a+") as f:
|
||||||
|
if not creds in _read_shadow():
|
||||||
|
f.write(creds)
|
||||||
|
|
||||||
def _eval_creds(creds: str) -> bool:
|
def _eval_creds(creds: str) -> bool:
|
||||||
"""
|
"""
|
||||||
@@ -132,8 +141,6 @@ def get_html_from(params=None) -> str:
|
|||||||
|
|
||||||
response = session.get(base)
|
response = session.get(base)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
#print("Download successful")
|
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
print("Download unsuccessful")
|
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
+104
-110
@@ -4,12 +4,13 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from solenlib import networking as nw
|
from solenlib import networking as nw
|
||||||
|
from solenlib import crypto as cr
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import os
|
import os
|
||||||
|
|
||||||
not_login = 0
|
not_login = 0
|
||||||
|
|
||||||
def fetch_locally() -> list:
|
def fetch_locally(password, username) -> list:
|
||||||
"""
|
"""
|
||||||
Helper function for exctracting
|
Helper function for exctracting
|
||||||
data from a local file.
|
data from a local file.
|
||||||
@@ -20,10 +21,10 @@ def fetch_locally() -> list:
|
|||||||
if not_login == 1:
|
if not_login == 1:
|
||||||
not_login = 0
|
not_login = 0
|
||||||
|
|
||||||
schedule = schedule_from_file()
|
schedule = schedule_from_file(password, username)
|
||||||
return schedule
|
return schedule
|
||||||
|
|
||||||
def get_schedule(url: str) -> list:
|
def get_schedule(password, url: str) -> list:
|
||||||
"""
|
"""
|
||||||
Filters & extracts the schedule from
|
Filters & extracts the schedule from
|
||||||
the HTML soup.
|
the HTML soup.
|
||||||
@@ -128,15 +129,15 @@ def get_schedule(url: str) -> list:
|
|||||||
}
|
}
|
||||||
|
|
||||||
schedule.append(schedule_row)
|
schedule.append(schedule_row)
|
||||||
|
|
||||||
return schedule
|
return schedule
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
global not_login
|
global not_login
|
||||||
|
|
||||||
not_login = 1
|
not_login = 1
|
||||||
return fetch_locally()
|
return fetch_locally(password, username)
|
||||||
|
|
||||||
def schedule_from_file_to_list():
|
def schedule_from_file_to_list(password, username, strdata):
|
||||||
"""
|
"""
|
||||||
Extracts the schedule from
|
Extracts the schedule from
|
||||||
a local file and returns
|
a local file and returns
|
||||||
@@ -146,41 +147,39 @@ def schedule_from_file_to_list():
|
|||||||
|
|
||||||
schedule = []
|
schedule = []
|
||||||
schedule_ = []
|
schedule_ = []
|
||||||
|
|
||||||
try:
|
|
||||||
with open(f"{os.environ['HOME']}/.local/lib/solendata/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):
|
import csv
|
||||||
del schedule_[i][0]
|
from io import StringIO
|
||||||
|
f = StringIO(strdata)
|
||||||
|
reader = csv.reader(f, delimiter=",")
|
||||||
|
for row in reader:
|
||||||
|
temp = []
|
||||||
|
for col in row:
|
||||||
|
temp.append(col.strip())
|
||||||
|
schedule_.append(temp)
|
||||||
|
|
||||||
temp = []
|
for i in range(0, len(schedule_), 2):
|
||||||
for i in range(1, len(schedule_), 2):
|
del schedule_[i][0]
|
||||||
temp.append(schedule_[i][0])
|
|
||||||
del schedule_[i][0]
|
|
||||||
schedule.append(temp)
|
|
||||||
|
|
||||||
for i in range(10):
|
temp = []
|
||||||
temp = []
|
for i in range(1, len(schedule_), 2):
|
||||||
for j in range(10):
|
temp.append(schedule_[i][0])
|
||||||
try:
|
del schedule_[i][0]
|
||||||
temp.append(schedule_[j][0])
|
schedule.append(temp)
|
||||||
del schedule_[j][0]
|
|
||||||
except IndexError:
|
for i in range(10):
|
||||||
pass
|
temp = []
|
||||||
schedule.append(temp)
|
for j in range(10):
|
||||||
except FileNotFoundError:
|
try:
|
||||||
print("No schedule_tdy_tmr.csv file found\n")
|
temp.append(schedule_[j][0])
|
||||||
|
del schedule_[j][0]
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
schedule.append(temp)
|
||||||
|
|
||||||
return schedule
|
return schedule
|
||||||
|
|
||||||
def get_whitespace(key: str, idx=0) -> int:
|
def get_whitespace(password, username, strdata, key: str, idx=0) -> int:
|
||||||
"""
|
"""
|
||||||
Helper function that calculates
|
Helper function that calculates
|
||||||
how much whitespaces are needed
|
how much whitespaces are needed
|
||||||
@@ -188,7 +187,7 @@ def get_whitespace(key: str, idx=0) -> int:
|
|||||||
correctly.
|
correctly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
schedule = schedule_from_file_to_list()
|
schedule = schedule_from_file_to_list(password, username, strdata)
|
||||||
max_len = 0
|
max_len = 0
|
||||||
|
|
||||||
if key == "DATE":
|
if key == "DATE":
|
||||||
@@ -204,22 +203,28 @@ def get_whitespace(key: str, idx=0) -> int:
|
|||||||
|
|
||||||
return max_len
|
return max_len
|
||||||
|
|
||||||
def show_schedule(local=False):
|
def show_schedule(password, username, local=False):
|
||||||
"""
|
"""
|
||||||
Shows the schedule on the
|
Shows the schedule on the
|
||||||
terminal in a pretty way.
|
terminal in a pretty way.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
strdata = cr.read_from_file_and_decrypt(password, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-schedule_tdy_tmr")
|
||||||
|
except:
|
||||||
|
print("Could not read the file")
|
||||||
|
return
|
||||||
|
|
||||||
if local == False:
|
if local == False:
|
||||||
# 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("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
|
schedule = get_schedule(password, "/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
|
||||||
schedule_to_file(schedule)
|
schedule_to_file(password, username, schedule)
|
||||||
else:
|
else:
|
||||||
schedule = schedule_from_file()
|
schedule = schedule_from_file(password, username)
|
||||||
else:
|
else:
|
||||||
schedule = schedule_from_file()
|
schedule = schedule_from_file(password, username)
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
@@ -229,123 +234,112 @@ def show_schedule(local=False):
|
|||||||
|
|
||||||
if schedule == []:
|
if schedule == []:
|
||||||
print("N/A\n")
|
print("N/A\n")
|
||||||
import os
|
|
||||||
|
|
||||||
if os.path.exists(f"{os.environ['HOME']}/.local/lib/solendata/schedule_tdy_tmr.csv"):
|
|
||||||
os.remove(f"{os.environ['HOME']}/.local/lib/solendata/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(password, username, strdata, "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(password, username, strdata, "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(password, username, strdata, "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(password, username, strdata, "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_to_file(schedule=None):
|
def schedule_to_file(password, username, schedule=None):
|
||||||
"""
|
"""
|
||||||
Writes the schedule to
|
Writes the schedule to
|
||||||
a local file for later
|
a local file for later
|
||||||
use.
|
use.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import csv
|
|
||||||
if schedule is None:
|
if schedule is None:
|
||||||
try:
|
try:
|
||||||
schedule = get_schedule("/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
|
schedule = get_schedule(password, "/SOL/App/Spolecne/KZZ010_RychlyPrehled.aspx")
|
||||||
except Exception:
|
except:
|
||||||
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(f"{os.environ['HOME']}/.local/lib/solendata/schedule_tdy_tmr.csv", mode="w", newline="") as f:
|
lstrdata = []
|
||||||
data = []
|
|
||||||
|
|
||||||
for i in range(len(schedule)):
|
for row in schedule:
|
||||||
temp = []
|
lstrdata.append(row["DAY"])
|
||||||
try:
|
lstrdata.append(",")
|
||||||
temp.append(schedule[i]["DAY"])
|
for i in row["SUBJECTS"]:
|
||||||
except KeyError:
|
lstrdata.append(i)
|
||||||
temp.append("N/A")
|
lstrdata.append(",")
|
||||||
for j in range(len(schedule[i]["SUBJECTS"])):
|
del lstrdata[-1]
|
||||||
try:
|
lstrdata.append("\n")
|
||||||
temp.append(schedule[i]["SUBJECTS"][j])
|
|
||||||
except KeyError:
|
|
||||||
temp.append("N/A")
|
|
||||||
data.append(temp)
|
|
||||||
|
|
||||||
temp = []
|
|
||||||
try:
|
|
||||||
temp.append(schedule[i]["DATE"])
|
|
||||||
except KeyError:
|
|
||||||
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)
|
lstrdata.append(row["DATE"])
|
||||||
for row in data:
|
for i in row["WHERES"]:
|
||||||
writer.writerow(row)
|
lstrdata.append(i)
|
||||||
|
lstrdata.append(",")
|
||||||
|
del lstrdata[-1]
|
||||||
|
lstrdata.append("\n")
|
||||||
|
|
||||||
def schedule_from_file() -> list:
|
strdata = "".join(lstrdata)
|
||||||
|
|
||||||
|
cr.encrypt_and_write_to_file(password, strdata, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-schedule_tdy_tmr")
|
||||||
|
|
||||||
|
def schedule_from_file(password, username) -> list:
|
||||||
"""
|
"""
|
||||||
|
Fetches schedule data from a file.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
|
from io import StringIO
|
||||||
schedule = []
|
schedule = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(f"{os.environ['HOME']}/.local/lib/solendata/schedule_tdy_tmr.csv", mode="r") as f:
|
strdata = cr.read_from_file_and_decrypt(password, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-schedule_tdy_tmr")
|
||||||
reader = csv.reader(f)
|
except:
|
||||||
rows = []
|
print("Could not read the file")
|
||||||
for row in reader:
|
return
|
||||||
rows.append(row)
|
f = StringIO(strdata)
|
||||||
|
reader = csv.reader(f, delimiter=",")
|
||||||
|
rows = []
|
||||||
|
for row in reader:
|
||||||
|
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:
|
||||||
|
temp["DATE"] = rows[row+1][0]
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
subjects = []
|
||||||
temp["DATE"] = rows[row+1][0]
|
for i in range(1, len(rows[row])):
|
||||||
except IndexError:
|
subjects.append(rows[row][i])
|
||||||
pass
|
temp["SUBJECTS"] = subjects
|
||||||
|
|
||||||
subjects = []
|
|
||||||
for i in range(1, len(rows[row])):
|
|
||||||
subjects.append(rows[row][i])
|
|
||||||
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:
|
|
||||||
print("No schedule_tdy_tmr.csv file found\n")
|
|
||||||
|
|
||||||
return schedule
|
return schedule
|
||||||
|
|||||||
+152
-110
@@ -1,21 +1,36 @@
|
|||||||
|
"""
|
||||||
|
Functions related to this
|
||||||
|
week's schedule.
|
||||||
|
"""
|
||||||
|
|
||||||
from solenlib import networking as nw
|
from solenlib import networking as nw
|
||||||
|
from solenlib import crypto as cr
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import os
|
import os
|
||||||
|
|
||||||
not_login = 0
|
not_login = 0
|
||||||
|
|
||||||
def fetch_locally() -> list:
|
def fetch_locally(password, username) -> list:
|
||||||
|
"""
|
||||||
|
Helper function for extracting
|
||||||
|
data from a local file.
|
||||||
|
"""
|
||||||
|
|
||||||
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("Fetching the schedule for this week locally\n")
|
schedule = schedule_from_file(password, username)
|
||||||
schedule = schedule_from_file()
|
|
||||||
return schedule
|
return schedule
|
||||||
|
|
||||||
|
|
||||||
def sort_schedule(schedule: list[dict]) -> list[dict]:
|
def sort_schedule(schedule: list[dict]) -> list[dict]:
|
||||||
|
"""
|
||||||
|
Helper function for sorting
|
||||||
|
the schedule correctly.
|
||||||
|
"""
|
||||||
|
|
||||||
sorted_schedule: list[dict] = [{}, {}, {}, {}, {}]
|
sorted_schedule: list[dict] = [{}, {}, {}, {}, {}]
|
||||||
|
|
||||||
for i in range(len(schedule)):
|
for i in range(len(schedule)):
|
||||||
@@ -32,7 +47,12 @@ def sort_schedule(schedule: list[dict]) -> list[dict]:
|
|||||||
|
|
||||||
return sorted_schedule
|
return sorted_schedule
|
||||||
|
|
||||||
def get_schedule(url: str):
|
def get_schedule(password, url: str) -> list:
|
||||||
|
"""
|
||||||
|
Filters & extracts the schedule from
|
||||||
|
the HTML soup.
|
||||||
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
html = nw.get_html_from(url)
|
html = nw.get_html_from(url)
|
||||||
|
|
||||||
@@ -146,45 +166,57 @@ def get_schedule(url: str):
|
|||||||
schedule = fetch_locally()
|
schedule = fetch_locally()
|
||||||
return schedule
|
return schedule
|
||||||
|
|
||||||
def schedule_from_file_to_list():
|
def schedule_from_file_to_list(password, username, strdata):
|
||||||
|
"""
|
||||||
|
Extracts the schedule from
|
||||||
|
a local file and returns
|
||||||
|
it in a list, which is better
|
||||||
|
for the get_whitespace() function.
|
||||||
|
"""
|
||||||
|
|
||||||
schedule = []
|
schedule = []
|
||||||
schedule_ = []
|
schedule_ = []
|
||||||
|
|
||||||
try:
|
import csv
|
||||||
with open(f"{os.environ['HOME']}/.local/lib/solendata/schedule_week.csv") as f:
|
from io import StringIO
|
||||||
import csv
|
f = StringIO(strdata)
|
||||||
reader = csv.reader(f)
|
reader = csv.reader(f, delimiter=",")
|
||||||
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:
|
|
||||||
print("No schedule_week.csv file found\n")
|
|
||||||
|
|
||||||
return schedule
|
return schedule
|
||||||
|
|
||||||
def get_whitespace(key: str, idx=0) -> int:
|
def get_whitespace(password, username, strdata, key: str, idx=0) -> int:
|
||||||
schedule = schedule_from_file_to_list()
|
"""
|
||||||
|
Helper function that calculates
|
||||||
|
how much whitespaces are needed
|
||||||
|
in order to display the table grid
|
||||||
|
correctly.
|
||||||
|
"""
|
||||||
|
|
||||||
|
schedule = schedule_from_file_to_list(password, username, strdata)
|
||||||
max_len = 0
|
max_len = 0
|
||||||
|
|
||||||
if key == "DATE":
|
if key == "DATE":
|
||||||
@@ -200,17 +232,28 @@ def get_whitespace(key: str, idx=0) -> int:
|
|||||||
|
|
||||||
return max_len
|
return max_len
|
||||||
|
|
||||||
def show_schedule(local=False):
|
def show_schedule(password, username, local=False):
|
||||||
|
"""
|
||||||
|
Shows the schedule on the
|
||||||
|
terminal in a pretty way.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
strdata = cr.read_from_file_and_decrypt(password, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-schedule_week")
|
||||||
|
except:
|
||||||
|
print("Could not read the file")
|
||||||
|
return
|
||||||
|
|
||||||
if local == False:
|
if local == False:
|
||||||
# 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("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
|
schedule = get_schedule(password, "/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
|
||||||
schedule_to_file(schedule)
|
schedule_to_file(password, username, schedule)
|
||||||
else:
|
else:
|
||||||
schedule = schedule_from_file()
|
schedule = schedule_from_file(password, username)
|
||||||
else:
|
else:
|
||||||
schedule = schedule_from_file()
|
schedule = schedule_from_file(password, username)
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
@@ -220,113 +263,112 @@ def show_schedule(local=False):
|
|||||||
|
|
||||||
if schedule == []:
|
if schedule == []:
|
||||||
print("N/A\n")
|
print("N/A\n")
|
||||||
import os
|
|
||||||
|
|
||||||
if os.path.exists(f"{os.environ['HOME']}/.local/lib/solendata/schedule_week.csv"):
|
|
||||||
os.remove(f"{os.environ['HOME']}/.local/lib/solendata/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(password, username, strdata, "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(password, username, strdata, "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(password, username, strdata, "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(password, username, strdata, "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_to_file(schedule=None):
|
def schedule_to_file(password, username, schedule=None):
|
||||||
import csv
|
"""
|
||||||
|
Writes the schedule to
|
||||||
|
a local file for later
|
||||||
|
use.
|
||||||
|
"""
|
||||||
|
|
||||||
if schedule is None:
|
if schedule is None:
|
||||||
try:
|
try:
|
||||||
schedule = get_schedule("/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
|
schedule = get_schedule(password, "/SOL/App/Kalendar/KZK001_KalendarTyden.aspx")
|
||||||
except Exception:
|
except:
|
||||||
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(f"{os.environ['HOME']}/.local/lib/solendata/schedule_week.csv", mode="w", newline="") as f:
|
lstrdata = []
|
||||||
data = []
|
|
||||||
|
|
||||||
for i in range(len(schedule)):
|
for row in schedule:
|
||||||
temp = []
|
lstrdata.append(row["DAY"])
|
||||||
try:
|
lstrdata.append(",")
|
||||||
temp.append(schedule[i]["DAY"])
|
for i in row["SUBJECTS"]:
|
||||||
except KeyError:
|
lstrdata.append(i)
|
||||||
temp.append("N/A")
|
lstrdata.append(",")
|
||||||
for j in range(len(schedule[i]["SUBJECTS"])):
|
del lstrdata[-1]
|
||||||
try:
|
lstrdata.append("\n")
|
||||||
temp.append(schedule[i]["SUBJECTS"][j])
|
|
||||||
except KeyError:
|
lstrdata.append(row["DATE"])
|
||||||
temp.append("N/A")
|
for i in row["WHERES"]:
|
||||||
data.append(temp)
|
lstrdata.append(i)
|
||||||
|
lstrdata.append(",")
|
||||||
|
del lstrdata[-1]
|
||||||
|
lstrdata.append("\n")
|
||||||
|
|
||||||
|
strdata = "".join(lstrdata)
|
||||||
|
|
||||||
|
cr.encrypt_and_write_to_file(password, strdata, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-schedule_week")
|
||||||
|
|
||||||
temp = []
|
def schedule_from_file(password, username) -> list:
|
||||||
try:
|
"""
|
||||||
temp.append(schedule[i]["DATE"])
|
Fetches schedule data from a file.
|
||||||
except KeyError:
|
"""
|
||||||
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)
|
|
||||||
for row in data:
|
|
||||||
writer.writerow(row)
|
|
||||||
|
|
||||||
def schedule_from_file() -> list:
|
|
||||||
import csv
|
import csv
|
||||||
|
from io import StringIO
|
||||||
schedule = []
|
schedule = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(f"{os.environ['HOME']}/.local/lib/solendata/schedule_week.csv", mode="r") as f:
|
strdata = cr.read_from_file_and_decrypt(password, f"{os.environ['HOME']}/.local/lib/solendata/{nw.get_user_hash(username)}-schedule_week")
|
||||||
reader = csv.reader(f)
|
except:
|
||||||
rows = []
|
print("Could not read the file")
|
||||||
for row in reader:
|
return
|
||||||
rows.append(row)
|
f = StringIO(strdata)
|
||||||
|
reader = csv.reader(f)
|
||||||
|
rows = []
|
||||||
|
for row in reader:
|
||||||
|
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:
|
|
||||||
print("No schedule_week.csv file found\n")
|
|
||||||
|
|
||||||
return schedule
|
return schedule
|
||||||
|
|||||||
+8
-8
@@ -95,14 +95,14 @@ if __name__ == "__main__":
|
|||||||
try:
|
try:
|
||||||
if args.week:
|
if args.week:
|
||||||
if args.local:
|
if args.local:
|
||||||
sw.show_schedule(local=True)
|
sw.show_schedule(pwd, user, local=True)
|
||||||
else:
|
else:
|
||||||
sw.show_schedule()
|
sw.show_schedule(pwd, user)
|
||||||
if args.today_tomorrow:
|
if args.today_tomorrow:
|
||||||
if args.local:
|
if args.local:
|
||||||
st.show_schedule(local=True)
|
st.show_schedule(pwd, user, local=True)
|
||||||
else:
|
else:
|
||||||
st.show_schedule()
|
st.show_schedule(pwd, user)
|
||||||
if args.grades:
|
if args.grades:
|
||||||
if args.local:
|
if args.local:
|
||||||
gr.show_grades(pwd, user, local=True)
|
gr.show_grades(pwd, user, local=True)
|
||||||
@@ -111,12 +111,12 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
if not args.grades and not args.today_tomorrow and not args.week:
|
if not args.grades and not args.today_tomorrow and not args.week:
|
||||||
if args.local:
|
if args.local:
|
||||||
sw.show_schedule(local=True)
|
sw.show_schedule(pwd, user, local=True)
|
||||||
st.show_schedule(local=True)
|
st.show_schedule(pwd, user, local=True)
|
||||||
gr.show_grades(pwd, user, local=True)
|
gr.show_grades(pwd, user, local=True)
|
||||||
else:
|
else:
|
||||||
sw.show_schedule()
|
sw.show_schedule(pwd, user)
|
||||||
st.show_schedule()
|
st.show_schedule(pwd, user)
|
||||||
gr.show_grades(pwd, user)
|
gr.show_grades(pwd, user)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
|
|||||||
Reference in New Issue
Block a user