forked from jekahamster/coursera-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (61 loc) · 2.62 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
import sys
import json
from pathlib import Path
from command_parser import CommandParserBuilder
from driver_builder import build_chrome_driver
from defines import ROOT_DIR
from defines import COOKIES_PATH
from defines import WEBDRIVER_PATH
from defines import DOWNLOAD_PATH
from coursera_parser import CourseraParser
from utils import prepare_file_name
from utils import init
from typing import Union
def get_course_data(parser:CourseraParser, url:str, download_path:Path):
course_data = parser.get_course_data(url)
file_name = prepare_file_name(course_data["name"])
prepared_file_name = prepare_file_name(file_name)
with open(download_path / f"{prepared_file_name}.json", "w", encoding="utf-8") as file:
json.dump(course_data, file, indent=2)
def download_course_by_data(parser:CourseraParser, course_data_path:Path, download_path:Path):
course_data = None
with open(course_data_path, "r", encoding="utf-8") as file:
course_data = json.load(file)
assert course_data is not None, "Course data json is empty"
parser.download_by_course_data(course_data, download_path=download_path)
def main(args_):
command_parser = CommandParserBuilder.build()
parse_res = command_parser.parse_args(args_)
init()
driver = build_chrome_driver(
headless=False,
tor=False,
no_logging=True,
detach=False,
download_path=DOWNLOAD_PATH
)
coursera_parser = CourseraParser(webdriver=driver)
if parse_res.command == "get-course-data":
url = parse_res.url
cookies_path = COOKIES_PATH / parse_res.cookies
coursera_parser.login_by_cookies(cookies_path)
get_course_data(coursera_parser, url, DOWNLOAD_PATH)
elif parse_res.command == "download-course":
course_data_path = parse_res.path
cookies_path = COOKIES_PATH / parse_res.cookies
coursera_parser.login_by_cookies(cookies_path)
download_course_by_data(coursera_parser, course_data_path, DOWNLOAD_PATH)
elif parse_res.command == "login":
email = parse_res.email
password = parse_res.password
file_name = parse_res.file_name
coursera_parser.login_by_site(email, password)
coursera_parser.save_cookies(COOKIES_PATH / file_name)
elif parse_res.command == "download-video":
url = parse_res.url
path = Path(parse_res.path)
file_name = parse_res.cookies
coursera_parser.login_by_cookies(COOKIES_PATH / file_name)
coursera_parser.download_from_video_page(url=url, download_path=path)
if __name__ == "__main__":
main(sys.argv[1:])