""" Flask application factory for Podcastrr. """ import os from flask import Flask from flask_migrate import Migrate def create_app(config=None): """ Create and configure the Flask application. Args: config: Configuration object or path to configuration file. Returns: Flask application instance. """ app = Flask(__name__, template_folder='templates', static_folder='static') # Load default configuration app.config.from_mapping( SECRET_KEY=os.environ.get('SECRET_KEY', 'dev'), SQLALCHEMY_DATABASE_URI=os.environ.get('DATABASE_URI', 'sqlite:///podcastrr.db'), SQLALCHEMY_TRACK_MODIFICATIONS=False, DOWNLOAD_PATH=os.environ.get('DOWNLOAD_PATH', os.path.join(os.getcwd(), 'downloads')), ) # Load additional configuration if provided if config: app.config.from_mapping(config) # Import and initialize database from app.models.database import db db.init_app(app) Migrate(app, db) # Import and register blueprints from app.web.routes.main import main_bp from app.web.routes.podcasts import podcasts_bp from app.web.routes.settings import settings_bp from app.web.routes.api import api_bp from app.web.routes.tasks import tasks_bp app.register_blueprint(main_bp) app.register_blueprint(podcasts_bp, url_prefix='/podcasts') app.register_blueprint(settings_bp, url_prefix='/settings') app.register_blueprint(api_bp, url_prefix='/api') app.register_blueprint(tasks_bp) # Ensure the download directory exists os.makedirs(app.config['DOWNLOAD_PATH'], exist_ok=True) # Run database migrations with app.app_context(): try: from migrations.add_season_explicit_naming_format import run_migration run_migration() # Run migration to add episode_ordering column from migrations.add_episode_ordering import run_migration as run_episode_ordering_migration run_episode_ordering_migration() except Exception as e: app.logger.error(f"Error running migration: {str(e)}") return app