-
Notifications
You must be signed in to change notification settings - Fork 8
/
app-cli.py
293 lines (228 loc) · 10.3 KB
/
app-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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
import argparse
import base64
import json
import os
import re
import sys
from functools import partial
from typing import List
from lib.captions import Captions
from lib.core.impartus import Impartus
from lib.utils import Utils
from lib.variables import Variables
def validate_numeric(id_value):
if re.match(r'[0-9]+', str(id_value)):
return id_value
raise argparse.ArgumentTypeError("Bad numeric value {}".format(id_value))
def validate_email(user_email: str):
if '@' in user_email:
return user_email
raise argparse.ArgumentTypeError("Bad user/email {}".format(user_email))
def validate_jwt_token(jwt_token: str):
token_parts = '.'.split(jwt_token)
if len(token_parts) != 3:
raise argparse.ArgumentTypeError("Bad jwt token {}".format(token))
if base64.b64decode(token_parts[0]) and base64.b64decode(token_parts[1]):
return token
raise argparse.ArgumentTypeError("jwt token cannot be decoded.")
def error(message):
print('ERROR: {}'.format(message))
def login():
if impartus.login():
print(impartus.token)
else:
error("Unable to authenticate.")
def subjects():
subject_list = impartus.get_subjects()
if vars(app_args).get('dir'):
os.makedirs(app_args.dir, exist_ok=True)
for subject_data in subject_list:
filename = re.sub(r'[^a-zA-Z0-9._-]+', '', subject_data.get('subjectName'))
with open('{}/{}.json'.format(app_args.dir, filename), 'w') as fh:
json.dump(subject_data, fh, indent=4)
else:
print(json.dumps(subject_list, indent=4))
def lectures():
with open(app_args.json, 'r') as fh:
subject_json = json.load(fh)
videos_list = impartus.get_lecture_videos([subject_json])
if vars(app_args).get('dir'):
os.makedirs(app_args.dir, exist_ok=True)
for video_metadata in videos_list:
filename = os.path.basename(Utils.get_mkv_path(video_metadata[1]))
with open('{}/{}.json'.format(app_args.dir, filename), 'w') as fh:
json.dump(video_metadata[1], fh, indent=4)
else:
print(json.dumps([x[1] for x in videos_list], indent=4))
def documents():
with open(app_args.json, 'r') as fh:
subject_json = json.load(fh)
_, docs_list = next(impartus.get_slides([subject_json]))
if vars(app_args).get('dir'):
os.makedirs(app_args.dir, exist_ok=True)
for i, doc_metadata in enumerate(docs_list):
filename = '{:02d}-{}'.format(i, os.path.basename(doc_metadata.get('filePath')))
with open('{}/{}.json'.format(app_args.dir, filename), 'w') as fh:
json.dump(doc_metadata, fh, indent=4)
else:
print(json.dumps(docs_list, indent=4))
def progress_log(value: int):
if value % 10 == 0:
print("^", end='')
else:
print(".", end='')
if value == 100:
print("\n")
sys.stdout.flush()
def download_video():
with open(app_args.json, 'r') as fh:
video_metadata = json.load(fh)
callback = partial(progress_log)
mkv_filepath = '{}/{}'.format(app_args.dir, os.path.basename(Utils.get_mkv_path(video_metadata)))
impartus.process_video(video_metadata, mkv_filepath, None, None, callback, app_args.quality)
def download_chat():
with open(app_args.json, 'r') as fh:
video_metadata = json.load(fh)
vtt_filename = os.path.basename(Utils.get_captions_path(video_metadata))
vtt_filepath = '{}/{}'.format(app_args.dir, vtt_filename)
chats = impartus.get_chats(video_metadata)
video_id = video_metadata['ttid'] if video_metadata.get('ttid') else video_metadata['fcid']
Captions.save_as_captions(video_id, video_metadata, chats, vtt_filepath)
def download_document():
with open(app_args.json, 'r') as fh:
document_metadata = json.load(fh)
doc_filename = os.path.basename(Utils.get_documents_path(document_metadata))
doc_filepath = '{}/{}'.format(app_args.dir, doc_filename)
impartus.download_slides(document_metadata.get('filePath'), doc_filepath)
def download():
if app_args.subcommand == 'video':
download_video()
elif app_args.subcommand == 'chat':
download_chat()
elif app_args.subcommand == 'document':
download_document()
else:
parser.print_help()
app = sys.argv[0]
epilog_login = """
Returns user JWT token.
example:
./{app} login
""".format(app=app)
epiog_subjects = """
Returns a json list containing subject data.
example:
./{app} subjects [-o dir]
""".format(app=app)
epilog_lectures = """
Returns a json list containing lecture videos info.
example:
./{app} lectures -j subject.json [-o dir]
""".format(app=app)
epilog_documents = """
Returns a json list containing backpack documents info.
example:
./{app} documents -j subject.json [-o dir]
""".format(app=app)
epilog_download_video = """
Download a lecture video
example:
./{app} download video -j lecture.json -o dir
"""
epilog_download_chat = """
Download chat for a lecture
example:
./{app} download chat -j lecture.json -o dir
"""
epilog_download_document = """
Download a document.
example:
./{app} download document -j document.json -o dir
""".format(app=app)
epilog = """
The CLI requires that the following variables are exported in the environment:
IMPARTUS_USER
IMPARTUS_PASS
Once logged in, users would need to export
IMPARTUS_TOKEN
IMPARTUS_URL may also be exported to use a server other than https://a.impartus.com
"""
def _parse_args(args: List):
subparsers = parser.add_subparsers(dest="command", title='subcommands')
login_parser = subparsers.add_parser('login',
help="Login to Impartus.",
epilog=epilog_login,
formatter_class=argparse.RawDescriptionHelpFormatter)
subject_parser = subparsers.add_parser('subjects',
help="Get list of subjects subscribed by the user.",
epilog=epiog_subjects,
formatter_class=argparse.RawDescriptionHelpFormatter)
subject_parser.add_argument('-o', '--dir', help='Directory to save subjects json metadata.')
lecture_parser = subparsers.add_parser('lectures',
help="Get lecture info for a subject.",
epilog=epilog_lectures,
formatter_class=argparse.RawDescriptionHelpFormatter)
lecture_parser.add_argument('-j', '--json', required=True, help='Subject json file.')
lecture_parser.add_argument('-o', '--dir', help='Directory to save lecture json metadata.')
document_parser = subparsers.add_parser('documents',
help="Get backpack documents info for a subject.",
epilog=epilog_lectures,
formatter_class=argparse.RawDescriptionHelpFormatter)
document_parser.add_argument('-j', '--json', required=True, help='Subject json file.')
document_parser.add_argument('-o', '--dir', help='Directory to save document json metadata.')
download_parser = subparsers.add_parser('download',
help="Download a video/chat/document.",
formatter_class=argparse.RawDescriptionHelpFormatter)
download_subparsers = download_parser.add_subparsers(dest="subcommand", title='subcommands')
video_dl_parser = download_subparsers.add_parser('video',
help="Download lecture video.",
epilog=epilog_download_video,
formatter_class=argparse.RawDescriptionHelpFormatter)
video_dl_parser.add_argument('-j', '--json', required=True, help='Lecture json.')
video_dl_parser.add_argument('-q', '--quality', default='highest', help='Video quality (default=highest).')
video_dl_parser.add_argument('-o', '--dir', required=True, help='Output mkv file')
chat_dl_parser = download_subparsers.add_parser('chat',
help="Download lecture chat.",
epilog=epilog_download_chat,
formatter_class=argparse.RawDescriptionHelpFormatter)
chat_dl_parser.add_argument('-j', '--json', required=True, help='Lecture json.')
chat_dl_parser.add_argument('-o', '--dir', required=True, help='Output vtt file')
document_dl_parser = download_subparsers.add_parser('document',
help="Download backpack document.",
epilog=epilog_download_document,
formatter_class=argparse.RawDescriptionHelpFormatter)
document_dl_parser.add_argument('-j', '--json', required=True, help='Document json file.')
document_dl_parser.add_argument('-o', '--dir', required=True, help='Output file')
return parser.parse_args(args)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Impartus CLI.',
epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter)
app_args = _parse_args(sys.argv[1:])
impartus_url = os.environ.get('IMPARTUS_URL')
if not impartus_url:
impartus_url = 'https://a.impartus.com'
Variables().set_login_url(impartus_url)
if app_args.command == 'login':
username = os.environ.get('IMPARTUS_USER')
password = os.environ.get('IMPARTUS_PASS')
if not username:
error("IMPARTUS_USER needed.")
sys.exit(1)
if not password:
error("IMPARTUS_PASS needed.")
sys.exit(1)
Variables().set_login_email(username)
Variables().set_login_password(password)
impartus = Impartus()
else:
token = os.environ.get('IMPARTUS_TOKEN')
if not token:
error("IMPARTUS_TOKEN needed.")
sys.exit(1)
impartus = Impartus(token)
if vars(app_args).get('command'):
globals()[app_args.command]()
else:
parser.print_help()