Spaces:
Runtime error
Runtime error
Delete cli.py
Browse files
cli.py
DELETED
|
@@ -1,133 +0,0 @@
|
|
| 1 |
-
import threading
|
| 2 |
-
import time
|
| 3 |
-
import traceback
|
| 4 |
-
import sys
|
| 5 |
-
|
| 6 |
-
from base import VERSION, LoginException, Scraper, Udemy, scraper_dict
|
| 7 |
-
from colors import bw, by, fb, fg, fr
|
| 8 |
-
|
| 9 |
-
# DUCE-CLI
|
| 10 |
-
|
| 11 |
-
def create_scraping_thread(site: str):
|
| 12 |
-
"""Creates a separate thread for scraping each site."""
|
| 13 |
-
code_name = scraper_dict[site]
|
| 14 |
-
try:
|
| 15 |
-
t = threading.Thread(target=getattr(scraper, code_name), daemon=True)
|
| 16 |
-
t.start()
|
| 17 |
-
|
| 18 |
-
while getattr(scraper, f"{code_name}_length") == 0:
|
| 19 |
-
time.sleep(0.1) # Avoid busy waiting
|
| 20 |
-
|
| 21 |
-
if getattr(scraper, f"{code_name}_length") == -1:
|
| 22 |
-
raise Exception(f"Error in: {site}")
|
| 23 |
-
|
| 24 |
-
prev_progress = 0
|
| 25 |
-
total = getattr(scraper, f"{code_name}_length")
|
| 26 |
-
|
| 27 |
-
while not getattr(scraper, f"{code_name}_done"):
|
| 28 |
-
time.sleep(0.5)
|
| 29 |
-
current_progress = getattr(scraper, f"{code_name}_progress")
|
| 30 |
-
percent = (current_progress / total) * 100 if total else 0
|
| 31 |
-
print(f"[{site}] Progress: {percent:.2f}%")
|
| 32 |
-
sys.stdout.flush()
|
| 33 |
-
|
| 34 |
-
print(f"[{site}] Scraping Completed β
")
|
| 35 |
-
sys.stdout.flush()
|
| 36 |
-
|
| 37 |
-
except Exception as e:
|
| 38 |
-
error = getattr(scraper, f"{code_name}_error", traceback.format_exc())
|
| 39 |
-
print(f"[ERROR] {site}: {error}")
|
| 40 |
-
sys.stdout.flush()
|
| 41 |
-
|
| 42 |
-
##########################################
|
| 43 |
-
|
| 44 |
-
udemy = Udemy("cli")
|
| 45 |
-
udemy.load_settings()
|
| 46 |
-
login_title, main_title = udemy.check_for_update()
|
| 47 |
-
|
| 48 |
-
if "Update" in login_title:
|
| 49 |
-
print(by + fr + login_title)
|
| 50 |
-
sys.stdout.flush()
|
| 51 |
-
|
| 52 |
-
############## MAIN #############
|
| 53 |
-
|
| 54 |
-
login_successful = False
|
| 55 |
-
while not login_successful:
|
| 56 |
-
try:
|
| 57 |
-
if udemy.settings["use_browser_cookies"]:
|
| 58 |
-
udemy.fetch_cookies()
|
| 59 |
-
login_method = "Browser Cookies"
|
| 60 |
-
elif udemy.settings["email"] and udemy.settings["password"]:
|
| 61 |
-
email, password = udemy.settings["email"], udemy.settings["password"]
|
| 62 |
-
login_method = "Saved Email and Password"
|
| 63 |
-
else:
|
| 64 |
-
email = input("Email: ")
|
| 65 |
-
password = input("Password: ")
|
| 66 |
-
login_method = "Email and Password"
|
| 67 |
-
|
| 68 |
-
print(fb + f"Trying to login using {login_method}")
|
| 69 |
-
sys.stdout.flush()
|
| 70 |
-
|
| 71 |
-
if "Email" in login_method:
|
| 72 |
-
udemy.manual_login(email, password)
|
| 73 |
-
|
| 74 |
-
udemy.get_session_info()
|
| 75 |
-
if "Email" in login_method:
|
| 76 |
-
udemy.settings["email"], udemy.settings["password"] = email, password
|
| 77 |
-
login_successful = True
|
| 78 |
-
except LoginException as e:
|
| 79 |
-
print(fr + f"Login Failed: {e}")
|
| 80 |
-
sys.stdout.flush()
|
| 81 |
-
if "Browser" in login_method:
|
| 82 |
-
print("Can't login using cookies. Switching to manual login.")
|
| 83 |
-
udemy.settings["use_browser_cookies"] = False
|
| 84 |
-
elif "Email" in login_method:
|
| 85 |
-
udemy.settings["email"], udemy.settings["password"] = "", ""
|
| 86 |
-
|
| 87 |
-
udemy.save_settings()
|
| 88 |
-
|
| 89 |
-
print(fg + f"Logged in as {udemy.display_name}")
|
| 90 |
-
sys.stdout.flush()
|
| 91 |
-
|
| 92 |
-
user_dumb = udemy.is_user_dumb()
|
| 93 |
-
|
| 94 |
-
if user_dumb:
|
| 95 |
-
print(bw + fr + "Invalid user settings. Exiting...")
|
| 96 |
-
sys.stdout.flush()
|
| 97 |
-
exit()
|
| 98 |
-
|
| 99 |
-
scraper = Scraper(udemy.sites)
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
print("π Starting Course Scraping...")
|
| 103 |
-
sys.stdout.flush()
|
| 104 |
-
|
| 105 |
-
udemy.scraped_data = scraper.get_scraped_courses(create_scraping_thread)
|
| 106 |
-
time.sleep(0.5)
|
| 107 |
-
|
| 108 |
-
print("\nβ
Scraping Completed. Starting Enrollment...\n")
|
| 109 |
-
sys.stdout.flush()
|
| 110 |
-
|
| 111 |
-
udemy.start_enrolling()
|
| 112 |
-
|
| 113 |
-
udemy.print(
|
| 114 |
-
f"\nβ Successfully Enrolled: {udemy.successfully_enrolled_c}", color="green"
|
| 115 |
-
)
|
| 116 |
-
udemy.print(
|
| 117 |
-
f"π° Amount Saved: {round(udemy.amount_saved_c,2)} {udemy.currency.upper()}",
|
| 118 |
-
color="light green",
|
| 119 |
-
)
|
| 120 |
-
udemy.print(f"π΅ Already Enrolled: {udemy.already_enrolled_c}", color="blue")
|
| 121 |
-
udemy.print(f"β οΈ Excluded Courses: {udemy.excluded_c}", color="yellow")
|
| 122 |
-
udemy.print(f"β Expired Courses: {udemy.expired_c}", color="red")
|
| 123 |
-
|
| 124 |
-
sys.stdout.flush()
|
| 125 |
-
|
| 126 |
-
except Exception as e:
|
| 127 |
-
error = traceback.format_exc()
|
| 128 |
-
print(f"\n[ERROR] {error}\n")
|
| 129 |
-
sys.stdout.flush()
|
| 130 |
-
|
| 131 |
-
# β
Remove `input("Press Enter to exit...")` to prevent blocking in Flask
|
| 132 |
-
print("β
Process Completed!")
|
| 133 |
-
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|