-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
114 lines (98 loc) · 3.72 KB
/
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
import threading
import time
import traceback
from tqdm import tqdm
from base import VERSION, LoginException, Scraper, Udemy, scraper_dict
from colors import *
# DUCE-CLI
def create_scraping_thread(site: str):
code_name = scraper_dict[site]
try:
t = threading.Thread(target=getattr(scraper, code_name), daemon=True)
t.start()
while getattr(scraper, f"{code_name}_length") == 0:
time.sleep(0.1) # Avoid busy waiting
if getattr(scraper, f"{code_name}_length") == -1:
raise Exception(f"Error in: {site}")
progress_bar = tqdm(
total=getattr(scraper, f"{code_name}_length"), desc=site, leave=False
)
prev_progress = -1
while prev_progress != getattr(scraper, f"{code_name}_length"):
time.sleep(0.1)
progress_bar.update(
getattr(scraper, f"{code_name}_progress") - prev_progress
)
prev_progress = getattr(scraper, f"{code_name}_progress")
if getattr(scraper, f"{code_name}_done"):
break
# progress_bar.close()
except Exception as e:
error = getattr(scraper, f"{code_name}_error", traceback.format_exc())
print(error)
print("\nError in: " + site + " " + str(VERSION))
##########################################
udemy = Udemy("cli")
udemy.load_settings()
login_title, main_title = udemy.check_for_update()
if login_title.__contains__("Update"):
print(by + fr + login_title)
############## MAIN #############
login_error = True
while login_error:
try:
if udemy.settings["use_browser_cookies"]:
udemy.fetch_cookies()
using = "Browser Cookies"
elif udemy.settings["email"] and udemy.settings["password"]:
email, password = udemy.settings["email"], udemy.settings["password"]
using = "Saved Email and Password"
else:
email = input("Email: ")
password = input("Password: ")
using = "Email and Password"
print(fb + f"Trying to login using {using}")
if "Email" in using:
udemy.manual_login(email, password)
udemy.get_session_info()
if "Email" in using:
udemy.settings["email"], udemy.settings["password"] = email, password
login_error = False
except LoginException as e:
print(fr + str(e))
if "Browser" in using:
print("Make sure you have logged in to Udemy in your browser")
elif "Email" in using:
udemy.settings["email"], udemy.settings["password"] = "", ""
udemy.save_settings()
print(fg + f"Logged in as {udemy.display_name}")
user_dumb = udemy.is_user_dumb()
if user_dumb:
print(bw + fr + "What do you even expect to happen!")
exit()
if not user_dumb:
scraper = Scraper(udemy.sites)
try:
udemy.scraped_data = scraper.get_scraped_courses(create_scraping_thread)
time.sleep(0.5)
print("\n")
udemy.start_enrolling()
udemy.print(
f"\nSuccessfully Enrolled: {udemy.successfully_enrolled_c}", color="green"
)
udemy.print(
f"Amount Saved: {round(udemy.amount_saved_c,2)} {udemy.currency.upper()}",
color="light green",
)
udemy.print(f"Already Enrolled: {udemy.already_enrolled_c}", color="blue")
udemy.print(f"Excluded Courses: {udemy.excluded_c}", color="yellow")
udemy.print(f"Expired Courses: {udemy.expired_c}", color="red")
except:
e = traceback.format_exc()
print(
(
"Error",
e + f"\n\n{udemy.link}\n{udemy.title}" + f"|:|Unknown Error {VERSION}",
)
)
input("Press Enter to exit...")