-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.py
516 lines (429 loc) · 23.9 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import json
import os
import sys
import requests
import argparse
import subprocess
from pathvalidate import sanitize_filename
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn
from rich.live import Live
from rich.tree import Tree
from rich.text import Text
from rich import print as rprint
import re
import http.cookiejar as cookielib
from concurrent.futures import ThreadPoolExecutor, as_completed
from constants import *
from utils.process_m3u8 import download_and_merge_m3u8
from utils.process_mpd import download_and_merge_mpd
from utils.process_captions import download_captions
from utils.process_assets import download_supplementary_assets
from utils.process_articles import download_article
from utils.process_mp4 import download_mp4
console = Console()
class Udemy:
def __init__(self):
global cookie_jar
try:
cookie_jar = cookielib.MozillaCookieJar(cookie_path)
cookie_jar.load()
except Exception as e:
logger.critical(f"The provided cookie file could not be read or is incorrectly formatted. Please ensure the file is in the correct format and contains valid authentication cookies.")
sys.exit(1)
def request(self, url):
try:
response = requests.get(url, cookies=cookie_jar, stream=True)
return response
except Exception as e:
logger.critical(f"There was a problem reaching the Udemy server. This could be due to network issues, an invalid URL, or Udemy being temporarily unavailable.")
def extract_course_id(self, course_url):
with Loader(f"Fetching course ID"):
response = self.request(course_url)
content_str = response.content.decode('utf-8')
meta_match = re.search(r'<meta\s+property="og:image"\s+content="([^"]+)"', content_str)
if meta_match:
url = meta_match.group(1)
number_match = re.search(r'/(\d+)_', url)
if number_match:
number = number_match.group(1)
logger.info(f"Course ID Extracted: {number}")
return number
else:
logger.critical("Unable to retrieve a valid course ID from the provided course URL. Please check the course URL or try with --id.")
sys.exit(1)
else:
logger.critical("Unable to retrieve a valid course ID from the provided course URL. Please check the course URL or try with --id")
sys.exit(1)
def fetch_course(self, course_id):
try:
response = self.request(COURSE_URL.format(course_id=course_id)).json()
if response.get('detail') == 'Not found.':
logger.critical("The course could not be found with the provided ID or URL. Please verify the course ID/URL and ensure that it is publicly accessible or you have the necessary permissions.")
sys.exit(1)
return response
except Exception as e:
logger.critical(f"Unable to retrieve the course details: {e}")
sys.exit(1)
def fetch_course_curriculum(self, course_id):
all_results = []
url = CURRICULUM_URL.format(course_id=course_id)
total_count = 0
logger.info("Fetching course curriculum. This may take a while")
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3}%"),
transient=True
) as progress:
task = progress.add_task(description="Fetching Course Curriculum", total=total_count)
while url:
response = self.request(url).json()
if response.get('detail') == 'You do not have permission to perform this action.':
progress.console.log("[red]The course was found, but the curriculum (lectures and materials) could not be retrieved. This could be due to API issues, restrictions on the course, or a malformed course structure.[/red]")
sys.exit(1)
if response.get('detail') == 'Not found.':
progress.console.log("[red]The course was found, but the curriculum (lectures and materials) could not be retrieved. This could be due to API issues, restrictions on the course, or a malformed course structure.[/red]")
sys.exit(1)
if total_count == 0:
total_count = response.get('count', 0)
progress.update(task, total=total_count)
results = response.get('results', [])
all_results.extend(results)
progress.update(task, completed=len(all_results))
url = response.get('next')
progress.update(task_id = task, description="Fetched Course Curriculum", total=total_count)
return self.organize_curriculum(all_results)
def organize_curriculum(self, results):
curriculum = []
current_chapter = None
total_lectures = 0
for item in results:
if item['_class'] == 'chapter':
current_chapter = {
'id': item['id'],
'title': item['title'],
'is_published': item['is_published'],
'children': []
}
curriculum.append(current_chapter)
elif item['_class'] == 'lecture':
if current_chapter is not None:
current_chapter['children'].append(item)
if item['_class'] == 'lecture':
total_lectures += 1
else:
logger.warning("Found lecture without a parent chapter.")
num_chapters = len(curriculum)
logger.info(f"Discovered Chapter(s): {num_chapters}")
logger.info(f"Discovered Lectures(s): {total_lectures}")
return curriculum
def build_curriculum_tree(self, data, tree, index=1):
for i, item in enumerate(data, start=index):
if 'title' in item:
title = f"{i:02d}. {item['title']}"
if '_class' in item and item['_class'] == 'lecture':
time_estimation = item.get('asset', {}).get('time_estimation')
if time_estimation:
time_str = format_time(time_estimation)
title += f" ({time_str})"
node_text = Text(title, style="cyan")
else:
node_text = Text(title, style="magenta")
node = tree.add(node_text)
if 'children' in item:
self.build_curriculum_tree(item['children'], node, index=1)
def fetch_lecture_info(self, course_id, lecture_id):
try:
return self.request(LECTURE_URL.format(course_id=course_id, lecture_id=lecture_id)).json()
except Exception as e:
logger.critical(f"Failed to fetch lecture info: {e}")
sys.exit(1)
def create_directory(self, path):
try:
os.makedirs(path)
except FileExistsError:
pass
except Exception as e:
logger.error(f"Failed to create directory \"{path}\": {e}")
sys.exit(1)
def download_lecture(self, course_id, lecture, lect_info, temp_folder_path, lindex, folder_path, task_id, progress):
if not skip_captions and len(lect_info["asset"]["captions"]) > 0:
download_captions(lect_info["asset"]["captions"], folder_path, f"{lindex}. {sanitize_filename(lecture['title'])}", captions, convert_to_srt)
if not skip_assets and len(lecture["supplementary_assets"]) > 0:
download_supplementary_assets(self, lecture["supplementary_assets"], folder_path, course_id, lect_info["id"])
if not skip_lectures and lect_info['asset']['asset_type'] == "Video":
mpd_url = next((item['src'] for item in lect_info['asset']['media_sources'] if item['type'] == "application/dash+xml"), None)
mp4_url = next((item['src'] for item in lect_info['asset']['media_sources'] if item['type'] == "video/mp4"), None)
m3u8_url = next((item['src'] for item in lect_info['asset']['media_sources'] if item['type'] == "application/x-mpegURL"), None)
if mpd_url is None:
if m3u8_url is None:
if mp4_url is None:
logger.error(f"This lecture appears to be served in different format. We currently do not support downloading this format. Please create an issue on GitHub if you need this feature.")
else:
download_mp4(mp4_url, temp_folder_path, f"{lindex}. {sanitize_filename(lecture['title'])}", task_id, progress)
else:
download_and_merge_m3u8(m3u8_url, temp_folder_path, f"{lindex}. {sanitize_filename(lecture['title'])}", task_id, progress)
else:
if key is None:
logger.warning("The video appears to be DRM-protected, and it may not play without a valid Widevine decryption key.")
download_and_merge_mpd(mpd_url, temp_folder_path, f"{lindex}. {sanitize_filename(lecture['title'])}", lecture['asset']['time_estimation'], key, task_id, progress)
elif not skip_articles and lect_info['asset']['asset_type'] == "Article":
download_article(self, lect_info['asset'], temp_folder_path, f"{lindex}. {sanitize_filename(lecture['title'])}", task_id, progress)
try:
progress.remove_task(task_id)
except KeyError:
pass
def download_course(self, course_id, curriculum):
progress = Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
ElapsedTimeColumn(),
)
tasks = {}
futures = []
with ThreadPoolExecutor(max_workers=max_concurrent_lectures) as executor, Live(progress, refresh_per_second=10):
task_generator = (
(f"{mindex:02}" if mindex < 10 else f"{mindex}",
chapter,
f"{lindex:02}" if lindex < 10 else f"{lindex}",
lecture)
for mindex, chapter in enumerate(curriculum, start=1)
if is_valid_chapter(mindex, start_chapter, end_chapter)
for lindex, lecture in enumerate(chapter['children'], start=1)
if is_valid_lecture(mindex, lindex, start_chapter, start_lecture, end_chapter, end_lecture)
)
for _ in range(max_concurrent_lectures):
try:
mindex, chapter, lindex, lecture = next(task_generator)
folder_path = os.path.join(COURSE_DIR, f"{mindex}. {remove_emojis_and_binary(sanitize_filename(chapter['title']))}")
temp_folder_path = os.path.join(folder_path, str(lecture['id']))
self.create_directory(temp_folder_path)
lect_info = self.fetch_lecture_info(course_id, lecture['id'])
task_id = progress.add_task(
f"Downloading Lecture: {lecture['title']} ({lindex}/{len(chapter['children'])})",
total=100
)
tasks[task_id] = (lecture, lect_info, temp_folder_path, lindex, folder_path)
future = executor.submit(
self.download_lecture, course_id, lecture, lect_info, temp_folder_path, lindex, folder_path, task_id, progress
)
futures.append((task_id, future))
except StopIteration:
break
while futures:
for future in as_completed(f[1] for f in futures):
task_id = next(task_id for task_id, f in futures if f == future)
future.result()
try:
progress.remove_task(task_id)
except:
pass
futures = [f for f in futures if f[1] != future]
try:
mindex, chapter, lindex, lecture = next(task_generator)
folder_path = os.path.join(COURSE_DIR, f"{mindex}. {sanitize_filename(chapter['title'])}")
temp_folder_path = os.path.join(folder_path, str(lecture['id']))
self.create_directory(temp_folder_path)
lect_info = self.fetch_lecture_info(course_id, lecture['id'])
task_id = progress.add_task(
f"Downloading Lecture: {lecture['title']} ({lindex}/{len(chapter['children'])})",
total=100
)
tasks[task_id] = (lecture, lect_info, temp_folder_path, lindex, folder_path)
future = executor.submit(
self.download_lecture, course_id, lecture, lect_info, temp_folder_path, lindex, folder_path, task_id, progress
)
futures.append((task_id, future))
except StopIteration:
break
def check_prerequisites():
if not cookie_path:
if not os.path.isfile(os.path.join(HOME_DIR, "cookies.txt")):
logger.error(f"Please provide a valid cookie file using the '--cookie' option.")
return False
else:
if not os.path.isfile(cookie_path):
logger.error(f"The provided cookie file path does not exist.")
return False
try:
subprocess.run(["ffmpeg", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
except:
logger.error("ffmpeg is not installed or not found in the system PATH.")
return False
try:
subprocess.run(["n_m3u8dl-re", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
except:
logger.error("Make sure mp4decrypt & n_m3u8dl-re is not installed or not found in the system PATH.")
return False
return True
def main():
try:
global course_url, key, cookie_path, COURSE_DIR, captions, max_concurrent_lectures, skip_captions, skip_assets, skip_lectures, skip_articles, skip_assignments, convert_to_srt, start_chapter, end_chapter, start_lecture, end_lecture
parser = argparse.ArgumentParser(description="Udemy Course Downloader")
parser.add_argument("--id", "-i", type=int, required=False, help="The ID of the Udemy course to download")
parser.add_argument("--url", "-u", type=str, required=False, help="The URL of the Udemy course to download")
parser.add_argument("--key", "-k", type=str, help="Key to decrypt the DRM-protected videos")
parser.add_argument("--cookies", "-c", type=str, default="cookies.txt", help="Path to cookies.txt file")
parser.add_argument("--load", "-l", help="Load course curriculum from file", action=LoadAction, const=True, nargs='?')
parser.add_argument("--save", "-s", help="Save course curriculum to a file", action=LoadAction, const=True, nargs='?')
parser.add_argument("--concurrent", "-cn", type=int, default=4, help="Maximum number of concurrent downloads")
# parser.add_argument("--quality", "-q", type=str, help="Specify the quality of the videos to download.")
parser.add_argument("--start-chapter", type=int, help="Start the download from the specified chapter")
parser.add_argument("--start-lecture", type=int, help="Start the download from the specified lecture")
parser.add_argument("--end-chapter", type=int, help="End the download at the specified chapter")
parser.add_argument("--end-lecture", type=int, help="End the download at the specified lecture")
parser.add_argument("--captions", type=str, help="Specify what captions to download. Separate multiple captions with commas")
parser.add_argument("--srt", help="Convert the captions to srt format", action=LoadAction, const=True, nargs='?')
parser.add_argument("--tree", help="Create a tree view of the course curriculum", action=LoadAction, nargs='?')
parser.add_argument("--skip-captions", type=bool, default=False, help="Skip downloading captions", action=LoadAction, nargs='?')
parser.add_argument("--skip-assets", type=bool, default=False, help="Skip downloading assets", action=LoadAction, nargs='?')
parser.add_argument("--skip-lectures", type=bool, default=False, help="Skip downloading lectures", action=LoadAction, nargs='?')
parser.add_argument("--skip-articles", type=bool, default=False, help="Skip downloading articles", action=LoadAction, nargs='?')
parser.add_argument("--skip-assignments", type=bool, default=False, help="Skip downloading assignments", action=LoadAction, nargs='?')
args = parser.parse_args()
if len(sys.argv) == 1:
print(parser.format_help())
sys.exit(0)
course_url = args.url
key = args.key
if args.concurrent > 25:
logger.warning("The maximum number of concurrent downloads is 25. The provided number of concurrent downloads will be capped to 25.")
max_concurrent_lectures = 25
elif args.concurrent < 1:
logger.warning("The minimum number of concurrent downloads is 1. The provided number of concurrent downloads will be capped to 1.")
max_concurrent_lectures = 1
else:
max_concurrent_lectures = args.concurrent
if not course_url and not args.id:
logger.error("You must provide either the course ID with '--id' or the course URL with '--url' to proceed.")
return
elif course_url and args.id:
logger.warning("Both course ID and URL provided. Prioritizing course ID over URL.")
if key is not None and not ":" in key:
logger.error("The provided Widevine key is either malformed or incorrect. Please check the key and try again.")
return
if args.cookies:
cookie_path = args.cookies
if not check_prerequisites():
return
udemy = Udemy()
if args.id:
course_id = args.id
else:
course_id = udemy.extract_course_id(course_url)
if args.captions:
try:
captions = args.captions.split(",")
except:
logger.error("Invalid captions provided. Captions should be separated by commas.")
else:
captions = ["en_US"]
skip_captions = args.skip_captions
skip_assets = args.skip_assets
skip_lectures = args.skip_lectures
skip_articles = args.skip_articles
skip_assignments = args.skip_assignments
course_info = udemy.fetch_course(course_id)
COURSE_DIR = os.path.join(DOWNLOAD_DIR, remove_emojis_and_binary(sanitize_filename(course_info['title'])))
logger.info(f"Course Title: {course_info['title']}")
udemy.create_directory(os.path.join(COURSE_DIR))
if args.load:
if args.load is True and os.path.isfile(os.path.join(HOME_DIR, "course.json")):
try:
course_curriculum = json.load(open(os.path.join(HOME_DIR, "course.json"), "r"))
logger.info(f"The course curriculum is successfully loaded from course.json")
except json.JSONDecodeError:
logger.error("The course curriculum file provided is either malformed or corrupted.")
sys.exit(1)
elif args.load:
if os.path.isfile(args.load):
try:
course_curriculum = json.load(open(args.load, "r"))
logger.info(f"The course curriculum is successfully loaded from {args.load}")
except json.JSONDecodeError:
logger.error("The course curriculum file provided is either malformed or corrupted.")
sys.exit(1)
else:
logger.error("The course curriculum file could not be located. Please verify the file path and ensure that the file exists.")
sys.exit(1)
else:
logger.error("Please provide the path to the course curriculum file.")
sys.exit(1)
else:
try:
course_curriculum = udemy.fetch_course_curriculum(course_id)
except Exception as e:
logger.critical(f"Unable to retrieve the course curriculum. {e}")
sys.exit(1)
if args.save:
if args.save is True:
if (os.path.isfile(os.path.join(HOME_DIR, "course.json"))):
logger.warning("Course curriculum file already exists. Overwriting the existing file.")
with open(os.path.join(HOME_DIR, "course.json"), "w") as f:
json.dump(course_curriculum, f, indent=4)
logger.info(f"The course curriculum has been successfully saved to course.json")
elif args.save:
if (os.path.isfile(args.save)):
logger.warning("Course curriculum file already exists. Overwriting the existing file.")
with open(args.save, "w") as f:
json.dump(course_curriculum, f, indent=4)
logger.info(f"The course curriculum has been successfully saved to {args.save}")
if args.tree:
root_tree = Tree(course_info['title'], style="green")
udemy.build_curriculum_tree(course_curriculum, root_tree)
rprint(root_tree)
if args.tree is True:
pass
elif args.tree:
if (os.path.isfile(args.tree)):
logger.warning("Course Curriculum Tree file already exists. Overwriting the existing file.")
with open(args.tree, "w") as f:
rprint(root_tree, file=f)
logger.info(f"The course curriculum tree has been successfully saved to {args.tree}")
if args.srt:
convert_to_srt = True
else:
convert_to_srt = False
if args.start_lecture:
if args.start_chapter:
start_chapter = args.start_chapter
start_lecture = args.start_lecture
else:
logger.error("When using --start-lecture please provide --start-chapter")
sys.exit(1)
elif args.start_chapter:
start_chapter = args.start_chapter
start_lecture = 0
else:
start_chapter = 0
start_lecture = 0
if args.end_lecture:
if args.end_chapter:
end_chapter = args.end_chapter
end_lecture = args.end_lecture
elif args.end_chapter:
logger.error("When using --end-lecture please provide --end-chapter")
sys.exit(1)
elif args.end_chapter:
end_chapter = args.end_chapter
end_lecture = 1000
else:
end_chapter = len(course_curriculum)
end_lecture = 1000
logger.info("The course download is starting. Please wait while the materials are being downloaded.")
start_time = time.time()
udemy.download_course(course_id, course_curriculum)
end_time = time.time()
elapsed_time = end_time - start_time
logger.info(f"Download finished in {format_time(elapsed_time)}")
logger.info("All course materials have been successfully downloaded.")
logger.info("Download Complete.")
except KeyboardInterrupt:
logger.warning("Process interrupted. Exiting")
sys.exit(1)
if __name__ == "__main__":
main()