Spaces:
Running
Running
import json | |
import os | |
SETTINGS_FILE = "settings.json" | |
class Udemy: | |
def __init__(self, mode): | |
self.mode = mode | |
self.settings = {} | |
def load_settings(self): | |
"""Load user settings from JSON, handle errors if file is empty or corrupted.""" | |
try: | |
if not os.path.exists(SETTINGS_FILE) or os.stat(SETTINGS_FILE).st_size == 0: | |
print("⚠️ Settings file missing or empty. Creating default settings...") | |
self.create_default_settings() | |
with open(SETTINGS_FILE, "r") as f: | |
self.settings = json.load(f) | |
except (json.JSONDecodeError, ValueError): | |
print("⚠️ Settings file is corrupted. Resetting to default settings...") | |
self.create_default_settings() | |
def create_default_settings(self): | |
"""Create a default settings file if it's missing or corrupted.""" | |
self.settings = { | |
"use_browser_cookies": False, | |
"email": "", | |
"password": "", | |
"scraper_sites": ["DiscUdemy", "Udemy Freebies", "Tutorial Bar"], | |
} | |
with open(SETTINGS_FILE, "w") as f: | |
json.dump(self.settings, f, indent=4) | |
print("✅ Default settings file created.") | |