-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
100 lines (84 loc) · 3.43 KB
/
Copy pathconfig.py
File metadata and controls
100 lines (84 loc) · 3.43 KB
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
import os
from typing import Dict, Any
class Config:
"""Base configuration class"""
# Flask settings
SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')
DEBUG = False
TESTING = False
# Application settings
UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER', 'downloads')
MAX_CONTENT_LENGTH = int(os.environ.get('MAX_CONTENT_LENGTH', 16 * 1024 * 1024)) # 16MB
# Server settings
HOST = os.environ.get('HOST', '0.0.0.0')
PORT = int(os.environ.get('PORT', 8080))
# yt-dlp settings
YT_DLP_TIMEOUT = int(os.environ.get('YT_DLP_TIMEOUT', 300)) # 5 minutes
YT_DLP_RETRIES = int(os.environ.get('YT_DLP_RETRIES', 3))
# Rate limiting
RATELIMIT_STORAGE_URL = os.environ.get('RATELIMIT_STORAGE_URL', 'memory://')
RATELIMIT_DEFAULT = os.environ.get('RATELIMIT_DEFAULT', '100 per hour')
# Logging
LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
LOG_FORMAT = os.environ.get('LOG_FORMAT', '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Security headers
SECURITY_HEADERS = {
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'X-XSS-Protection': '1; mode=block',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'Content-Security-Policy': "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';"
}
@staticmethod
def get_yt_dlp_options(download_dir: str) -> Dict[str, Any]:
"""Get yt-dlp configuration options"""
return {
'outtmpl': os.path.join(download_dir, '%(title)s.%(ext)s'),
'format': 'best/mp4/any',
'noplaylist': True,
'extract_flat': False,
'socket_timeout': Config.YT_DLP_TIMEOUT,
'retries': Config.YT_DLP_RETRIES,
'http_headers': {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://www.tiktok.com/',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
},
'extractor_retries': 3,
'fragment_retries': 3,
}
class DevelopmentConfig(Config):
"""Development configuration"""
DEBUG = True
LOG_LEVEL = 'DEBUG'
class ProductionConfig(Config):
"""Production configuration"""
DEBUG = False
LOG_LEVEL = 'WARNING'
# Enhanced security for production
SECURITY_HEADERS = {
**Config.SECURITY_HEADERS,
'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload',
}
class TestingConfig(Config):
"""Testing configuration"""
TESTING = True
DEBUG = True
LOG_LEVEL = 'DEBUG'
UPLOAD_FOLDER = 'test_downloads'
# Configuration mapping
config = {
'development': DevelopmentConfig,
'production': ProductionConfig,
'testing': TestingConfig,
'default': ProductionConfig
}
def get_config(config_name: str = None) -> Config:
"""Get configuration based on environment"""
if config_name is None:
config_name = os.environ.get('FLASK_ENV', 'production')
return config.get(config_name, config['default'])