-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (64 loc) · 2.84 KB
/
main.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
import sys
import bakalari
import timetable_cal
import re
from dotenv import set_key, load_dotenv
import os
from time import sleep
import yaml
school_url, username, password, access_token, refresh_token = (None, None, None, None, None)
def first_login():
global school_url, username, password, access_token, refresh_token
# Load/create configuration
if os.path.isfile(".env"):
load_dotenv(".env")
school_url = os.getenv("SCHOOL_URL")
access_token = os.getenv("ACCESS_TOKEN")
refresh_token = os.getenv("REFRESH_TOKEN")
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
else:
school_url = input("Prosím zadej URL své školy - Pokud je přihlášení do Bakalářů na adrese "
"https://www.example.com/next/login.aspx, pak bude požadovaná adresa "
"https://www.example.com: ").strip("/")
if re.match(
r"(?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)",
school_url):
pass
else:
sys.exit("Zadej URL adresu ve správném formátu!")
username = input("Prosím zadej své uživ. jméno: ")
password = input("Prosím zadej své heslo: ")
access_token, refresh_token = bakalari.get_token(school_url, username, password)
with open(".env", "w") as f:
f.write(f"SCHOOL_URL={school_url}\n")
f.write(f"ACCESS_TOKEN={access_token}\n")
f.write(f"REFRESH_TOKEN={refresh_token}\n")
f.write(f"USERNAME={username}\n")
f.write(f"PASSWORD={password}\n")
f.close()
def refresh_tokens():
global school_url, username, password, access_token, refresh_token
access_token, refresh_token = bakalari.get_token(school_url, username, password, refresh_token)
set_key(".env", "ACCESS_TOKEN", access_token)
set_key(".env", "REFRESH_TOKEN", refresh_token)
def main():
global school_url, username, password, access_token, refresh_token
with open('config.yml', 'r') as config_file:
config = yaml.safe_load(config_file)
first_login()
while True:
if bakalari.get_timetable(school_url, access_token) == "401 Error":
refresh_tokens()
bakalari.get_timetable(school_url, access_token)
if config['download_future']:
if bakalari.get_timetable(school_url, access_token, True) == "401 Error":
refresh_tokens()
bakalari.get_timetable(school_url, access_token, True)
timetable_cal.parse_json_timetable('timetable.json')
if config['download_future']:
timetable_cal.parse_json_timetable('timetable_future.json')
timetable_cal.create_ics()
sleep(10 * 60)
if __name__ == '__main__':
main()