Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,19 +32,47 @@ app = Flask(__name__)
|
|
| 32 |
app.secret_key = os.environ.get('FLASK_SECRET_KEY', secrets.token_hex(32))
|
| 33 |
|
| 34 |
# --- Add this section for User Configuration ---
|
|
|
|
| 35 |
USER_CONFIG_FILE = 'user_config.json'
|
| 36 |
|
| 37 |
def load_user_config():
|
| 38 |
"""Load user configuration from file."""
|
| 39 |
-
config = {}
|
| 40 |
if os.path.exists(USER_CONFIG_FILE):
|
| 41 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
with open(USER_CONFIG_FILE, 'r') as f:
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def save_user_config(config):
|
| 49 |
"""Save user configuration to file."""
|
| 50 |
try:
|
|
@@ -615,10 +643,33 @@ def youtube_callback():
|
|
| 615 |
flash('YouTube authentication failed!', 'error')
|
| 616 |
return redirect(url_for('dashboard'))
|
| 617 |
|
|
|
|
| 618 |
@app.route('/telegram_auth')
|
| 619 |
@login_required
|
| 620 |
def telegram_auth():
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
|
| 623 |
# --- Add routes for Telegram auth via web ---
|
| 624 |
# Global variables for Telegram auth
|
|
|
|
| 32 |
app.secret_key = os.environ.get('FLASK_SECRET_KEY', secrets.token_hex(32))
|
| 33 |
|
| 34 |
# --- Add this section for User Configuration ---
|
| 35 |
+
# --- Add/Replace this section for User Configuration ---
|
| 36 |
USER_CONFIG_FILE = 'user_config.json'
|
| 37 |
|
| 38 |
def load_user_config():
|
| 39 |
"""Load user configuration from file."""
|
| 40 |
+
config = {} # Default to empty dict
|
| 41 |
if os.path.exists(USER_CONFIG_FILE):
|
| 42 |
try:
|
| 43 |
+
file_stat = os.stat(USER_CONFIG_FILE)
|
| 44 |
+
# Check if file is empty (st_size == 0)
|
| 45 |
+
if file_stat.st_size == 0:
|
| 46 |
+
web_logger.add_log('WARNING', f"User config file '{USER_CONFIG_FILE}' is empty.")
|
| 47 |
+
# Return empty config, don't treat as error
|
| 48 |
+
return config
|
| 49 |
+
|
| 50 |
with open(USER_CONFIG_FILE, 'r') as f:
|
| 51 |
+
# Load and return config
|
| 52 |
+
loaded_config = json.load(f)
|
| 53 |
+
# Basic check if it's a dict
|
| 54 |
+
if isinstance(loaded_config, dict):
|
| 55 |
+
web_logger.add_log('INFO', f"User config loaded successfully from '{USER_CONFIG_FILE}'.")
|
| 56 |
+
return loaded_config
|
| 57 |
+
else:
|
| 58 |
+
# If the JSON is valid but not a dict (e.g., a list or primitive)
|
| 59 |
+
web_logger.add_log('ERROR', f"User config file '{USER_CONFIG_FILE}' does not contain a JSON object.")
|
| 60 |
+
return config # Return default empty config
|
| 61 |
+
|
| 62 |
+
except json.JSONDecodeError as e:
|
| 63 |
+
# Handle invalid JSON specifically
|
| 64 |
+
web_logger.add_log('ERROR', f"Error decoding JSON in user config file '{USER_CONFIG_FILE}': {e}")
|
| 65 |
+
# Optionally, you could return an empty dict or raise an error
|
| 66 |
+
# Returning empty dict allows the app to continue, perhaps prompting re-save
|
| 67 |
+
return config
|
| 68 |
except Exception as e:
|
| 69 |
+
# Handle other potential file read errors (permissions, etc.)
|
| 70 |
+
web_logger.add_log('ERROR', f"Unexpected error loading user config file '{USER_CONFIG_FILE}': {e}")
|
| 71 |
+
return config # Return default empty config
|
| 72 |
+
else:
|
| 73 |
+
web_logger.add_log('INFO', f"User config file '{USER_CONFIG_FILE}' not found. Using defaults.")
|
| 74 |
+
return config # Return empty config if file doesn't exist
|
| 75 |
+
|
| 76 |
def save_user_config(config):
|
| 77 |
"""Save user configuration to file."""
|
| 78 |
try:
|
|
|
|
| 643 |
flash('YouTube authentication failed!', 'error')
|
| 644 |
return redirect(url_for('dashboard'))
|
| 645 |
|
| 646 |
+
# --- Update the telegram_auth route ---
|
| 647 |
@app.route('/telegram_auth')
|
| 648 |
@login_required
|
| 649 |
def telegram_auth():
|
| 650 |
+
global workflow_instance
|
| 651 |
+
# Ensure workflow instance exists to check auth status
|
| 652 |
+
if not workflow_instance:
|
| 653 |
+
try:
|
| 654 |
+
workflow_instance = TelegramYouTubeWorkflow()
|
| 655 |
+
except Exception as e:
|
| 656 |
+
web_logger.add_log('ERROR', f"Failed to initialize TelegramYouTubeWorkflow in telegram_auth: {e}")
|
| 657 |
+
# Even if init fails, we can still show the auth page
|
| 658 |
+
# but indicate Telegram is not connected
|
| 659 |
+
return render_template('telegram_auth.html', status={'telegram_authenticated': False})
|
| 660 |
+
|
| 661 |
+
# Determine Telegram authentication status
|
| 662 |
+
telegram_authenticated = False
|
| 663 |
+
try:
|
| 664 |
+
# A simple check is if the session file exists.
|
| 665 |
+
# A more robust check would involve trying to connect briefly.
|
| 666 |
+
telegram_authenticated = os.path.exists('session.session')
|
| 667 |
+
except Exception as e:
|
| 668 |
+
web_logger.add_log('WARNING', f"Could not determine Telegram auth status in telegram_auth route: {e}")
|
| 669 |
+
# Keep telegram_authenticated as False
|
| 670 |
+
|
| 671 |
+
# Pass the status to the template
|
| 672 |
+
return render_template('telegram_auth.html', status={'telegram_authenticated': telegram_authenticated})
|
| 673 |
|
| 674 |
# --- Add routes for Telegram auth via web ---
|
| 675 |
# Global variables for Telegram auth
|