-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcal_service.py
146 lines (120 loc) · 5.61 KB
/
gcal_service.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
140
141
142
143
144
145
146
from utils import text_contains, text_contains_or
import json
import requests
import sys
import urllib3
requests.packages.urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class GcalService:
__gcal_endpoint = 'https://script.google.com/macros/s/AKfycbwXSY3FWlFOIUXiC7wpR--3aVfLbRUKoQBVcL9GtrvA2f2t1vFEOPpUASOLgUn5fTNl/exec' # noqa
# public methods (ORDER ALPHABETICALLY)
def get_events(text_message, group_id, start_date=None, days=None):
if text_message is None or group_id is None:
raise ValueError('text_message or group_id cannot be None')
year = GcalService.__get_student_year_from_text(text_message)
major = GcalService.__get_student_major_from_text(text_message)
param = dict()
param['textMessage'] = text_message
param['groupId'] = group_id
if (start_date is not None):
param['startDate'] = start_date.strftime('%Y-%m-%d')
if (days is not None):
param['days'] = days
data = dict()
data['action'] = 'getEventsByDuration'
data['param'] = json.dumps(param)
r = requests.get(GcalService.__gcal_endpoint, params=data, verify=False)
if (r.status_code != 200):
raise Exception('request return with status code {}'.format(r.status_code))
try:
content = str(r.content, 'utf-8')
result = json.loads(content)
except json.decoder.JSONDecodeError:
raise Exception("Can't decode JSON: " + content)
if (result.get('code') != 'SUCCESS'):
raise Exception(result.get('code') + ' is not SUCCESS')
events = result.get('result')
if year is not None:
events = GcalService.__event_filter_assignment_by_year(events, year)
if major is not None:
events = GcalService.__event_filter_assignment_by_major(events, major)
context = GcalService.__get_context_from_text(text_message)
events = GcalService.__event_filter_by_context(events, context)
return events
# private helper methods (ORDER ALPHABETICALLY)
def __event_filter_assignment_by_major(events, major):
filtered = []
for event in events:
if GcalService.__event_is_assignment_for_major(event, major):
filtered.append(event)
return filtered
def __event_filter_assignment_by_year(events, year):
filtered = []
for event in events:
if GcalService.__event_is_assignment_for_year(event, year):
filtered.append(event)
return filtered
def __event_filter_by_context(events, context):
filtered = []
keywordUjian = ["UAS", "UTS"]
keywordTA = ["Sidang", "Seminar"]
for event in events:
eventName = event.get('name')
isEventUjian = text_contains_or(eventName, keywordUjian)
isEventTA = text_contains_or(eventName,keywordTA)
if context == "ujian" and isEventUjian:
filtered.append(event)
elif context == "TA" and isEventTA:
filtered.append(event)
elif context == "deadline" and (not isEventUjian and not isEventTA):
filtered.append(event)
return filtered
def __event_is_assignment_for_major(event, major):
return event['name'].find('[' + major) != -1
def __event_is_assignment_for_year(event, year):
return event['name'].find(str(year) + ']') != -1
def __get_context_from_text(text_message):
'''
context is "ujian" or "deadline" or "TA"
'''
if text_message.find("ujian") != -1:
return "ujian"
elif text_message.find("seminar") != -1 or text_message.find("sidang") != -1:
return "TA"
return "deadline"
def __get_student_major_from_text(text_message):
major = None
if text_contains(text_message, ['if']):
major = 'IF'
elif text_contains(text_message, ['sti']):
major = 'STI'
return major
def __get_student_year_from_text(text_message):
year = None
if (text_contains(text_message, ['untuk', 'bit'], series=True)
or text_contains(text_message, ['untuk', '16'], series=True)):
year = 16
elif (text_contains(text_message, ['untuk', 'unix'], series=True)
or text_contains(text_message, ['untuk', '17'], series=True)):
year = 17
elif (text_contains(text_message, ['untuk', 'decrypt'], series=True)
or text_contains(text_message, ['untuk', '18'], series=True)):
year = 18
elif (text_contains(text_message, ['untuk', 'async'], series=True)
or text_contains(text_message, ['untuk', '19'], series=True)):
year = 19
elif (text_contains(text_message, ['untuk', 'init'], series=True)
or text_contains(text_message, ['untuk', '20'], series=True)):
year = 20
elif (text_contains(text_message, ['untuk', 'sudo'], series=True)
or text_contains(text_message, ['untuk', '21'], series=True)):
year = 21
elif (text_contains(text_message, ['untuk', 'byte'], series=True)
or text_contains(text_message, ['untuk', '22'], series=True)):
year = 22
return year
if __name__ == '__main__':
days = None if len(sys.argv) < 2 else int(sys.argv[1])
start_date = None if len(sys.argv) < 3 else sys.argv[2]
text_message = "ada seminar apa aja minggu ini untuk init"
group_id = "group_id"
print(GcalService.get_events(text_message, group_id, start_date=start_date, days=days))