-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
139 lines (130 loc) · 3.35 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import print_function
from cmd import Cmd
import getpass
import six
import os
try:
from bakalari.bakalari import BakalariAPI
except ImportError:
import sys
try:
sys.path.insert(0, os.path.abspath("bakalari"))
from bakalari.bakalari import BakalariAPI
except ImportError:
print("*ERROR* `bakalari` module is missing")
sys.exit(1)
class MainPrompt(Cmd):
username = ""
password = ""
loggedIn = False
url = ""
api = None
marks = None
@staticmethod
def parse_mark(mark):
if isinstance(mark, six.string_types):
if len(mark) == 0:
return float(0)
part = 0
if mark.endswith('-'):
part = 0.5
mark = mark[0:-1]
try:
fMark = float(mark)
except ValueError:
return float(0)
return fMark + part
else:
return float(0)
def get_api(self):
if not self.loggedIn:
print("You have to be logged in!")
self.onecmd("login")
if self.api == None:
self.api = BakalariAPI(self.username, self.password, self.url)
self.api.login()
return self.api
def do_login(self, args):
"""Log in to be able to make API calls (which is necessary to view the marks)"""
eUrl = None
eUsr = None
ePas = None
if "bakalariUrl" in os.environ:
eUrl = os.environ["bakalariUrl"]
if "bakalariUsr" in os.environ:
eUsr = os.environ["bakalariUsr"]
if "bakalariPas" in os.environ:
ePas = os.environ["bakalariPas"]
try:
if eUrl == None:
self.url = raw_input("url: ")
else:
self.url = raw_input("url [%s]: " % (eUrl))
if self.url == "":
self.url = eUrl
if eUsr == None:
self.username = raw_input("username: ")
else:
self.username = raw_input("username [%s]: " % (eUsr))
if self.username == "":
self.username = eUsr
except NameError:
if eUrl == None:
self.url = input("url: ")
else:
self.url = input("url [%s]: " % (eUrl))
if self.url == "":
self.url = eUrl
if eUsr == None:
self.username = input("username: ")
else:
self.username = input("username [%s]: " % (eUsr))
if self.username == "":
self.username = eUsr
if ePas == None:
self.password = getpass.getpass("password: ")
else:
self.password = getpass.getpass("password [<password>]: ")
if self.password == "":
self.password = ePas
self.api = None
self.loggedIn = True
def do_update(self, args):
"""Flush the cache and download new marks"""
api = self.get_api()
self.marks = api.znamky()["predmet"]
def do_all(self, args):
"""Print summary of all marks"""
if self.marks == None:
print("Updating...")
self.onecmd("update")
for subject in self.marks:
print(subject["zkratka"].upper(), end=": ")
wAvgSum = 0
wSum = 0
if subject["znamky"] != None:
marks = subject["znamky"]["znamka"]
for mark in marks:
value = MainPrompt.parse_mark(mark["znamka"])
if value == 0:
weight = 0
else:
weight = int(mark["vaha"])
wAvgSum += value * weight
wSum += weight
print("%s(%d)" % (mark["znamka"], weight), end=" ")
print("|", end=" ")
if "ctvrt" in subject and subject["ctvrt"] != None:
print("(%s)" % (subject["ctvrt"]), end="")
if wSum == 0:
wAvg = 0
else:
wAvg = round(wAvgSum / wSum, 2)
print(" %.2f" % (wAvg))
if __name__ == '__main__':
prompt = MainPrompt()
prompt.prompt = '> '
try:
prompt.cmdloop()
except KeyboardInterrupt:
print("") # do not print the stacktrace, just a newline