-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlecture_summarizer.py
177 lines (133 loc) · 5.34 KB
/
lecture_summarizer.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import requests
import argparse
from typing import List
from abc import abstractmethod
import json
class RequestProcessor(object):
def __init__(self, args):
self.args = args
self.base_url = 'http://%s' % args.base_path
def validate_args_all_of(self, required_items: List[str]):
missing_args = [item for item in required_items if getattr(self.args, item) is None]
if len(missing_args) > 0:
raise RuntimeError('There were missing arguments for this command: ', ' '.join(missing_args))
def validate_args_any_of(self, item_list: List[str]):
if not any(getattr(self.args, x) is not None for x in item_list):
raise RuntimeError("Need at least one item in list: %s" % ','.join(item_list))
def run_post(self, url, body):
req = requests.post(url, json.dumps(body), headers = {
'Content-Type': 'application/json'
})
if req.status_code > 200:
raise RuntimeError(req.json())
print(req.json())
def run_get(self, url):
req = requests.get(url, headers = {
'Content-Type': 'application/json'
})
if req.status_code > 200:
raise RuntimeError(req.json())
print(req.json())
def build_params(self, arg_list):
start = '?'
for name, arg in arg_list:
if arg:
to_add = ('&%s=%s' if start != '?' else '%s=%s') % (name, arg)
start += to_add
return start if start != '?' else ''
@abstractmethod
def run(self):
raise NotImplementedError()
def __call__(self, *args, **kwargs):
self.run()
class CreateLecture(RequestProcessor):
def __init__(self, args):
super(CreateLecture, self).__init__(args)
def __get_lecture_content(self):
with open(self.args.path) as d:
all_data = d.read()
url = self.base_url + '/udacity'
req = requests.post(url, all_data)
if req.status_code > 200:
raise RuntimeError("Invalid request to service with file given")
return req.json()['paragraph']
def __create_body(self, to_upload):
return {
'course': self.args.course,
'name': self.args.name,
'content': to_upload
}
def run(self):
self.validate_args_all_of(['path', 'course', 'name'])
to_upload = self.__get_lecture_content()
body = self.__create_body(to_upload)
url = self.base_url + '/lectures'
self.run_post(url, body)
class GetLectures(RequestProcessor):
def __init__(self, args):
super(GetLectures, self).__init__(args)
def __build_url(self):
if self.args.lecture_id:
return '/%s' % self.args.lecture_id
return self.build_params([('course', self.args.course), ('name', self.args.name)])
def run(self):
url = self.base_url + '/lectures'
url += self.__build_url()
self.run_get(url)
class CreateSummary(RequestProcessor):
def __init__(self, args):
super(CreateSummary, self).__init__(args)
def __build_body(self):
return {
'name': self.args.name,
'course': self.args.course,
'ratio': float(self.args.ratio)
}
def run(self):
self.validate_args_all_of(['lecture_id', 'name', 'ratio'])
body = self.__build_body()
url = '%s/lectures/%s/summaries' % (self.base_url, self.args.lecture_id)
self.run_post(url, body)
class GetSummaries(RequestProcessor):
def __init__(self, args):
super(GetSummaries, self).__init__(args)
def __build_url(self):
if self.args.summary_id:
return '/%s' % self.args.summary_id
return self.build_params([('name', self.args.name)])
def run(self):
self.validate_args_all_of(['lecture_id'])
url = self.base_url + '/lectures/%s/summaries' % self.args.lecture_id
url += self.__build_url()
self.run_get(url)
class DeleteSummary(RequestProcessor):
def __init__(self, args):
super(DeleteSummary, self).__init__(args)
def run(self):
self.validate_args_all_of(['lecture_id', 'summary_id'])
url = '%s/lectures/%s/summaries/%s' % (self.base_url, self.args.lecture_id, self.args.summary_id)
req = requests.delete(url)
if req.status_code > 200:
raise RuntimeError(req.json())
print(req.json())
factory = {
'create-lecture': CreateLecture,
'get-lectures': GetLectures,
'create-summary': CreateSummary,
'get-summaries': GetSummaries,
'delete-summary': DeleteSummary
}
def run():
parser = argparse.ArgumentParser(description='Process and summarize lectures')
parser.add_argument('action')
parser.add_argument('-path', dest='path', default=None, help='File path of lecture')
parser.add_argument('-course', dest='course', default=None, help='')
parser.add_argument('-name', dest='name', default=None, help='')
parser.add_argument('-lecture-id', dest='lecture_id', default=None, help='')
parser.add_argument('-ratio', dest='ratio', help='', default=None)
parser.add_argument('-summary-id', dest='summary_id', default=None, help='')
parser.add_argument('-base-path', dest='base_path', default='54.85.20.109:5000', help='')
args = parser.parse_args()
factory[args.action](args)()
if __name__ == '__main__':
run()