-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplexcache.py
More file actions
169 lines (142 loc) · 5.53 KB
/
Copy pathplexcache.py
File metadata and controls
169 lines (142 loc) · 5.53 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
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
#!/usr/bin/env python3
"""PlexCache-D - Plex media caching automation for Unraid.
This is the unified entry point for PlexCache-D. It provides:
- Automatic first-run setup when no configuration exists
- Manual setup access via --setup flag
- Web UI via --web flag
- Normal caching operation
Usage:
python plexcache.py # Run caching (auto-setup if needed)
python plexcache.py --setup # Run setup wizard
python plexcache.py --web # Start web UI
python plexcache.py --dry-run # Simulate without moving files
python plexcache.py --verbose # Enable debug logging
python plexcache.py --help # Show help
"""
import sys
import os
def get_help_text():
"""Generate help text with the actual Python command being used."""
# Get the actual python command (e.g., python, python3, python3.11)
python_cmd = os.path.basename(sys.executable)
return f"""
PlexCache-D - Plex media caching automation for Unraid
Usage: {python_cmd} plexcache.py [OPTIONS]
Options:
--setup Run the setup wizard to configure PlexCache
--web Start the web UI (FastAPI server)
--dry-run Simulate operations without moving files
--verbose, -v Enable debug-level logging
--quiet Only notify on errors (suppress info messages)
--show-priorities Display cache priority scores for all cached files
--show-mappings Display path mapping configuration and status
--restore-plexcached Emergency restore of .plexcached backup files
Pinned Media:
--list-pins List all pinned media items
--pin KEY Pin a media item by Plex rating key
--unpin KEY Unpin a media item by Plex rating key
--pin-by-title TITLE Search Plex by title and pin interactively
Web UI Options (use with --web):
--host HOST Host to bind to (default: 127.0.0.1)
--port PORT Port to listen on (default: 5000)
--reload Enable auto-reload for development
Examples:
{python_cmd} plexcache.py Run caching (auto-setup on first run)
{python_cmd} plexcache.py --setup Configure or reconfigure settings
{python_cmd} plexcache.py --web Start web UI on localhost:5000
{python_cmd} plexcache.py --web --port 8080 Start web UI on custom port
{python_cmd} plexcache.py --dry-run --verbose Test run with full debug output
{python_cmd} plexcache.py --show-priorities See which files would be evicted first
{python_cmd} plexcache.py --list-pins Show all pinned media
{python_cmd} plexcache.py --pin-by-title "Breaking Bad" Search and pin
Documentation: https://github.com/StudioNirin/PlexCache-D
"""
def run_web_ui():
"""Start the web UI server."""
# Parse web-specific arguments
host = '127.0.0.1'
port = 5000
reload_enabled = False
args = sys.argv[1:]
i = 0
while i < len(args):
if args[i] == '--host' and i + 1 < len(args):
host = args[i + 1]
i += 2
elif args[i] == '--port' and i + 1 < len(args):
try:
port = int(args[i + 1])
except ValueError:
print(f"Error: Invalid port number: {args[i + 1]}")
return 1
i += 2
elif args[i] == '--reload':
reload_enabled = True
i += 1
else:
i += 1
# Check for required dependencies
try:
import uvicorn
except ImportError:
print("Error: Web UI dependencies not installed.")
print("")
print("Install with:")
print(" pip install fastapi uvicorn[standard] jinja2 python-multipart websockets aiofiles")
print("")
print("Or install all requirements:")
print(" pip install -r requirements.txt")
return 1
try:
from web.main import app # noqa: F401 - verify import works
except ImportError as e:
print(f"Error: Failed to import web application: {e}")
print("")
print("Make sure you're running from the PlexCache-D directory.")
return 1
print("=" * 60)
print(" PlexCache-D Web UI")
print("=" * 60)
print(f" URL: http://{host}:{port}")
print(f" Reload: {'Enabled' if reload_enabled else 'Disabled'}")
print("=" * 60)
print("")
print("Press Ctrl+C to stop the server")
print("")
uvicorn.run(
"web.main:app",
host=host,
port=port,
reload=reload_enabled,
log_level="info"
)
return 0
def main():
"""Main entry point for PlexCache-D."""
# Check for help flags
if "--help" in sys.argv or "-h" in sys.argv or "--h" in sys.argv:
print(get_help_text())
return 0
# Check for --setup flag (explicit setup request)
if "--setup" in sys.argv:
from core.setup import run_setup
run_setup()
return 0
# Check for --web flag (start web UI)
if "--web" in sys.argv:
return run_web_ui()
# Get project root directory
script_dir = os.path.dirname(os.path.abspath(__file__))
settings_path = os.path.join(script_dir, "plexcache_settings.json")
# Auto-run setup if no settings file exists (first-run experience)
if not os.path.exists(settings_path):
print("No configuration found. Starting setup wizard...")
print()
from core.setup import run_setup
run_setup()
return 0
# Normal operation - run the caching application
from core.app import main as app_main
return app_main()
if __name__ == "__main__":
sys.exit(main() or 0)