-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstp
executable file
·63 lines (51 loc) · 1.87 KB
/
stp
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
#!/usr/bin/python
import requests
import sys
import json
from dateutil.parser import parse
from prettytable import PrettyTable
import datetime
def extract_arguments():
arguments = sys.argv
student_name = "zulfileo"
days_to_add = 0
if len(arguments) == 2:
days_to_add = arguments[1]
return student_name, days_to_add
def send_request(student_name, days_to_add):
url = 'https://api.apps.engineering.zhaw.ch/v1/schedules/students/' + student_name
wanted_date = datetime.datetime.now() + datetime.timedelta(days=int(days_to_add))
date_string = wanted_date.strftime('%Y-%m-%d')
params = {'days': 1, 'startingAt': date_string}
headers = {'Accept': 'application/json',
'user-agent': 'student/stundenplan (https://crap.com)'}
return requests.get(url, params=params, headers=headers)
def get_courses(response):
courses = []
json_response = json.loads(response.text)
events = json_response['days'][0]['events']
for event in events:
course = []
course.append(parse(event["startTime"]).strftime('%H:%M'))
course.append(parse(event["endTime"]).strftime('%H:%M'))
course.append(event["name"])
realization = event["eventRealizations"]
if realization is not None:
course.append(realization[0]["room"]["name"])
courses.append(course)
return courses
def print_courses(courses):
table = PrettyTable(["Start", "End", "Course", "Room"])
for event in courses:
start_time = event[0]
end_time = event[1]
course = event[2]
room = ""
if len(event) == 4:
room = event[3]
table.add_row([start_time, end_time, course, room])
print table
student_name, days_to_add = extract_arguments()
response = send_request(student_name, days_to_add)
found_courses = get_courses(response)
print_courses(found_courses)