File size: 5,663 Bytes
0e982f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
A web application to download music from YouTube and Spotify.
No user data storage - respecting privacy.
"""

import os
import logging
import shutil
from logging.handlers import RotatingFileHandler

from flask import Flask
from flask_wtf.csrf import CSRFProtect
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

from config import config
from routes import register_routes, register_error_handlers, init_rq

# Base directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))


def create_app(config_name="development"):
    """
    Factory function to create the Flask application.

    Args:
        config_name: Configuration name to use ('development', 'production', 'testing')

    Returns:
        Flask: Configured application instance
    """
    app = Flask(__name__)

    # Load configuration
    app.config.from_object(config[config_name])

    # Setup logging
    setup_logging(app)

    # Initial cleanup of temporary directories
    cleanup_temp_dirs(app)

    # Initialize extensions
    CSRFProtect(app)

    # Rate limiting to prevent abuse - using configuration values
    limiter = Limiter(
        key_func=get_remote_address,
        app=app,
        default_limits=[
            f"{app.config['RATE_LIMIT_PER_DAY']} per day",
            f"{app.config['RATE_LIMIT_PER_HOUR']} per hour",
        ],
        storage_uri="memory://",
    )

    # Initialise Redis connection and RQ task queue.
    # The same REDIS_URL is shared by the progress store in logic.py and the
    # RQ queue in routes.py so that the Flask app and the RQ worker both
    # read/write progress via the same Redis instance.
    redis_url = app.config.get("REDIS_URL", "redis://localhost:6379/0")

    from logic import init_redis as _init_logic_redis

    _init_logic_redis(redis_url)
    init_rq(redis_url)

    app.logger.info(f"Redis & RQ initialised ({redis_url})")

    # Register routes
    register_routes(app, limiter)

    # Register error handlers
    register_error_handlers(app)

    return app


def setup_logging(app):
    """Configures the application logging system."""
    # Remove duplicate Flask handlers if they exist
    if app.logger.hasHandlers():
        app.logger.handlers.clear()

    # Create logs directory if it doesn't exist
    log_dir = os.path.join(BASE_DIR, "logs")
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    # Configure log format
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )

    # File handler with UTF-8 encoding to support emojis
    file_handler = RotatingFileHandler(
        os.path.join(log_dir, "app.log"),
        maxBytes=10240000,  # 10MB
        backupCount=10,
        encoding="utf-8",
    )
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)

    # Console handler for development with UTF-8 encoding
    if app.debug:
        import sys

        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setFormatter(formatter)
        console_handler.setLevel(logging.DEBUG)
        if hasattr(console_handler.stream, "reconfigure"):
            console_handler.stream.reconfigure(encoding="utf-8", errors="replace")
        app.logger.addHandler(console_handler)

    app.logger.addHandler(file_handler)
    app.logger.setLevel(logging.INFO)
    app.logger.info("Offliner application started")


def cleanup_temp_dirs(app):
    """Cleans the temporary downloads directory at startup."""
    try:
        # Clean both Temp and Zip folders inside the downloads directory
        downloads_temp = os.path.join(BASE_DIR, "Downloads", "Temp")
        downloads_zip = os.path.join(BASE_DIR, "Downloads", "Zip")

        for path in (downloads_temp, downloads_zip):
            if os.path.exists(path):
                # Only remove and log if the directory actually contains files/subdirs
                has_contents = False
                if os.path.isdir(path):
                    try:
                        with os.scandir(path) as it:
                            for _ in it:
                                has_contents = True
                                break
                    except Exception:
                        # If we can't scan the dir, fall back to listing
                        try:
                            has_contents = len(os.listdir(path)) > 0
                        except Exception:
                            has_contents = False
                else:
                    # If it's not a directory (unexpected), treat it as content
                    has_contents = True

                if has_contents:
                    shutil.rmtree(path, ignore_errors=True)
                    app.logger.info(f"Initial cleanup: {path} deleted.")

        # Recreate empty Temp directory
        os.makedirs(downloads_temp, exist_ok=True)
        os.makedirs(downloads_zip, exist_ok=True)
    except Exception as e:
        app.logger.error(f"Error cleaning temporary directories: {e}")


# Create application instance
app = create_app(os.getenv("FLASK_ENV", "development"))


if __name__ == "__main__":
    from services import ensure_services

    # Start Redis server and RQ worker before accepting requests.
    # In debug mode with the reloader, only the *parent* process starts
    # services; the reloader children skip this to avoid duplicate processes.
    if os.environ.get("WERKZEUG_RUN_MAIN") != "true":
        redis_url = app.config.get("REDIS_URL", "redis://localhost:6379/0")
        ensure_services(redis_url)

    port = int(os.getenv("PORT", 7860))
    app.run(host="0.0.0.0", port=port)