bk/bk.nim

106 lines
3.1 KiB
Nim
Raw Normal View History

2020-09-04 11:32:45 +02:00
import json, os, times, unicode, uri
import cligen
2020-09-04 13:10:32 +02:00
from bakalari as baka import newBakalari
2020-09-04 11:32:45 +02:00
type
Config = object
website: string
username: string
refreshToken: string
const
defaultConfigFile = getConfigDir() / "bk.json"
2020-09-04 20:43:05 +02:00
dayNames = [
1: "Mon",
2: "Tue",
3: "Wed",
4: "Thu",
5: "Fri",
6: "Sat",
7: "Sun",
]
2020-09-04 11:32:45 +02:00
proc loadConfig(configFile = defaultConfigFile): Config =
2020-09-07 17:18:43 +02:00
try:
defaultConfigFile.readFile.parseJson.to(Config)
except IOError:
stderr.writeLine "Error: Can't read the config file. Are you signed in?"
quit QuitFailure
except JsonParsingError, JsonKindError:
stderr.writeLine "Error: Invalid config file. Make sure it's in JSON format."
quit QuitFailure
except KeyError:
stderr.writeLine "Error: The config file doesn't contain all necessary fields."
quit QuitFailure
2020-09-04 11:32:45 +02:00
proc saveConfig(configFile = defaultConfigFile, config: Config) =
2020-09-07 17:18:43 +02:00
try:
configFile.writeFile((%*config).pretty)
except IOError:
stderr.writeLine "Error: Can't write the config file."
quit QuitFailure
2020-09-04 11:32:45 +02:00
proc signin(
website: string,
username: string,
password: string,
configFile = defaultConfigFile,
) =
2020-09-04 13:10:32 +02:00
## sign in to Bakaláři and save the credentials
2020-09-04 11:32:45 +02:00
let
bakalari = newBakalari(website.parseUri, username, password)
config = Config(
website: website,
username: username,
refreshToken: bakalari.refreshToken,
)
configFile.saveConfig(config)
2020-09-04 13:10:32 +02:00
proc timetable(
2020-09-04 11:32:45 +02:00
configFile = defaultConfigFile,
date = "",
oneDay = false,
permanent = false,
) =
2020-09-04 13:10:32 +02:00
## display the timetable (for the current week by default)
2020-09-04 11:32:45 +02:00
var config = configFile.loadConfig
let bakalari = newBakalari(config.website.parseUri, config.refreshToken)
config.refreshToken = bakalari.refreshToken
configFile.saveConfig(config)
2020-09-04 13:10:32 +02:00
let timetable = baka.timetable(bakalari, permanent)
2020-09-04 11:32:45 +02:00
for day in timetable.days:
2020-09-04 20:43:05 +02:00
stdout.writeLine dayNames[day.dayOfWeek] & " " & day.date.format("yyyyMMdd")
2020-09-04 11:32:45 +02:00
for lesson in day.lessons:
var line = ""
line &= lesson.hour.number
line &= ". "
line &= lesson.hour.beginTime.align(5, '0'.Rune)
line &= "-"
line &= lesson.hour.endTime.align(5, '0'.Rune)
line &= " "
line &= lesson.subject.abbrev.align(4)
line &= " "
line &= lesson.teacher.abbrev.align(4)
line &= " "
line &= lesson.room.abbrev.align(4)
stdout.writeLine line
2020-09-04 13:10:32 +02:00
2020-09-07 17:18:43 +02:00
try:
dispatchMulti(
[signin, help = {
"website": "the URL for Bakaláři (e.g. https://bakalari.myschool.cz)",
"username": "your username",
"password": "your password (will not be stored anywhere)",
"config-file": "where to store the credentials",
}],
[timetable, help = {
"config-file": "where the credentials are stored",
"date": "any date inside the week you want to display, in YYYYMMDD format (defaults to today)",
"one-day": "display only the specified day",
"permanent": "display the permanent timetable",
}],
)
except OSError:
stderr.writeLine "Error: Generic OS error. Check your internet connection."
quit QuitFailure