-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.py
75 lines (68 loc) · 2.4 KB
/
cli.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
#!/usr/bin/env python3
import argparse
import asyncio
import datetime
import sys
import cqupt_ics
import providers
parser = argparse.ArgumentParser(prog="CQUPT-ICS", description="Generate ICS file schedule of CQUPT")
parser.add_argument("student_id", metavar="student_id",
type=str, help="Your student ID number")
parser.add_argument("-o", "--output", type=str, help="Specify output file", default="-")
parser.add_argument("--disable-exam", help="Disable exam information in ICS file", default=False, action='store_true')
parser.add_argument("--disable-class", help="Disable class information in ICS file", default=False, action='store_true')
parser.add_argument("--disable-geo", help="Disable location data in ICS File", default=False, action='store_true')
parser.add_argument("--start-day", type=str, help="Specify the first Monday the term starts, in format YYYY-MM-dd. E.g: 1970-01-01", default="1970-01-01")
parser.add_argument("--provider", type=str, help="Specify data provider")
async def main():
config = parser.parse_args()
writer = sys.stdout.write
provider_list = []
err = None
f = None
if config.output != "-":
try:
f = open(config.output, "w")
writer = f.write
except:
pass
mode = cqupt_ics.ICS_ALL
if config.disable_exam:
mode ^= cqupt_ics.ICS_EXAM
if config.disable_class:
mode ^= cqupt_ics.ICS_CLASS
start_day_tuple = ()
for i in config.start_day.split('-'):
start_day_tuple += (int(i),)
try:
if config.provider:
provider_list.append(providers.providers[config.provider])
else:
for key in providers.providers:
provider_list.append(providers.providers[key])
except:
sys.stderr.write("Invalid provider: " + config.provider)
header_written = False
for provider in provider_list:
try:
if not header_written:
writer(cqupt_ics.ICS_HEADER)
header_written = True
async for event in cqupt_ics.get_events(student_id=config.student_id, mode=mode,
enable_geo=(not config.disable_geo),
provider=provider,
start_day=datetime.datetime(start_day_tuple[0], start_day_tuple[1], start_day_tuple[2])):
writer(event)
writer(cqupt_ics.ICS_FOOTER)
err = None
break
except Exception as e:
err = e
continue
if err:
sys.stderr.write("Exception occured: " + str(err.args[0]))
if f:
f.close()
if __name__ == "__main__":
mainloop = asyncio.get_event_loop_policy().get_event_loop()
mainloop.run_until_complete(main())