afluakd9 commited on
Commit
bbe872c
·
0 Parent(s):

Initial commit without binary files

Browse files
.env.example ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Telegram Bot Configuration
2
+ BOT_TOKEN=your-bot-token-from-botfather
3
+
4
+ # Telegram Bot API Server (self-hosted for 2GB upload limit)
5
+ # Get these from https://my.telegram.org -> API Development Tools
6
+ TELEGRAM_API_ID=your-api-id
7
+ TELEGRAM_API_HASH=your-api-hash
8
+
9
+ # Reclip Configuration
10
+ RECLIP_URL=http://reclip:8899
11
+ RECLIP_HOST=0.0.0.0
12
+ RECLIP_PORT=8899
13
+ MAX_CONCURRENT_DOWNLOADS=3
14
+ DOWNLOAD_TIMEOUT=900
15
+
16
+ # Cleanup Configuration
17
+ CLEANUP_MAX_AGE_HOURS=1
18
+ CLEANUP_MAX_DISK_MB=5000
19
+ CLEANUP_INTERVAL_SECONDS=300
20
+
21
+ # Shared downloads directory (Docker volume)
22
+ DOWNLOADS_PATH=/downloads
23
+
24
+ # Dashboard Configuration
25
+ DASHBOARD_USER=admin
26
+ DASHBOARD_PASSWORD=your-dashboard-password
27
+ DASHBOARD_PORT=8080
28
+ DASHBOARD_SECRET_KEY=change-me-to-a-random-string
.github/tests/test_ci_config.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Validation tests for GitHub Actions workflow and docker-compose.yml configuration."""
2
+
3
+ import pytest
4
+ import yaml
5
+ from pathlib import Path
6
+
7
+ REPO_ROOT = Path(__file__).resolve().parents[2]
8
+ WORKFLOW_PATH = REPO_ROOT / ".github/workflows/release.yml"
9
+ COMPOSE_PATH = REPO_ROOT / "docker-compose.yml"
10
+
11
+ WORKFLOW_SERVICES = ["reclip", "bot", "dashboard"]
12
+ COMPOSE_SERVICES_WITH_BUILD = ["reclip", "bot", "dashboard"]
13
+
14
+
15
+ def load_yaml(path: Path) -> dict:
16
+ with open(path) as f:
17
+ return yaml.safe_load(f)
18
+
19
+
20
+ # ---------------------------------------------------------------------------
21
+ # Test 1: YAML parsing
22
+ # ---------------------------------------------------------------------------
23
+
24
+ class TestYAMLValid:
25
+ def test_workflow_valid_yaml(self):
26
+ result = load_yaml(WORKFLOW_PATH)
27
+ assert isinstance(result, dict), "release.yml should parse to a dict"
28
+
29
+ def test_compose_valid_yaml(self):
30
+ result = load_yaml(COMPOSE_PATH)
31
+ assert isinstance(result, dict), "docker-compose.yml should parse to a dict"
32
+
33
+
34
+ # ---------------------------------------------------------------------------
35
+ # Test 2: Matrix coverage
36
+ # ---------------------------------------------------------------------------
37
+
38
+ class TestMatrixCoverage:
39
+ def test_matrix_service_contents(self):
40
+ workflow = load_yaml(WORKFLOW_PATH)
41
+ matrix = workflow["jobs"]["build-and-push"]["strategy"]["matrix"]
42
+ assert "service" in matrix, "matrix must define 'service'"
43
+ assert set(matrix["service"]) == set(WORKFLOW_SERVICES), (
44
+ f"matrix.service must be exactly {WORKFLOW_SERVICES}, got {matrix['service']}"
45
+ )
46
+
47
+ def test_telegram_bot_api_not_in_matrix(self):
48
+ workflow = load_yaml(WORKFLOW_PATH)
49
+ matrix = workflow["jobs"]["build-and-push"]["strategy"]["matrix"]
50
+ assert "telegram-bot-api" not in matrix.get("service", []), (
51
+ "telegram-bot-api must not appear in matrix.service"
52
+ )
53
+
54
+
55
+ # ---------------------------------------------------------------------------
56
+ # Test 3: Build context consistency
57
+ # ---------------------------------------------------------------------------
58
+
59
+ class TestBuildContextConsistency:
60
+ def test_workflow_context_pattern(self):
61
+ workflow = load_yaml(WORKFLOW_PATH)
62
+ build_step = next(s for s in workflow["jobs"]["build-and-push"]["steps"] if "context" in (s.get("with") or {}))
63
+ assert "context" in build_step["with"], "build-push step must set context"
64
+ assert build_step["with"]["context"] == "./${{ matrix.service }}", (
65
+ f"workflow context must be './${{{{ matrix.service }}}}', "
66
+ f"got {build_step['with']['context']!r}"
67
+ )
68
+
69
+ def test_compose_build_contexts_exist(self):
70
+ compose = load_yaml(COMPOSE_PATH)
71
+ for service in COMPOSE_SERVICES_WITH_BUILD:
72
+ build_ctx = compose["services"][service].get("build", "")
73
+ if build_ctx.startswith("./"):
74
+ build_ctx = build_ctx[2:]
75
+ assert build_ctx == service, (
76
+ f"service '{service}' build context must be './{service}', "
77
+ f"got {compose['services'][service].get('build')!r}"
78
+ )
79
+
80
+
81
+ # ---------------------------------------------------------------------------
82
+ # Test 4: Image tag pattern
83
+ # ---------------------------------------------------------------------------
84
+
85
+ class TestImageTagPattern:
86
+ def test_compose_images_use_ghcr(self):
87
+ compose = load_yaml(COMPOSE_PATH)
88
+ for service in COMPOSE_SERVICES_WITH_BUILD:
89
+ image = compose["services"][service].get("image", "")
90
+ assert image.startswith("ghcr.io/"), (
91
+ f"service '{service}' image must start with 'ghcr.io/', got {image!r}"
92
+ )
93
+
94
+ def test_compose_images_use_tag_variable(self):
95
+ compose = load_yaml(COMPOSE_PATH)
96
+ for service in COMPOSE_SERVICES_WITH_BUILD:
97
+ image = compose["services"][service].get("image", "")
98
+ assert "${IMAGE_TAG:-latest}" in image, (
99
+ f"service '{service}' image must include '${IMAGE_TAG:-latest}', got {image!r}"
100
+ )
101
+
102
+ def test_workflow_tags_ghcr_pattern(self):
103
+ workflow = load_yaml(WORKFLOW_PATH)
104
+ build_step = next(s for s in workflow["jobs"]["build-and-push"]["steps"] if "tags" in (s.get("with") or {}))
105
+ # tags is a multi-line string, not a YAML list
106
+ tags_raw = build_step["with"].get("tags", "")
107
+ assert "ghcr.io/" in tags_raw, (
108
+ f"workflow tags must include 'ghcr.io/', got:\n{tags_raw}"
109
+ )
110
+ assert ":${{ steps.version.outputs.version }}" in tags_raw, (
111
+ f"workflow tags must include ':${{{{ steps.version.outputs.version }}}}', got:\n{tags_raw}"
112
+ )
113
+ assert ":latest" in tags_raw, (
114
+ f"workflow tags must include ':latest', got:\n{tags_raw}"
115
+ )
116
+
117
+
118
+ # ---------------------------------------------------------------------------
119
+ # Test 5: Prerelease skip
120
+ # ---------------------------------------------------------------------------
121
+
122
+ class TestPrereleaseSkip:
123
+ def test_prerelease_skip_condition_exists(self):
124
+ workflow = load_yaml(WORKFLOW_PATH)
125
+ job = workflow["jobs"]["build-and-push"]
126
+ assert "if" in job, "build-and-push job must have an 'if' condition"
127
+ assert "!github.event.release.prerelease" in job["if"], (
128
+ f"job 'if' must check '!github.event.release.prerelease', got {job['if']!r}"
129
+ )
.github/workflows/release.yml ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Build and Push Docker Images
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ contents: read
9
+ packages: write
10
+
11
+ jobs:
12
+ build-and-push:
13
+ if: ${{ !github.event.release.prerelease }}
14
+ runs-on: ubuntu-latest
15
+ strategy:
16
+ matrix:
17
+ service: [reclip, bot, dashboard]
18
+ steps:
19
+ - name: Checkout
20
+ uses: actions/checkout@v4
21
+
22
+ - name: Set up Docker Buildx
23
+ uses: docker/setup-buildx-action@v4
24
+
25
+ - name: Login to GHCR
26
+ uses: docker/login-action@v4
27
+ with:
28
+ registry: ghcr.io
29
+ username: ${{ github.actor }}
30
+ password: ${{ secrets.GITHUB_TOKEN }}
31
+
32
+ - name: Lowercase repository
33
+ id: repo
34
+ run: echo "repo=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
35
+
36
+ - name: Extract version
37
+ id: version
38
+ run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
39
+
40
+ - name: Build and push ${{ matrix.service }}
41
+ uses: docker/build-push-action@v7
42
+ with:
43
+ context: ./${{ matrix.service }}
44
+ push: true
45
+ build-args: VERSION=${{ steps.version.outputs.version }}
46
+ tags: |
47
+ ghcr.io/${{ steps.repo.outputs.repo }}/${{ matrix.service }}:${{ steps.version.outputs.version }}
48
+ ghcr.io/${{ steps.repo.outputs.repo }}/${{ matrix.service }}:latest
.gitignore ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ .env
2
+ .venv/
3
+ downloads/
4
+ __pycache__/
5
+ *.pyc
6
+ .pytest_cache/
7
+ telegram-bot-api-data/
8
+ DESIGN.md
9
+ CLAUDE.md
10
+ docs/
AGENTS.md ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AGENTS.md — ReClip Telegram Bot
2
+
3
+ Compact instruction file for OpenCode sessions working in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ Self-hosted Telegram bot that downloads media from YouTube, TikTok, Instagram, and 1000+ other sites. Built from four Dockerized Python services:
8
+
9
+ | Service | Tech | Port | Role |
10
+ |---------|------|------|------|
11
+ | `reclip/` | Flask + yt-dlp | 8899 | Download engine with REST API |
12
+ | `bot/` | python-telegram-bot + httpx | — | Telegram bot client |
13
+ | `dashboard/` | FastAPI + aiosqlite + Jinja2 | 8080 | Admin panel with stats |
14
+ | `telegram-bot-api` | aiogram/telegram-bot-api | 8081 | Self-hosted Bot API for 2GB uploads |
15
+
16
+ ## Running Locally (No Docker)
17
+
18
+ Start services in this order — each needs its own terminal:
19
+
20
+ ```bash
21
+ # 1. Reclip (download engine)
22
+ cd reclip
23
+ pip install flask yt-dlp
24
+ python app.py # listens on 8899
25
+
26
+ # 2. Dashboard (admin panel)
27
+ cd dashboard
28
+ pip install -r requirements.txt
29
+ ADMIN_PASSWORD=changeme DB_PATH=./reclip.db \
30
+ uvicorn main:create_app --factory --host 0.0.0.0 --port 8080
31
+
32
+ # 3. Bot (Telegram client)
33
+ cd bot
34
+ pip install -r requirements.txt
35
+ BOT_TOKEN=<token> RECLIP_URL=http://localhost:8899 \
36
+ DOWNLOADS_PATH=../reclip/downloads python bot.py
37
+ ```
38
+
39
+ The bot waits up to 60s for the self-hosted Bot API server on startup. In Docker it talks to `http://telegram-bot-api:8081`; locally you need that container running too, or the bot will warn and start anyway (uploads >20MB will fail).
40
+
41
+ ## Running Tests
42
+
43
+ ```bash
44
+ # Bot tests
45
+ cd bot
46
+ pip install -r requirements.txt
47
+ pip install pytest pytest-asyncio
48
+ python -m pytest tests/ -v
49
+
50
+ # Dashboard tests
51
+ cd dashboard
52
+ pip install -r requirements.txt
53
+ pip install pytest pytest-asyncio httpx
54
+ python -m pytest tests/ -v
55
+ ```
56
+
57
+ **Critical:** Dashboard tests must set `DB_PATH`, `ADMIN_PASSWORD`, and `SECRET_KEY` **before** importing `db.py` or `main.py`. The test files do this via `os.environ` at the top of the file. If you add new dashboard test files, copy that pattern or imports will create the DB in the wrong place.
58
+
59
+ ## Architecture Notes
60
+
61
+ ### Reclip API Surface (`reclip/app.py`)
62
+ - `POST /api/info` — returns title, thumbnail, duration, uploader, extractor, and available quality formats
63
+ - `POST /api/download` — spawns a threaded yt-dlp download, returns `job_id`
64
+ - `GET /api/status/<job_id>` — returns `status` (`downloading` | `done` | `error`), progress dict, file metadata
65
+ - `GET /api/file/<job_id>` — serves finished file as attachment
66
+
67
+ ### Bot → Dashboard Events (`bot/event_client.py`)
68
+ The bot sends fire-and-forget HTTP POSTs to `DASHBOARD_URL/api/events` with `type: download_start | progress | download_done | download_error`. These are best-effort; failures are logged at DEBUG and ignored.
69
+
70
+ ### Download Flow
71
+ 1. User sends URL → bot calls `reclip_client.get_info()`
72
+ 2. Bot shows thumbnail + format buttons (MP4 / MP3)
73
+ 3. MP4 path shows quality picker; MP3 path starts audio download directly
74
+ 4. Bot calls `start_download()` → polls `poll_status()` every 2s
75
+ 5. Progress updates edit the Telegram message in real time
76
+ 6. On `status: done`, bot uploads the file via the self-hosted Bot API
77
+
78
+ ### Post-Processing in Reclip
79
+ - yt-dlp downloads to `DOWNLOADS_PATH/<job_id>.<ext>`
80
+ - AV1/VP9/VP8 videos are transcoded to H264 via ffmpeg (Telegram compatibility)
81
+ - Other MP4s get `-movflags +faststart` via ffmpeg copy
82
+ - File duration/width/height extracted with ffprobe
83
+
84
+ ### Concurrency & Limits
85
+ - Reclip uses `threading.Semaphore(MAX_CONCURRENT_DOWNLOADS)` (default 3)
86
+ - Each download has a hard timeout of `DOWNLOAD_TIMEOUT` seconds (default 900)
87
+ - Bot has a 10-minute in-memory session TTL (`STATE_TTL = 600`)
88
+
89
+ ### Cleanup (`bot/cleanup.py`)
90
+ Background asyncio task that runs every `CLEANUP_INTERVAL_SECONDS` (default 300):
91
+ 1. Deletes files older than `CLEANUP_MAX_AGE_HOURS` (default 1)
92
+ 2. If disk usage exceeds `CLEANUP_MAX_DISK_MB` (default 5000), deletes oldest files first
93
+
94
+ ## Environment Variables
95
+
96
+ Required:
97
+ - `BOT_TOKEN` — from @BotFather
98
+ - `TELEGRAM_API_ID` / `TELEGRAM_API_HASH` — from my.telegram.org
99
+ - `DASHBOARD_PASSWORD` — dashboard login password
100
+
101
+ Commonly overridden for local dev:
102
+ - `RECLIP_URL=http://localhost:8899`
103
+ - `TELEGRAM_BOT_API_URL=http://localhost:8081`
104
+ - `DOWNLOADS_PATH=./downloads`
105
+ - `DB_PATH=./reclip.db`
106
+ - `SECRET_KEY=change-me` (dashboard cookie signing)
107
+
108
+ See `.env.example` for the full list with defaults.
109
+
110
+ ## Code Conventions
111
+
112
+ - **No linting / formatting / typechecking tooling is configured.** There is no black, ruff, mypy, or pre-commit setup.
113
+ - Imports use `sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))` in test files to reach parent modules.
114
+ - Dashboard DB uses SQLite with WAL mode (`PRAGMA journal_mode=WAL`).
115
+ - Auth is session-cookie based (itsdangerous URLSafeTimedSerializer), not JWT or OAuth.
116
+ - All event_client calls are fire-and-forget with a 2-second timeout; they must never crash the bot.
117
+
118
+ ## Common Gotchas
119
+
120
+ - **Shared volume:** `downloads` is a Docker shared volume between reclip, bot, and dashboard. Locally, all three services must point to the same filesystem path.
121
+ - **Bot API server first startup:** The self-hosted Bot API downloads Telegram server data on first run. This can take 30–60 seconds. The bot polls `wait_for_bot_api()` for up to 60s.
122
+ - **Dashboard DB path:** `_db_path()` re-reads `DB_PATH` from env on every call so tests can override it dynamically.
123
+ - **Reclip job state is in-memory:** `jobs = {}` in `reclip/app.py` is not persistent. Restarting reclip loses active download state.
124
+ - **FFmpeg dependency:** The reclip Dockerfile installs ffmpeg. Locally you need it installed (`apt install ffmpeg` or equivalent).
README.md ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ReClip Telegram Bot
2
+
3
+ A self-hosted Telegram bot that downloads media from YouTube, TikTok, Instagram, Twitter, Reddit, and 1000+ other sites. Powered by [reclip](https://github.com/averygan/reclip) and yt-dlp.
4
+
5
+ Send a link, pick your format and quality, get the file delivered right in the chat.
6
+
7
+ ![Bot conversation](images/bot.png)
8
+
9
+ ## Features
10
+
11
+ - Multi-platform support (YouTube, TikTok, Instagram, Twitter, Reddit, and 1000+ more via yt-dlp)
12
+ - Format selection (MP4 video or MP3 audio)
13
+ - Quality picker with all available resolutions
14
+ - Real-time download progress (percentage)
15
+ - Thumbnail preview with metadata (title, platform, duration)
16
+ - Files up to 2GB via self-hosted Telegram Bot API
17
+ - Automatic file cleanup (configurable age and disk limits)
18
+ - Concurrent download limiting (prevents resource exhaustion)
19
+ - Admin dashboard with download stats, history, error tracking, and disk management
20
+ - Web UI included (reclip's built-in web interface)
21
+
22
+ ![Admin dashboard](images/admin.png)
23
+
24
+ ## Architecture
25
+
26
+ ```
27
+ Telegram User ──> Self-hosted Bot API (2GB limit)
28
+ │
29
+ ▼
30
+ bot (python-telegram-bot + httpx)
31
+ │ │
32
+ HTTP │ │ HTTP events
33
+ ▼ ▼
34
+ reclip (Flask) dashboard (FastAPI)
35
+ port 8899 port 8080
36
+ │
37
+ ▼
38
+ SQLite
39
+ ```
40
+
41
+ Four Docker containers via docker-compose:
42
+ 1. **reclip** - Media download engine with REST API and web UI
43
+ 2. **bot** - Telegram bot that wraps reclip's API
44
+ 3. **telegram-bot-api** - Self-hosted Telegram Bot API server for 2GB upload limit
45
+ 4. **dashboard** - Admin panel with download stats, history, errors, and file management
46
+
47
+ ## Quick Start
48
+
49
+ ### Prerequisites
50
+
51
+ - Docker and Docker Compose
52
+ - A Telegram bot token (from [@BotFather](https://t.me/BotFather))
53
+ - Telegram API credentials (from [my.telegram.org](https://my.telegram.org))
54
+
55
+ ### Setup
56
+
57
+ 1. Clone this repository:
58
+ ```bash
59
+ git clone https://github.com/gth-ai/reclip-telegram-bot.git
60
+ cd reclip_bot
61
+ ```
62
+
63
+ 2. Copy the example environment file:
64
+ ```bash
65
+ cp .env.example .env
66
+ ```
67
+
68
+ 3. Edit `.env` with your credentials:
69
+ ```bash
70
+ BOT_TOKEN=your-bot-token-from-botfather
71
+ TELEGRAM_API_ID=your-api-id
72
+ TELEGRAM_API_HASH=your-api-hash
73
+ ```
74
+
75
+ To get Telegram API credentials:
76
+ - Go to https://my.telegram.org
77
+ - Log in with your phone number
78
+ - Go to "API Development Tools"
79
+ - Create a new application (any name/description works)
80
+ - Copy the `api_id` and `api_hash`
81
+
82
+ 4. Start the services:
83
+ ```bash
84
+ docker-compose up -d
85
+ ```
86
+
87
+ 5. Send a video link to your bot on Telegram.
88
+
89
+ ### First Run Note
90
+
91
+ The self-hosted Bot API server downloads some data from Telegram on first startup. This can take a minute. The bot will start responding once the Bot API server is ready.
92
+
93
+ ## Configuration
94
+
95
+ All configuration is via environment variables in `.env`:
96
+
97
+ | Variable | Default | Description |
98
+ |---|---|---|
99
+ | `BOT_TOKEN` | (required) | Telegram bot token from @BotFather |
100
+ | `TELEGRAM_API_ID` | (required) | Telegram API ID from my.telegram.org |
101
+ | `TELEGRAM_API_HASH` | (required) | Telegram API hash from my.telegram.org |
102
+ | `MAX_CONCURRENT_DOWNLOADS` | 3 | Max parallel downloads |
103
+ | `CLEANUP_MAX_AGE_HOURS` | 1 | Delete files older than this |
104
+ | `CLEANUP_MAX_DISK_MB` | 5000 | Max disk usage before cleanup |
105
+ | `CLEANUP_INTERVAL_SECONDS` | 300 | Cleanup check interval |
106
+ | `DASHBOARD_USER` | admin | Dashboard login username |
107
+ | `DASHBOARD_PASSWORD` | (required) | Dashboard login password |
108
+ | `DASHBOARD_PORT` | 8080 | Dashboard port on host |
109
+ | `DASHBOARD_SECRET_KEY` | change-me | Cookie signing key |
110
+
111
+ ## Admin Dashboard
112
+
113
+ The admin dashboard is available at http://localhost:8080 after starting the services. Log in with the credentials from your `.env` file.
114
+
115
+ Pages:
116
+ - **Dashboard** — downloads today, active users, disk usage, error rate, charts
117
+ - **History** — full download log with filters and pagination
118
+ - **Errors** — failed downloads with error messages
119
+ - **Admin** — file management, system info, purge controls
120
+
121
+ ## Web UI
122
+
123
+ The reclip web UI is available if you uncomment the `reclip-web` service in `docker-compose.yml`:
124
+
125
+ ```yaml
126
+ reclip-web:
127
+ extends:
128
+ service: reclip
129
+ ports:
130
+ - "8899:8899"
131
+ ```
132
+
133
+ Then access it at http://localhost:8899.
134
+
135
+ ## How It Works
136
+
137
+ 1. You send a URL to the bot
138
+ 2. Bot sends "Fetching info..." immediately
139
+ 3. Bot calls reclip's API to get video metadata
140
+ 4. Bot displays thumbnail, title, platform, and format buttons (MP4/MP3)
141
+ 5. You tap MP4 to see quality options (1080p, 720p, etc.) or MP3 for audio
142
+ 6. Bot starts the download and shows real-time progress
143
+ 7. Bot uploads the file to the Telegram chat
144
+ 8. Cleanup task removes old files automatically
145
+
146
+ ## Development
147
+
148
+ ### Running locally (without Docker)
149
+
150
+ ```bash
151
+ # Start reclip
152
+ cd reclip && pip install flask yt-dlp && python app.py &
153
+
154
+ # Start the bot
155
+ cd bot && pip install -r requirements.txt
156
+ BOT_TOKEN=your-token RECLIP_URL=http://localhost:8899 DOWNLOADS_PATH=../reclip/downloads python bot.py
157
+ ```
158
+
159
+ ### Project structure
160
+
161
+ ```
162
+ reclip_bot/
163
+ ├── docker-compose.yml # 4 services: reclip, bot, telegram-bot-api, dashboard
164
+ ├── .env.example # Environment variables template
165
+ ├── bot/
166
+ │ ├── bot.py # Bot entry point
167
+ │ ├── handlers.py # Telegram message/callback handlers
168
+ │ ├── reclip_client.py # Async HTTP client for reclip API
169
+ │ ├── event_client.py # Fire-and-forget events to dashboard
170
+ │ ├── cleanup.py # Background file cleanup task
171
+ │ ├── requirements.txt
172
+ │ └── Dockerfile
173
+ ├── dashboard/
174
+ │ ├── main.py # FastAPI app with background tasks
175
+ │ ├── db.py # SQLite queries (async via aiosqlite)
176
+ │ ├── auth.py # Session cookie auth
177
+ │ ├── routes/ # API + page routes
178
+ │ ├── templates/ # Jinja2 templates (dark theme)
179
+ │ ├── static/ # CSS + Chart.js frontend
180
+ │ ├── requirements.txt
181
+ │ └── Dockerfile
182
+ └── reclip/ # Fork of averygan/reclip with enhancements
183
+ ├── app.py # Flask API + web UI (with progress hooks)
184
+ ├── Dockerfile
185
+ ├── templates/
186
+ └── static/
187
+ ```
188
+
189
+ ## Credits
190
+
191
+ - [reclip](https://github.com/averygan/reclip) by averygan - The media download engine
192
+ - [yt-dlp](https://github.com/yt-dlp/yt-dlp) - The download backend
193
+ - [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) - Telegram bot framework
194
+
195
+ ## License
196
+
197
+ MIT
bot/Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ CMD ["python", "bot.py"]
bot/bot.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import logging
3
+ import os
4
+ import sys
5
+
6
+ from telegram.ext import ApplicationBuilder
7
+
8
+ from cleanup import cleanup_loop
9
+ from handlers import register_handlers
10
+
11
+ logging.basicConfig(
12
+ format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
13
+ level=logging.INFO,
14
+ )
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ async def wait_for_bot_api(url: str, max_wait: int = 60):
19
+ """Wait for the self-hosted Bot API server to become reachable."""
20
+ import httpx
21
+ for i in range(max_wait):
22
+ try:
23
+ async with httpx.AsyncClient() as client:
24
+ resp = await client.get(url, timeout=2)
25
+ logger.info("Bot API server ready (attempt %d)", i + 1)
26
+ return
27
+ except Exception:
28
+ if i % 5 == 0:
29
+ logger.info("Waiting for Bot API server at %s... (attempt %d)", url, i + 1)
30
+ await asyncio.sleep(1)
31
+ logger.warning("Bot API server not reachable after %ds, starting anyway", max_wait)
32
+
33
+
34
+ def main():
35
+ bot_token = os.environ.get("BOT_TOKEN")
36
+ if not bot_token:
37
+ logger.error("BOT_TOKEN environment variable is required")
38
+ sys.exit(1)
39
+
40
+ api_url = os.environ.get("TELEGRAM_BOT_API_URL", "http://telegram-bot-api:8081")
41
+ base_url = f"{api_url}/bot"
42
+ base_file_url = f"{api_url}/file/bot"
43
+
44
+ logger.info("Starting bot with API server: %s", api_url)
45
+
46
+ asyncio.get_event_loop().run_until_complete(wait_for_bot_api(api_url))
47
+
48
+ app = (
49
+ ApplicationBuilder()
50
+ .token(bot_token)
51
+ .base_url(base_url)
52
+ .base_file_url(base_file_url)
53
+ .read_timeout(60)
54
+ .write_timeout(60)
55
+ .connect_timeout(30)
56
+ .build()
57
+ )
58
+
59
+ register_handlers(app)
60
+
61
+ async def post_init(application):
62
+ asyncio.create_task(cleanup_loop())
63
+ from telegram import BotCommand
64
+ await application.bot.set_my_commands([
65
+ BotCommand("start", "Start the bot"),
66
+ BotCommand("help", "Help and commands"),
67
+ BotCommand("mp3", "Download as MP3"),
68
+ BotCommand("mp4", "Download best quality MP4"),
69
+ BotCommand("best", "Best available quality"),
70
+ BotCommand("platforms", "Supported platforms"),
71
+ BotCommand("settings", "Your preferences"),
72
+ BotCommand("setquality", "Set default quality"),
73
+ BotCommand("setformat", "Set default format"),
74
+ BotCommand("stats", "Bot statistics"),
75
+ ])
76
+ logger.info("Bot commands registered")
77
+
78
+ app.post_init = post_init
79
+ app.run_polling(drop_pending_updates=True)
80
+
81
+
82
+ if __name__ == "__main__":
83
+ main()
bot/cleanup.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import logging
3
+ import os
4
+ import time
5
+ from pathlib import Path
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+ DOWNLOADS_PATH = Path(os.environ.get("DOWNLOADS_PATH", "/downloads"))
10
+ CLEANUP_INTERVAL_SECONDS = int(os.environ.get("CLEANUP_INTERVAL_SECONDS", "300"))
11
+ CLEANUP_MAX_AGE_HOURS = float(os.environ.get("CLEANUP_MAX_AGE_HOURS", "1"))
12
+ CLEANUP_MAX_DISK_MB = int(os.environ.get("CLEANUP_MAX_DISK_MB", "5000"))
13
+
14
+
15
+ async def cleanup_loop():
16
+ logger.info(
17
+ "Cleanup task started: path=%s interval=%ds max_age=%sh max_disk=%dMB",
18
+ DOWNLOADS_PATH, CLEANUP_INTERVAL_SECONDS, CLEANUP_MAX_AGE_HOURS, CLEANUP_MAX_DISK_MB,
19
+ )
20
+ while True:
21
+ await asyncio.sleep(CLEANUP_INTERVAL_SECONDS)
22
+ try:
23
+ _run_cleanup()
24
+ except Exception:
25
+ logger.exception("Cleanup cycle failed")
26
+
27
+
28
+ def _run_cleanup():
29
+ if not DOWNLOADS_PATH.exists():
30
+ return
31
+
32
+ now = time.time()
33
+ max_age_sec = CLEANUP_MAX_AGE_HOURS * 3600
34
+
35
+ for entry in DOWNLOADS_PATH.iterdir():
36
+ if not entry.is_file():
37
+ continue
38
+ try:
39
+ age = now - entry.stat().st_mtime
40
+ if age > max_age_sec:
41
+ entry.unlink()
42
+ logger.info("Deleted (age): %s (%.0fs old)", entry.name, age)
43
+ except PermissionError:
44
+ logger.warning("Permission denied: %s", entry.name)
45
+ except Exception:
46
+ logger.exception("Error checking file: %s", entry.name)
47
+
48
+ _enforce_disk_limit()
49
+
50
+
51
+ def _enforce_disk_limit():
52
+ if not DOWNLOADS_PATH.exists():
53
+ return
54
+
55
+ files = []
56
+ total_bytes = 0
57
+ for entry in DOWNLOADS_PATH.iterdir():
58
+ if not entry.is_file():
59
+ continue
60
+ try:
61
+ st = entry.stat()
62
+ files.append((entry, st.st_mtime, st.st_size))
63
+ total_bytes += st.st_size
64
+ except Exception:
65
+ continue
66
+
67
+ max_bytes = CLEANUP_MAX_DISK_MB * 1024 * 1024
68
+ if total_bytes <= max_bytes:
69
+ return
70
+
71
+ files.sort(key=lambda x: x[1]) # oldest first
72
+ for path, _, size in files:
73
+ if total_bytes <= max_bytes:
74
+ break
75
+ try:
76
+ path.unlink()
77
+ total_bytes -= size
78
+ logger.info("Deleted (disk limit): %s (freed %d bytes)", path.name, size)
79
+ except PermissionError:
80
+ logger.warning("Permission denied: %s", path.name)
81
+ except Exception:
82
+ logger.exception("Error deleting: %s", path.name)
bot/event_client.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+ from datetime import datetime, timezone
4
+
5
+ import httpx
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+ DASHBOARD_URL = os.environ.get("DASHBOARD_URL", "http://dashboard:8080")
10
+ _TIMEOUT = 2.0
11
+
12
+
13
+ def _now_iso() -> str:
14
+ return datetime.now(timezone.utc).isoformat()
15
+
16
+
17
+ async def send_download_start(
18
+ job_id: str,
19
+ user_id: int,
20
+ username: str,
21
+ chat_id: int,
22
+ url: str,
23
+ platform: str,
24
+ format: str,
25
+ quality: str,
26
+ title: str,
27
+ ) -> None:
28
+ try:
29
+ payload = {
30
+ "type": "download_start",
31
+ "ts": _now_iso(),
32
+ "job_id": job_id,
33
+ "user_id": user_id,
34
+ "username": username,
35
+ "chat_id": chat_id,
36
+ "url": url,
37
+ "platform": platform,
38
+ "format": format,
39
+ "quality": quality,
40
+ "title": title,
41
+ }
42
+ async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
43
+ await client.post(f"{DASHBOARD_URL}/api/events", json=payload)
44
+ except Exception as e:
45
+ logger.debug("event_client.send_download_start failed: %s", e)
46
+
47
+
48
+ async def send_progress(
49
+ job_id: str,
50
+ percent: float | None,
51
+ speed: float | None,
52
+ eta: float | None,
53
+ downloaded_bytes: int | None,
54
+ total_bytes: int | None,
55
+ ) -> None:
56
+ try:
57
+ payload = {
58
+ "type": "progress",
59
+ "ts": _now_iso(),
60
+ "job_id": job_id,
61
+ "percent": percent,
62
+ "speed": speed,
63
+ "eta": eta,
64
+ "downloaded_bytes": downloaded_bytes,
65
+ "total_bytes": total_bytes,
66
+ }
67
+ async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
68
+ await client.post(f"{DASHBOARD_URL}/api/events", json=payload)
69
+ except Exception as e:
70
+ logger.debug("event_client.send_progress failed: %s", e)
71
+
72
+
73
+ async def send_download_done(
74
+ job_id: str,
75
+ file_size_bytes: int,
76
+ duration_seconds: float,
77
+ filename: str,
78
+ ) -> None:
79
+ try:
80
+ payload = {
81
+ "type": "download_done",
82
+ "ts": _now_iso(),
83
+ "job_id": job_id,
84
+ "file_size_bytes": file_size_bytes,
85
+ "duration_seconds": duration_seconds,
86
+ "filename": filename,
87
+ }
88
+ async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
89
+ await client.post(f"{DASHBOARD_URL}/api/events", json=payload)
90
+ except Exception as e:
91
+ logger.debug("event_client.send_download_done failed: %s", e)
92
+
93
+
94
+ async def send_download_error(
95
+ job_id: str,
96
+ error_message: str,
97
+ ) -> None:
98
+ try:
99
+ payload = {
100
+ "type": "download_error",
101
+ "ts": _now_iso(),
102
+ "job_id": job_id,
103
+ "error_message": error_message,
104
+ }
105
+ async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
106
+ await client.post(f"{DASHBOARD_URL}/api/events", json=payload)
107
+ except Exception as e:
108
+ logger.debug("event_client.send_download_error failed: %s", e)
bot/handlers.py ADDED
@@ -0,0 +1,732 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import hashlib
3
+ import logging
4
+ import os
5
+ import re
6
+ import time
7
+ from pathlib import Path
8
+
9
+ from telegram import BotCommand, InlineKeyboardButton, InlineKeyboardMarkup, InputMediaPhoto, Update
10
+ from telegram.ext import CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters
11
+
12
+ from reclip_client import (
13
+ ReclipDownloadError,
14
+ ReclipError,
15
+ ReclipInfoError,
16
+ ReclipServiceDown,
17
+ get_info,
18
+ poll_status,
19
+ start_download,
20
+ )
21
+ import event_client
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+ DOWNLOADS_PATH = os.environ.get("DOWNLOADS_PATH", "/downloads")
26
+ URL_REGEX = re.compile(r"https?://[^\s<>\"']+")
27
+ STATE_TTL = 600 # 10 minutes
28
+ CAPTION_MAX = 1000 # Telegram caption limit is 1024, leave headroom
29
+
30
+
31
+ def _truncate_caption(text: str) -> str:
32
+ """Truncate a caption to fit within Telegram's 1024-char limit."""
33
+ if not text:
34
+ return ""
35
+ if len(text) <= CAPTION_MAX:
36
+ return text
37
+ return text[: CAPTION_MAX - 1] + "…"
38
+
39
+ _state: dict[str, dict] = {}
40
+ _stats = {"downloads": 0, "errors": 0, "started": time.time()}
41
+ _user_prefs: dict[int, dict] = {} # user_id -> {"quality": "best"|"720"|"480", "format": "video"|"audio"}
42
+
43
+ SUPPORTED_PLATFORMS = [
44
+ "YouTube", "TikTok", "Instagram", "Twitter/X", "Reddit",
45
+ "Facebook", "Vimeo", "Twitch", "Dailymotion", "SoundCloud",
46
+ "Bandcamp", "Bilibili", "Pinterest", "Tumblr", "Threads",
47
+ "LinkedIn", "Loom", "Streamable", "and 1000+ more via yt-dlp",
48
+ ]
49
+
50
+
51
+ async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
52
+ text = (
53
+ "Hey! I'm ReClip Bot.\n\n"
54
+ "Send me a video or audio link and I'll download it for you.\n\n"
55
+ "Supported platforms: YouTube, TikTok, Instagram, Twitter, Reddit, "
56
+ "and 1000+ more.\n\n"
57
+ "Commands:\n"
58
+ "/help - Help and commands\n"
59
+ "/platforms - Supported platforms\n"
60
+ "/settings - Your preferences\n"
61
+ "/stats - Bot statistics\n"
62
+ "/mp3 <link> - Download directly as MP3\n"
63
+ "/mp4 <link> - Download best quality MP4\n"
64
+ )
65
+ await update.message.reply_text(text)
66
+
67
+
68
+ async def cmd_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
69
+ text = (
70
+ "*How to use ReClip Bot:*\n\n"
71
+ "1\\. Send a link \\(YouTube, TikTok, etc\\.\\)\n"
72
+ "2\\. Pick a format \\(MP4 or MP3\\)\n"
73
+ "3\\. Pick quality\n"
74
+ "4\\. File delivered to the chat\\!\n\n"
75
+ "*Shortcuts:*\n"
76
+ "/mp3 \\<link\\> \\- Direct MP3 download\n"
77
+ "/mp4 \\<link\\> \\- Best quality MP4\n"
78
+ "/best \\<link\\> \\- Best available quality\n\n"
79
+ "*Preferences:*\n"
80
+ "/setquality \\<best/720/480\\> \\- Default quality\n"
81
+ "/setformat \\<video/audio\\> \\- Default format\n"
82
+ "/settings \\- View your preferences\n\n"
83
+ "*Other:*\n"
84
+ "/platforms \\- Supported sites\n"
85
+ "/stats \\- Bot stats\n"
86
+ "/cancel \\- Cancel \\(coming soon\\)\n\n"
87
+ "You can also send multiple links in a single message\\!"
88
+ )
89
+ await update.message.reply_text(text, parse_mode="MarkdownV2")
90
+
91
+
92
+ async def cmd_platforms(update: Update, context: ContextTypes.DEFAULT_TYPE):
93
+ text = "Supported platforms:\n\n" + "\n".join(f" {p}" for p in SUPPORTED_PLATFORMS)
94
+ await update.message.reply_text(text)
95
+
96
+
97
+ async def cmd_stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
98
+ uptime_s = int(time.time() - _stats["started"])
99
+ hours, remainder = divmod(uptime_s, 3600)
100
+ mins, secs = divmod(remainder, 60)
101
+
102
+ downloads_dir = Path(DOWNLOADS_PATH)
103
+ disk_mb = 0
104
+ file_count = 0
105
+ if downloads_dir.exists():
106
+ for f in downloads_dir.iterdir():
107
+ if f.is_file():
108
+ disk_mb += f.stat().st_size / (1024 * 1024)
109
+ file_count += 1
110
+
111
+ text = (
112
+ f"ReClip Bot Stats:\n\n"
113
+ f" Uptime: {hours}h {mins}m {secs}s\n"
114
+ f" Downloads: {_stats['downloads']}\n"
115
+ f" Errors: {_stats['errors']}\n"
116
+ f" Cached files: {file_count}\n"
117
+ f" Disk usage: {disk_mb:.1f} MB\n"
118
+ f" Active sessions: {len(_state)}\n"
119
+ )
120
+ await update.message.reply_text(text)
121
+
122
+
123
+ async def cmd_settings(update: Update, context: ContextTypes.DEFAULT_TYPE):
124
+ uid = update.effective_user.id
125
+ prefs = _user_prefs.get(uid, {})
126
+ quality = prefs.get("quality", "best")
127
+ fmt = prefs.get("format", "video")
128
+ text = (
129
+ f"Your preferences:\n\n"
130
+ f" Default quality: {quality}\n"
131
+ f" Default format: {fmt}\n\n"
132
+ f"Change:\n"
133
+ f" /setquality <best/720/480>\n"
134
+ f" /setformat <video/audio>\n"
135
+ )
136
+ await update.message.reply_text(text)
137
+
138
+
139
+ async def cmd_setquality(update: Update, context: ContextTypes.DEFAULT_TYPE):
140
+ uid = update.effective_user.id
141
+ if not context.args:
142
+ await update.message.reply_text("Usage: /setquality <best/1080/720/480/360>")
143
+ return
144
+ q = context.args[0].lower()
145
+ valid = ["best", "1080", "720", "480", "360"]
146
+ if q not in valid:
147
+ await update.message.reply_text(f"Invalid quality. Options: {', '.join(valid)}")
148
+ return
149
+ _user_prefs.setdefault(uid, {})["quality"] = q
150
+ await update.message.reply_text(f"Default quality set to: {q}")
151
+
152
+
153
+ async def cmd_setformat(update: Update, context: ContextTypes.DEFAULT_TYPE):
154
+ uid = update.effective_user.id
155
+ if not context.args:
156
+ await update.message.reply_text("Usage: /setformat <video/audio>")
157
+ return
158
+ f = context.args[0].lower()
159
+ if f not in ("video", "audio"):
160
+ await update.message.reply_text("Invalid format. Options: video, audio")
161
+ return
162
+ _user_prefs.setdefault(uid, {})["format"] = f
163
+ await update.message.reply_text(f"Default format set to: {f}")
164
+
165
+
166
+ def _extract_urls_from_command(update: Update) -> list[str]:
167
+ """Extract URLs from command text or from the replied-to message."""
168
+ text = update.message.text or ""
169
+ urls = URL_REGEX.findall(text)
170
+ if not urls and update.message.reply_to_message:
171
+ reply_text = update.message.reply_to_message.text or update.message.reply_to_message.caption or ""
172
+ urls = URL_REGEX.findall(reply_text)
173
+ return urls
174
+
175
+
176
+ async def cmd_mp3(update: Update, context: ContextTypes.DEFAULT_TYPE):
177
+ """Direct MP3 download without format picker."""
178
+ urls = _extract_urls_from_command(update)
179
+ if not urls:
180
+ await update.message.reply_text("Usage: /mp3 <link>\nOr reply to a message containing a link.")
181
+ return
182
+ for url in urls:
183
+ msg = await update.message.reply_text("Downloading MP3...")
184
+ await _direct_download(update, msg, url, "audio", None)
185
+
186
+
187
+ async def cmd_mp4(update: Update, context: ContextTypes.DEFAULT_TYPE):
188
+ """Direct best-quality MP4 download without format picker."""
189
+ urls = _extract_urls_from_command(update)
190
+ if not urls:
191
+ await update.message.reply_text("Usage: /mp4 <link>\nOr reply to a message containing a link.")
192
+ return
193
+ for url in urls:
194
+ msg = await update.message.reply_text("Downloading MP4 (best quality)...")
195
+ await _direct_download(update, msg, url, "video", None)
196
+
197
+
198
+ async def cmd_best(update: Update, context: ContextTypes.DEFAULT_TYPE):
199
+ """Alias for /mp4."""
200
+ await cmd_mp4(update, context)
201
+
202
+
203
+ async def _direct_download(update: Update, status_msg, url: str, fmt: str, format_id: str | None):
204
+ """Download without the interactive picker flow."""
205
+ try:
206
+ info = await get_info(url)
207
+ except ReclipServiceDown:
208
+ await _edit_safe(status_msg, "Download service temporarily unavailable.")
209
+ _stats["errors"] += 1
210
+ return
211
+ except ReclipError as e:
212
+ await _edit_safe(status_msg, f"Error: {e}")
213
+ _stats["errors"] += 1
214
+ return
215
+
216
+ title = info.get("title", "download")
217
+ entry = {"url": url, "info": info, "user_id": update.effective_user.id}
218
+
219
+ try:
220
+ job_id = await start_download(url, fmt, format_id, title)
221
+ except ReclipError as e:
222
+ await _edit_safe(status_msg, f"Error: {e}")
223
+ _stats["errors"] += 1
224
+ return
225
+
226
+ try:
227
+ await event_client.send_download_start(
228
+ job_id=job_id,
229
+ user_id=update.effective_user.id,
230
+ username=update.effective_user.username or str(update.effective_user.id),
231
+ chat_id=update.effective_chat.id,
232
+ url=url,
233
+ platform=info.get("extractor", "unknown"),
234
+ format=fmt,
235
+ quality=format_id or "best",
236
+ title=title,
237
+ )
238
+ except Exception:
239
+ pass
240
+
241
+ _direct_download_start = time.time()
242
+ file_path = None
243
+ for _ in range(450):
244
+ await asyncio.sleep(2)
245
+ try:
246
+ status = await poll_status(job_id)
247
+ except ReclipServiceDown:
248
+ await _edit_safe(status_msg, "Download service unavailable.")
249
+ _stats["errors"] += 1
250
+ await event_client.send_download_error(job_id=job_id, error_message="Download service unavailable")
251
+ return
252
+ except ReclipError:
253
+ continue
254
+
255
+ st = status.get("status")
256
+ if st == "done":
257
+ file_path = status.get("file_path") or status.get("filename")
258
+ video_meta = {
259
+ "width": status.get("width"),
260
+ "height": status.get("height"),
261
+ "duration": status.get("duration"),
262
+ }
263
+ break
264
+ elif st == "error":
265
+ await _edit_safe(status_msg, f"Error: {status.get('error', 'Unknown error')}")
266
+ _stats["errors"] += 1
267
+ await event_client.send_download_error(job_id=job_id, error_message=status.get("error", "Unknown error"))
268
+ return
269
+ else:
270
+ progress = status.get("progress")
271
+ if progress and isinstance(progress, dict) and progress.get("percent") is not None:
272
+ text = f"Downloading... {progress['percent']}%"
273
+ try:
274
+ await event_client.send_progress(
275
+ job_id=job_id,
276
+ percent=progress.get("percent"),
277
+ speed=progress.get("speed"),
278
+ eta=progress.get("eta"),
279
+ downloaded_bytes=progress.get("downloaded_bytes"),
280
+ total_bytes=progress.get("total_bytes"),
281
+ )
282
+ except Exception:
283
+ pass
284
+ else:
285
+ text = "Downloading..."
286
+ try:
287
+ await status_msg.edit_text(text)
288
+ except Exception:
289
+ pass
290
+
291
+ if not file_path:
292
+ await _edit_safe(status_msg, "Download timed out.")
293
+ _stats["errors"] += 1
294
+ await event_client.send_download_error(job_id=job_id, error_message="Download timed out")
295
+ return
296
+
297
+ local_path = Path(DOWNLOADS_PATH) / Path(file_path).name
298
+ if not local_path.exists():
299
+ local_path = Path(file_path)
300
+ if not local_path.exists():
301
+ await _edit_safe(status_msg, "File not found after download.")
302
+ _stats["errors"] += 1
303
+ await event_client.send_download_error(job_id=job_id, error_message="File not found after download")
304
+ return
305
+
306
+ for attempt in range(2):
307
+ try:
308
+ with open(local_path, "rb") as f:
309
+ if fmt == "video" and local_path.suffix.lower() == ".mp4":
310
+ dur = video_meta.get("duration")
311
+ await update.message.chat.send_video(
312
+ video=f,
313
+ caption=_truncate_caption(title),
314
+ supports_streaming=True,
315
+ width=video_meta.get("width"),
316
+ height=video_meta.get("height"),
317
+ duration=int(dur) if dur else None,
318
+ )
319
+ else:
320
+ await update.message.chat.send_document(document=f, caption=_truncate_caption(title))
321
+ _stats["downloads"] += 1
322
+ try:
323
+ await event_client.send_download_done(
324
+ job_id=job_id,
325
+ file_size_bytes=local_path.stat().st_size,
326
+ duration_seconds=time.time() - _direct_download_start,
327
+ filename=local_path.name,
328
+ )
329
+ except Exception:
330
+ pass
331
+ break
332
+ except Exception:
333
+ if attempt == 0:
334
+ await asyncio.sleep(1)
335
+ else:
336
+ await _edit_safe(status_msg, "Failed to upload file to Telegram.")
337
+ _stats["errors"] += 1
338
+ await event_client.send_download_error(job_id=job_id, error_message="Failed to upload to Telegram")
339
+ return
340
+
341
+ try:
342
+ await status_msg.edit_text(f"Sent: {title}")
343
+ except Exception:
344
+ pass
345
+ try:
346
+ local_path.unlink()
347
+ except Exception:
348
+ pass
349
+
350
+
351
+ def _state_key(chat_id: int, message_id: int, url_hash: str) -> str:
352
+ return f"{chat_id}:{message_id}:{url_hash}"
353
+
354
+
355
+ def _url_hash(url: str) -> str:
356
+ return hashlib.md5(url.encode()).hexdigest()[:8]
357
+
358
+
359
+ def _evict_stale():
360
+ now = time.time()
361
+ expired = [k for k, v in _state.items() if now - v["created"] > STATE_TTL]
362
+ for k in expired:
363
+ del _state[k]
364
+
365
+
366
+ def _format_duration(seconds: int | float | None) -> str:
367
+ if not seconds:
368
+ return "Unknown"
369
+ seconds = int(seconds)
370
+ mins, secs = divmod(seconds, 60)
371
+ hours, mins = divmod(mins, 60)
372
+ if hours:
373
+ return f"{hours}:{mins:02d}:{secs:02d}"
374
+ return f"{mins}:{secs:02d}"
375
+
376
+
377
+ def _build_format_buttons(message_id: int, url_hash: str) -> InlineKeyboardMarkup:
378
+ return InlineKeyboardMarkup([
379
+ [
380
+ InlineKeyboardButton("MP4", callback_data=f"fmt:{message_id}:{url_hash}:video"),
381
+ InlineKeyboardButton("MP3", callback_data=f"fmt:{message_id}:{url_hash}:audio"),
382
+ ]
383
+ ])
384
+
385
+
386
+ def _build_quality_buttons(message_id: int, url_hash: str, formats: list[dict]) -> InlineKeyboardMarkup:
387
+ buttons = []
388
+ for fmt in formats:
389
+ label = fmt.get("label", fmt.get("id", "?"))
390
+ buttons.append(
391
+ InlineKeyboardButton(label, callback_data=f"qty:{message_id}:{url_hash}:{fmt['id']}")
392
+ )
393
+ rows = [buttons[i : i + 3] for i in range(0, len(buttons), 3)]
394
+ rows.append([InlineKeyboardButton("Best quality", callback_data=f"qty:{message_id}:{url_hash}:best")])
395
+ return InlineKeyboardMarkup(rows)
396
+
397
+
398
+ async def url_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
399
+ _evict_stale()
400
+ text = update.message.text or ""
401
+ urls = URL_REGEX.findall(text)
402
+ if not urls:
403
+ return
404
+
405
+ for url in urls:
406
+ uhash = _url_hash(url)
407
+ status_msg = await update.message.reply_text("Fetching info...")
408
+
409
+ try:
410
+ info = await get_info(url)
411
+ except ReclipServiceDown:
412
+ await status_msg.edit_text("Download service temporarily unavailable.")
413
+ continue
414
+ except ReclipInfoError as e:
415
+ await status_msg.edit_text(f"Failed to fetch info: {e}")
416
+ continue
417
+ except ReclipError as e:
418
+ await status_msg.edit_text(f"Error: {e}")
419
+ continue
420
+
421
+ title = info.get("title", "Unknown")
422
+ extractor = info.get("extractor", "Unknown")
423
+ duration = _format_duration(info.get("duration"))
424
+ uploader = info.get("uploader", "")
425
+ thumbnail = info.get("thumbnail")
426
+
427
+ # Telegram caption limit is 1024 chars. Reserve ~200 for metadata lines
428
+ # and markdown escaping overhead, cap the title at 800 chars.
429
+ title_short = title if len(title) <= 800 else title[:799] + "…"
430
+
431
+ caption_lines = [
432
+ f"*{_escape_md(title_short)}*",
433
+ f"Platform: {_escape_md(extractor)}",
434
+ f"Duration: {_escape_md(duration)}",
435
+ ]
436
+ if uploader:
437
+ caption_lines.append(f"Uploader: {_escape_md(uploader)}")
438
+ caption = "\n".join(caption_lines)
439
+
440
+ key = _state_key(update.effective_chat.id, status_msg.message_id, uhash)
441
+ _state[key] = {
442
+ "url": url,
443
+ "user_id": update.effective_user.id,
444
+ "info": info,
445
+ "message_id": status_msg.message_id,
446
+ "created": time.time(),
447
+ }
448
+
449
+ keyboard = _build_format_buttons(status_msg.message_id, uhash)
450
+
451
+ if thumbnail:
452
+ try:
453
+ await status_msg.delete()
454
+ sent = await update.message.reply_photo(
455
+ photo=thumbnail,
456
+ caption=caption,
457
+ parse_mode="MarkdownV2",
458
+ reply_markup=keyboard,
459
+ )
460
+ _state[key]["message_id"] = sent.message_id
461
+ old_key = key
462
+ key = _state_key(update.effective_chat.id, sent.message_id, uhash)
463
+ _state[key] = _state.pop(old_key)
464
+ except Exception:
465
+ logger.exception("Failed to send thumbnail, falling back to text")
466
+ sent = await update.message.reply_text(
467
+ caption, parse_mode="MarkdownV2", reply_markup=keyboard
468
+ )
469
+ _state[key]["message_id"] = sent.message_id
470
+ old_key = key
471
+ key = _state_key(update.effective_chat.id, sent.message_id, uhash)
472
+ _state[key] = _state.pop(old_key)
473
+ else:
474
+ await status_msg.edit_text(caption, parse_mode="MarkdownV2", reply_markup=keyboard)
475
+
476
+
477
+ def _escape_md(text: str) -> str:
478
+ special = r"_*[]()~`>#+-=|{}.!\\"
479
+ return "".join(f"\\{c}" if c in special else c for c in str(text))
480
+
481
+
482
+ async def format_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
483
+ _evict_stale()
484
+ query = update.callback_query
485
+ await query.answer()
486
+
487
+ parts = query.data.split(":")
488
+ if len(parts) != 4:
489
+ return
490
+ _, msg_id_str, uhash, fmt = parts
491
+ msg_id = int(msg_id_str)
492
+
493
+ key = _state_key(query.message.chat_id, msg_id, uhash)
494
+ entry = _state.get(key)
495
+ if not entry:
496
+ key = _state_key(query.message.chat_id, query.message.message_id, uhash)
497
+ entry = _state.get(key)
498
+ if not entry:
499
+ await query.edit_message_text("Session expired. Please send the link again.")
500
+ return
501
+
502
+ if fmt == "back":
503
+ keyboard = _build_format_buttons(query.message.message_id, uhash)
504
+ await query.edit_message_reply_markup(reply_markup=keyboard)
505
+ return
506
+
507
+ if fmt == "audio":
508
+ asyncio.create_task(
509
+ download_and_send(query, entry, format="audio", format_id=None)
510
+ )
511
+ elif fmt == "video":
512
+ formats = entry["info"].get("formats", [])
513
+ if not formats:
514
+ asyncio.create_task(
515
+ download_and_send(query, entry, format="video", format_id=None)
516
+ )
517
+ return
518
+
519
+ keyboard = _build_quality_buttons(query.message.message_id, uhash, formats[:6])
520
+ await query.edit_message_reply_markup(reply_markup=keyboard)
521
+
522
+
523
+ async def quality_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
524
+ _evict_stale()
525
+ query = update.callback_query
526
+ await query.answer()
527
+
528
+ parts = query.data.split(":")
529
+ if len(parts) != 4:
530
+ return
531
+ _, msg_id_str, uhash, format_id = parts
532
+ msg_id = int(msg_id_str)
533
+
534
+ key = _state_key(query.message.chat_id, msg_id, uhash)
535
+ entry = _state.get(key)
536
+ if not entry:
537
+ key = _state_key(query.message.chat_id, query.message.message_id, uhash)
538
+ entry = _state.get(key)
539
+ if not entry:
540
+ await query.edit_message_text("Session expired. Please send the link again.")
541
+ return
542
+
543
+ fid = None if format_id == "best" else format_id
544
+ asyncio.create_task(
545
+ download_and_send(query, entry, format="video", format_id=fid)
546
+ )
547
+
548
+
549
+ async def download_and_send(query, entry: dict, format: str, format_id: str | None):
550
+ chat_id = query.message.chat_id
551
+ message = query.message
552
+ url = entry["url"]
553
+ title = entry["info"].get("title", "download")
554
+
555
+ try:
556
+ await message.edit_caption(caption="Starting download...") if message.photo else await message.edit_text("Starting download...")
557
+ except Exception:
558
+ pass
559
+
560
+ try:
561
+ job_id = await start_download(url, format, format_id, title)
562
+ except ReclipServiceDown:
563
+ await _edit_safe(message, "Download service temporarily unavailable.")
564
+ _stats["errors"] += 1
565
+ return
566
+ except ReclipDownloadError as e:
567
+ await _edit_safe(message, f"Download failed: {e}")
568
+ _stats["errors"] += 1
569
+ return
570
+ except ReclipError as e:
571
+ await _edit_safe(message, f"Error: {e}")
572
+ _stats["errors"] += 1
573
+ return
574
+
575
+ try:
576
+ await event_client.send_download_start(
577
+ job_id=job_id,
578
+ user_id=entry["user_id"],
579
+ username=str(entry.get("user_id", "")),
580
+ chat_id=chat_id,
581
+ url=url,
582
+ platform=entry["info"].get("extractor", "unknown"),
583
+ format=format,
584
+ quality=format_id or "best",
585
+ title=title,
586
+ )
587
+ except Exception:
588
+ pass
589
+
590
+ file_path = None
591
+ for _ in range(450):
592
+ await asyncio.sleep(2)
593
+ try:
594
+ status = await poll_status(job_id)
595
+ except ReclipServiceDown:
596
+ await _edit_safe(message, "Download service temporarily unavailable.")
597
+ _stats["errors"] += 1
598
+ await event_client.send_download_error(job_id=job_id, error_message="Download service temporarily unavailable")
599
+ return
600
+ except ReclipError:
601
+ continue
602
+
603
+ st = status.get("status")
604
+ if st == "done":
605
+ file_path = status.get("file_path") or status.get("filename")
606
+ video_meta = {
607
+ "width": status.get("width"),
608
+ "height": status.get("height"),
609
+ "duration": status.get("duration"),
610
+ }
611
+ break
612
+ elif st == "error":
613
+ await _edit_safe(message, f"Error: {status.get('error', 'Unknown error')}")
614
+ _stats["errors"] += 1
615
+ await event_client.send_download_error(job_id=job_id, error_message=status.get("error", "Unknown error"))
616
+ return
617
+ else:
618
+ progress = status.get("progress")
619
+ if progress and isinstance(progress, dict) and progress.get("percent") is not None:
620
+ text = f"Downloading... {progress['percent']}%"
621
+ try:
622
+ await event_client.send_progress(
623
+ job_id=job_id,
624
+ percent=progress.get("percent"),
625
+ speed=progress.get("speed"),
626
+ eta=progress.get("eta"),
627
+ downloaded_bytes=progress.get("downloaded_bytes"),
628
+ total_bytes=progress.get("total_bytes"),
629
+ )
630
+ except Exception:
631
+ pass
632
+ else:
633
+ text = "Downloading..."
634
+ try:
635
+ if message.photo:
636
+ await message.edit_caption(caption=text)
637
+ else:
638
+ await message.edit_text(text)
639
+ except Exception:
640
+ pass
641
+
642
+ if not file_path:
643
+ await _edit_safe(message, "Download timed out.")
644
+ _stats["errors"] += 1
645
+ await event_client.send_download_error(job_id=job_id, error_message="Download timed out")
646
+ return
647
+
648
+ local_path = Path(DOWNLOADS_PATH) / Path(file_path).name
649
+ if not local_path.exists():
650
+ local_path = Path(file_path)
651
+ if not local_path.exists():
652
+ await _edit_safe(message, "File not found after download.")
653
+ _stats["errors"] += 1
654
+ await event_client.send_download_error(job_id=job_id, error_message="File not found after download")
655
+ return
656
+
657
+ for attempt in range(2):
658
+ try:
659
+ with open(local_path, "rb") as f:
660
+ if format == "video" and local_path.suffix.lower() == ".mp4":
661
+ dur = video_meta.get("duration")
662
+ await query.message.chat.send_video(
663
+ video=f,
664
+ caption=_truncate_caption(title),
665
+ supports_streaming=True,
666
+ width=video_meta.get("width"),
667
+ height=video_meta.get("height"),
668
+ duration=int(dur) if dur else None,
669
+ )
670
+ else:
671
+ await query.message.chat.send_document(document=f, caption=_truncate_caption(title))
672
+ _stats["downloads"] += 1
673
+ try:
674
+ await event_client.send_download_done(
675
+ job_id=job_id,
676
+ file_size_bytes=local_path.stat().st_size,
677
+ duration_seconds=time.time() - entry["created"],
678
+ filename=local_path.name,
679
+ )
680
+ except Exception:
681
+ pass
682
+ break
683
+ except Exception:
684
+ if attempt == 0:
685
+ logger.warning("Upload failed, retrying once")
686
+ await asyncio.sleep(1)
687
+ else:
688
+ logger.exception("Upload failed after retry")
689
+ await _edit_safe(message, "Failed to upload file to Telegram.")
690
+ _stats["errors"] += 1
691
+ await event_client.send_download_error(job_id=job_id, error_message="Failed to upload to Telegram")
692
+ return
693
+
694
+ try:
695
+ sent_text = _truncate_caption(f"Sent: {title}")
696
+ if message.photo:
697
+ await message.edit_caption(caption=sent_text)
698
+ else:
699
+ await message.edit_text(sent_text)
700
+ except Exception:
701
+ pass
702
+
703
+ try:
704
+ local_path.unlink()
705
+ except Exception:
706
+ logger.debug("Could not delete %s", local_path)
707
+
708
+
709
+ async def _edit_safe(message, text: str):
710
+ try:
711
+ if message.photo:
712
+ await message.edit_caption(caption=text)
713
+ else:
714
+ await message.edit_text(text)
715
+ except Exception:
716
+ logger.debug("Failed to edit message with: %s", text)
717
+
718
+
719
+ def register_handlers(application):
720
+ application.add_handler(CommandHandler("start", cmd_start))
721
+ application.add_handler(CommandHandler("help", cmd_help))
722
+ application.add_handler(CommandHandler("platforms", cmd_platforms))
723
+ application.add_handler(CommandHandler("stats", cmd_stats))
724
+ application.add_handler(CommandHandler("settings", cmd_settings))
725
+ application.add_handler(CommandHandler("setquality", cmd_setquality))
726
+ application.add_handler(CommandHandler("setformat", cmd_setformat))
727
+ application.add_handler(CommandHandler("mp3", cmd_mp3))
728
+ application.add_handler(CommandHandler("mp4", cmd_mp4))
729
+ application.add_handler(CommandHandler("best", cmd_best))
730
+ application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, url_handler))
731
+ application.add_handler(CallbackQueryHandler(format_callback, pattern=r"^fmt:"))
732
+ application.add_handler(CallbackQueryHandler(quality_callback, pattern=r"^qty:"))
bot/reclip_client.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import logging
3
+ import httpx
4
+
5
+ logger = logging.getLogger(__name__)
6
+
7
+ RECLIP_URL = os.environ.get("RECLIP_URL", "http://reclip:8899")
8
+
9
+
10
+ class ReclipError(Exception):
11
+ pass
12
+
13
+
14
+ class ReclipInfoError(ReclipError):
15
+ pass
16
+
17
+
18
+ class ReclipDownloadError(ReclipError):
19
+ pass
20
+
21
+
22
+ class ReclipServiceDown(ReclipError):
23
+ pass
24
+
25
+
26
+ def _client() -> httpx.AsyncClient:
27
+ return httpx.AsyncClient(base_url=RECLIP_URL)
28
+
29
+
30
+ async def get_info(url: str) -> dict:
31
+ try:
32
+ async with _client() as client:
33
+ resp = await client.post("/api/info", json={"url": url}, timeout=60.0)
34
+ resp.raise_for_status()
35
+ data = resp.json()
36
+ return data
37
+ except httpx.ConnectError:
38
+ raise ReclipServiceDown("Cannot reach reclip service")
39
+ except httpx.TimeoutException:
40
+ raise ReclipInfoError("Info request timed out")
41
+ except httpx.HTTPStatusError as e:
42
+ raise ReclipInfoError(f"Info request failed: {e.response.status_code}")
43
+ except Exception as e:
44
+ raise ReclipInfoError(f"Info request failed: {e}")
45
+
46
+
47
+ async def start_download(url: str, format: str, format_id: str | None, title: str) -> str:
48
+ payload = {"url": url, "format": format, "title": title}
49
+ if format_id:
50
+ payload["format_id"] = format_id
51
+ try:
52
+ async with _client() as client:
53
+ resp = await client.post("/api/download", json=payload, timeout=60.0)
54
+ resp.raise_for_status()
55
+ data = resp.json()
56
+ return data["job_id"]
57
+ except httpx.ConnectError:
58
+ raise ReclipServiceDown("Cannot reach reclip service")
59
+ except httpx.TimeoutException:
60
+ raise ReclipDownloadError("Download request timed out")
61
+ except httpx.HTTPStatusError as e:
62
+ raise ReclipDownloadError(f"Download request failed: {e.response.status_code}")
63
+ except (KeyError, ValueError) as e:
64
+ raise ReclipDownloadError(f"Malformed response: {e}")
65
+ except ReclipError:
66
+ raise
67
+ except Exception as e:
68
+ raise ReclipDownloadError(f"Download request failed: {e}")
69
+
70
+
71
+ async def poll_status(job_id: str) -> dict:
72
+ try:
73
+ async with _client() as client:
74
+ resp = await client.get(f"/api/status/{job_id}", timeout=10.0)
75
+ resp.raise_for_status()
76
+ return resp.json()
77
+ except httpx.ConnectError:
78
+ raise ReclipServiceDown("Cannot reach reclip service")
79
+ except httpx.TimeoutException:
80
+ raise ReclipError("Status request timed out")
81
+ except httpx.HTTPStatusError as e:
82
+ raise ReclipError(f"Status request failed: {e.response.status_code}")
83
+ except Exception as e:
84
+ raise ReclipError(f"Status request failed: {e}")
bot/requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ python-telegram-bot>=20.0
2
+ httpx>=0.25.0
bot/tests/__init__.py ADDED
File without changes
bot/tests/test_cleanup.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import tempfile
4
+ from pathlib import Path
5
+
6
+ import sys
7
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
8
+
9
+ import cleanup
10
+
11
+
12
+ def test_deletes_old_files(tmp_path):
13
+ old_file = tmp_path / "old.mp4"
14
+ old_file.write_bytes(b"x" * 100)
15
+ os.utime(old_file, (time.time() - 7200, time.time() - 7200))
16
+
17
+ new_file = tmp_path / "new.mp4"
18
+ new_file.write_bytes(b"x" * 100)
19
+
20
+ cleanup.DOWNLOADS_PATH = tmp_path
21
+ cleanup.CLEANUP_MAX_AGE_HOURS = 1
22
+ cleanup.CLEANUP_MAX_DISK_MB = 99999
23
+
24
+ cleanup._run_cleanup()
25
+
26
+ assert not old_file.exists()
27
+ assert new_file.exists()
28
+
29
+
30
+ def test_enforces_disk_limit(tmp_path):
31
+ for i in range(5):
32
+ f = tmp_path / f"file{i}.mp4"
33
+ f.write_bytes(b"x" * 1024 * 1024)
34
+ os.utime(f, (time.time() - i * 10, time.time() - i * 10))
35
+
36
+ cleanup.DOWNLOADS_PATH = tmp_path
37
+ cleanup.CLEANUP_MAX_AGE_HOURS = 999
38
+ cleanup.CLEANUP_MAX_DISK_MB = 3
39
+
40
+ cleanup._run_cleanup()
41
+ cleanup._enforce_disk_limit()
42
+
43
+ remaining = list(tmp_path.iterdir())
44
+ total_mb = sum(f.stat().st_size for f in remaining) / (1024 * 1024)
45
+ assert total_mb <= 3
46
+
47
+
48
+ def test_handles_permission_error(tmp_path):
49
+ f = tmp_path / "locked.mp4"
50
+ f.write_bytes(b"x" * 100)
51
+ os.utime(f, (time.time() - 7200, time.time() - 7200))
52
+
53
+ cleanup.DOWNLOADS_PATH = tmp_path
54
+ cleanup.CLEANUP_MAX_AGE_HOURS = 1
55
+
56
+ # Make directory non-writable so unlink fails
57
+ os.chmod(tmp_path, 0o555)
58
+ try:
59
+ cleanup._run_cleanup()
60
+ # Should not crash, just log and skip
61
+ assert f.exists()
62
+ finally:
63
+ os.chmod(tmp_path, 0o755)
64
+
65
+
66
+ def test_skips_directories(tmp_path):
67
+ subdir = tmp_path / "subdir"
68
+ subdir.mkdir()
69
+
70
+ cleanup.DOWNLOADS_PATH = tmp_path
71
+ cleanup.CLEANUP_MAX_AGE_HOURS = 0
72
+ cleanup._run_cleanup()
73
+ assert subdir.exists()
74
+
75
+
76
+ def test_empty_directory(tmp_path):
77
+ cleanup.DOWNLOADS_PATH = tmp_path
78
+ cleanup._run_cleanup()
79
+
80
+
81
+ def test_nonexistent_directory():
82
+ cleanup.DOWNLOADS_PATH = Path("/nonexistent/path")
83
+ cleanup._run_cleanup()
bot/tests/test_event_client.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests that event_client functions are fire-and-forget (never raise)."""
2
+ import asyncio
3
+ import os
4
+
5
+ # Point at an unreachable URL before importing event_client
6
+ os.environ["DASHBOARD_URL"] = "http://localhost:99999"
7
+
8
+ import event_client
9
+
10
+
11
+ def test_send_download_start_no_raise():
12
+ asyncio.run(event_client.send_download_start(
13
+ job_id="job-1",
14
+ user_id=12345,
15
+ username="testuser",
16
+ chat_id=67890,
17
+ url="https://example.com/video",
18
+ platform="youtube",
19
+ format="video",
20
+ quality="best",
21
+ title="Test Video",
22
+ ))
23
+
24
+
25
+ def test_send_progress_no_raise():
26
+ asyncio.run(event_client.send_progress(
27
+ job_id="job-1",
28
+ percent=42.5,
29
+ speed=1024000.0,
30
+ eta=30.0,
31
+ downloaded_bytes=10485760,
32
+ total_bytes=25165824,
33
+ ))
34
+
35
+
36
+ def test_send_download_done_no_raise():
37
+ asyncio.run(event_client.send_download_done(
38
+ job_id="job-1",
39
+ file_size_bytes=25165824,
40
+ duration_seconds=12.5,
41
+ filename="video.mp4",
42
+ ))
43
+
44
+
45
+ def test_send_download_error_no_raise():
46
+ asyncio.run(event_client.send_download_error(
47
+ job_id="job-1",
48
+ error_message="Download timed out",
49
+ ))
bot/tests/test_reclip_client.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import httpx
3
+ from unittest.mock import AsyncMock, patch
4
+
5
+ import sys, os
6
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
7
+
8
+ from reclip_client import (
9
+ get_info,
10
+ start_download,
11
+ poll_status,
12
+ ReclipInfoError,
13
+ ReclipDownloadError,
14
+ ReclipServiceDown,
15
+ ReclipError,
16
+ )
17
+
18
+
19
+ @pytest.fixture
20
+ def mock_response():
21
+ def _make(status_code=200, json_data=None):
22
+ from unittest.mock import MagicMock
23
+ resp = MagicMock(spec=httpx.Response)
24
+ resp.status_code = status_code
25
+ resp.json.return_value = json_data or {}
26
+ if status_code >= 400:
27
+ resp.raise_for_status.side_effect = httpx.HTTPStatusError(
28
+ "error", request=MagicMock(), response=resp
29
+ )
30
+ return resp
31
+ return _make
32
+
33
+
34
+ class TestGetInfo:
35
+ @pytest.mark.asyncio
36
+ async def test_success(self, mock_response):
37
+ info = {
38
+ "title": "Test Video",
39
+ "thumbnail": "https://example.com/thumb.jpg",
40
+ "duration": 120,
41
+ "uploader": "TestUser",
42
+ "extractor": "youtube",
43
+ "formats": [{"id": "22", "label": "720p", "height": 720}],
44
+ }
45
+ resp = mock_response(200, info)
46
+ with patch("reclip_client._client") as mock_client:
47
+ client = AsyncMock()
48
+ client.__aenter__ = AsyncMock(return_value=client)
49
+ client.__aexit__ = AsyncMock(return_value=False)
50
+ client.post = AsyncMock(return_value=resp)
51
+ mock_client.return_value = client
52
+
53
+ result = await get_info("https://youtube.com/watch?v=test")
54
+ assert result["title"] == "Test Video"
55
+ assert result["extractor"] == "youtube"
56
+
57
+ @pytest.mark.asyncio
58
+ async def test_connection_error(self):
59
+ with patch("reclip_client._client") as mock_client:
60
+ client = AsyncMock()
61
+ client.__aenter__ = AsyncMock(return_value=client)
62
+ client.__aexit__ = AsyncMock(return_value=False)
63
+ client.post = AsyncMock(side_effect=httpx.ConnectError("refused"))
64
+ mock_client.return_value = client
65
+
66
+ with pytest.raises(ReclipServiceDown):
67
+ await get_info("https://youtube.com/watch?v=test")
68
+
69
+ @pytest.mark.asyncio
70
+ async def test_timeout(self):
71
+ with patch("reclip_client._client") as mock_client:
72
+ client = AsyncMock()
73
+ client.__aenter__ = AsyncMock(return_value=client)
74
+ client.__aexit__ = AsyncMock(return_value=False)
75
+ client.post = AsyncMock(side_effect=httpx.TimeoutException("timeout"))
76
+ mock_client.return_value = client
77
+
78
+ with pytest.raises(ReclipInfoError, match="timed out"):
79
+ await get_info("https://youtube.com/watch?v=test")
80
+
81
+ @pytest.mark.asyncio
82
+ async def test_http_error(self, mock_response):
83
+ resp = mock_response(400, {"error": "Unsupported URL"})
84
+ with patch("reclip_client._client") as mock_client:
85
+ client = AsyncMock()
86
+ client.__aenter__ = AsyncMock(return_value=client)
87
+ client.__aexit__ = AsyncMock(return_value=False)
88
+ client.post = AsyncMock(return_value=resp)
89
+ mock_client.return_value = client
90
+
91
+ with pytest.raises(ReclipInfoError):
92
+ await get_info("https://invalid-site.com/nope")
93
+
94
+
95
+ class TestStartDownload:
96
+ @pytest.mark.asyncio
97
+ async def test_success(self, mock_response):
98
+ resp = mock_response(200, {"job_id": "abc1234567"})
99
+ with patch("reclip_client._client") as mock_client:
100
+ client = AsyncMock()
101
+ client.__aenter__ = AsyncMock(return_value=client)
102
+ client.__aexit__ = AsyncMock(return_value=False)
103
+ client.post = AsyncMock(return_value=resp)
104
+ mock_client.return_value = client
105
+
106
+ job_id = await start_download("https://youtube.com/watch?v=test", "video", "22", "Test")
107
+ assert job_id == "abc1234567"
108
+
109
+ @pytest.mark.asyncio
110
+ async def test_service_down(self):
111
+ with patch("reclip_client._client") as mock_client:
112
+ client = AsyncMock()
113
+ client.__aenter__ = AsyncMock(return_value=client)
114
+ client.__aexit__ = AsyncMock(return_value=False)
115
+ client.post = AsyncMock(side_effect=httpx.ConnectError("refused"))
116
+ mock_client.return_value = client
117
+
118
+ with pytest.raises(ReclipServiceDown):
119
+ await start_download("https://youtube.com/watch?v=test", "video", None, "Test")
120
+
121
+
122
+ class TestPollStatus:
123
+ @pytest.mark.asyncio
124
+ async def test_done(self, mock_response):
125
+ data = {
126
+ "status": "done",
127
+ "filename": "test.mp4",
128
+ "file_path": "/downloads/abc1234567.mp4",
129
+ "progress": None,
130
+ }
131
+ resp = mock_response(200, data)
132
+ with patch("reclip_client._client") as mock_client:
133
+ client = AsyncMock()
134
+ client.__aenter__ = AsyncMock(return_value=client)
135
+ client.__aexit__ = AsyncMock(return_value=False)
136
+ client.get = AsyncMock(return_value=resp)
137
+ mock_client.return_value = client
138
+
139
+ result = await poll_status("abc1234567")
140
+ assert result["status"] == "done"
141
+ assert result["file_path"] == "/downloads/abc1234567.mp4"
142
+
143
+ @pytest.mark.asyncio
144
+ async def test_downloading_with_progress(self, mock_response):
145
+ data = {
146
+ "status": "downloading",
147
+ "progress": {"percent": 45.2, "downloaded_bytes": 23000000, "total_bytes": 51000000},
148
+ }
149
+ resp = mock_response(200, data)
150
+ with patch("reclip_client._client") as mock_client:
151
+ client = AsyncMock()
152
+ client.__aenter__ = AsyncMock(return_value=client)
153
+ client.__aexit__ = AsyncMock(return_value=False)
154
+ client.get = AsyncMock(return_value=resp)
155
+ mock_client.return_value = client
156
+
157
+ result = await poll_status("abc1234567")
158
+ assert result["status"] == "downloading"
159
+ assert result["progress"]["percent"] == 45.2
160
+
161
+ @pytest.mark.asyncio
162
+ async def test_error(self, mock_response):
163
+ data = {"status": "error", "error": "Download timed out (5 min limit)"}
164
+ resp = mock_response(200, data)
165
+ with patch("reclip_client._client") as mock_client:
166
+ client = AsyncMock()
167
+ client.__aenter__ = AsyncMock(return_value=client)
168
+ client.__aexit__ = AsyncMock(return_value=False)
169
+ client.get = AsyncMock(return_value=resp)
170
+ mock_client.return_value = client
171
+
172
+ result = await poll_status("abc1234567")
173
+ assert result["status"] == "error"
bot/tests/test_url_extract.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys, os
2
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
3
+
4
+ from handlers import URL_REGEX
5
+
6
+
7
+ def test_simple_url():
8
+ urls = URL_REGEX.findall("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
9
+ assert len(urls) == 1
10
+ assert "youtube.com" in urls[0]
11
+
12
+
13
+ def test_url_in_text():
14
+ urls = URL_REGEX.findall("check this out https://www.tiktok.com/@user/video/123 cool right?")
15
+ assert len(urls) == 1
16
+ assert "tiktok.com" in urls[0]
17
+
18
+
19
+ def test_multiple_urls():
20
+ text = "https://youtube.com/watch?v=abc and also https://tiktok.com/@user/video/456"
21
+ urls = URL_REGEX.findall(text)
22
+ assert len(urls) == 2
23
+
24
+
25
+ def test_no_url():
26
+ urls = URL_REGEX.findall("just some text without links")
27
+ assert len(urls) == 0
28
+
29
+
30
+ def test_short_url():
31
+ urls = URL_REGEX.findall("https://youtu.be/dQw4w9WgXcQ")
32
+ assert len(urls) == 1
33
+
34
+
35
+ def test_instagram_reel():
36
+ urls = URL_REGEX.findall("https://www.instagram.com/reel/ABC123/")
37
+ assert len(urls) == 1
38
+ assert "instagram.com" in urls[0]
39
+
40
+
41
+ def test_twitter_url():
42
+ urls = URL_REGEX.findall("https://x.com/user/status/123456789")
43
+ assert len(urls) == 1
44
+
45
+
46
+ def test_reddit_url():
47
+ urls = URL_REGEX.findall("https://www.reddit.com/r/videos/comments/abc123/test/")
48
+ assert len(urls) == 1
49
+
50
+
51
+ def test_url_with_query_params():
52
+ urls = URL_REGEX.findall("https://www.youtube.com/watch?v=abc&list=PLxyz&index=3")
53
+ assert len(urls) == 1
54
+ assert "list=PLxyz" in urls[0]
dashboard/Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ CMD ["uvicorn", "main:create_app", "--factory", "--host", "0.0.0.0", "--port", "8080"]
dashboard/auth.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Authentication module for the reclip_bot admin dashboard."""
2
+ import os
3
+ from typing import Optional
4
+
5
+ from itsdangerous import URLSafeTimedSerializer, BadSignature, SignatureExpired
6
+ from fastapi import Request, Response
7
+
8
+ COOKIE_NAME = "reclip_session"
9
+ _MAX_AGE_SECONDS = 86400 # 24 hours
10
+
11
+
12
+ def _admin_user() -> str:
13
+ return os.environ.get("ADMIN_USER", "admin")
14
+
15
+
16
+ def _admin_password() -> str:
17
+ pw = os.environ.get("ADMIN_PASSWORD")
18
+ if not pw:
19
+ raise RuntimeError("ADMIN_PASSWORD environment variable is required")
20
+ return pw
21
+
22
+
23
+ def _secret_key() -> str:
24
+ return os.environ.get("SECRET_KEY", "insecure-default-key-change-me")
25
+
26
+
27
+ def _serializer() -> URLSafeTimedSerializer:
28
+ return URLSafeTimedSerializer(_secret_key())
29
+
30
+
31
+ def verify_credentials(username: str, password: str) -> bool:
32
+ """Return True if credentials match env-configured admin user/password."""
33
+ return username == _admin_user() and password == _admin_password()
34
+
35
+
36
+ def create_session_cookie(response: Response, username: str) -> None:
37
+ """Sign and set the session cookie on the response."""
38
+ token = _serializer().dumps(username)
39
+ response.set_cookie(
40
+ key=COOKIE_NAME,
41
+ value=token,
42
+ max_age=_MAX_AGE_SECONDS,
43
+ httponly=True,
44
+ samesite="lax",
45
+ )
46
+
47
+
48
+ def get_current_user(request: Request) -> Optional[str]:
49
+ """Read the session cookie and return the username, or None if invalid/missing."""
50
+ token = request.cookies.get(COOKIE_NAME)
51
+ if not token:
52
+ return None
53
+ try:
54
+ username = _serializer().loads(token, max_age=_MAX_AGE_SECONDS)
55
+ return username
56
+ except (BadSignature, SignatureExpired):
57
+ return None
dashboard/db.py ADDED
@@ -0,0 +1,429 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Database layer for the reclip_bot admin dashboard.
2
+
3
+ Uses aiosqlite for async SQLite access with WAL journal mode.
4
+ DB path is read from the DB_PATH environment variable (default: /data/reclip_bot.db).
5
+ """
6
+ import os
7
+ from contextlib import asynccontextmanager
8
+ from datetime import datetime, timezone, timedelta
9
+ from typing import Any, AsyncGenerator, Dict, List, Optional
10
+
11
+ import aiosqlite
12
+
13
+ _SCHEMA = """
14
+ PRAGMA journal_mode=WAL;
15
+
16
+ CREATE TABLE IF NOT EXISTS downloads (
17
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
18
+ job_id TEXT UNIQUE NOT NULL,
19
+ user_id INTEGER,
20
+ username TEXT,
21
+ chat_id INTEGER,
22
+ url TEXT NOT NULL,
23
+ title TEXT,
24
+ platform TEXT,
25
+ format TEXT,
26
+ quality TEXT,
27
+ file_size_bytes INTEGER,
28
+ download_duration_sec REAL,
29
+ status TEXT NOT NULL DEFAULT 'started',
30
+ error_message TEXT,
31
+ started_at TEXT NOT NULL,
32
+ completed_at TEXT
33
+ );
34
+
35
+ CREATE TABLE IF NOT EXISTS disk_snapshots (
36
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
37
+ total_bytes INTEGER NOT NULL,
38
+ file_count INTEGER NOT NULL,
39
+ snapshot_at TEXT NOT NULL DEFAULT (datetime('now'))
40
+ );
41
+ """
42
+
43
+
44
+ # ---------------------------------------------------------------------------
45
+ # Internal helpers
46
+ # ---------------------------------------------------------------------------
47
+
48
+ def _db_path() -> str:
49
+ """Return current DB_PATH (re-read from env each call so tests can override)."""
50
+ return os.environ.get("DB_PATH", "/data/reclip_bot.db")
51
+
52
+
53
+ def _utc_now_str() -> str:
54
+ return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
55
+
56
+
57
+ @asynccontextmanager
58
+ async def _conn() -> AsyncGenerator[aiosqlite.Connection, None]:
59
+ """Open a WAL-mode connection, run schema migrations, yield, then close."""
60
+ path = _db_path()
61
+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
62
+ async with aiosqlite.connect(path) as conn:
63
+ conn.row_factory = aiosqlite.Row
64
+ await conn.executescript(_SCHEMA)
65
+ await conn.commit()
66
+ yield conn
67
+
68
+
69
+ # ---------------------------------------------------------------------------
70
+ # Public: schema init
71
+ # ---------------------------------------------------------------------------
72
+
73
+ async def init_db(path: Optional[str] = None) -> None:
74
+ """Initialise (or migrate) the database schema at the given path.
75
+
76
+ If path is provided it temporarily overrides the env var for this call.
77
+ """
78
+ if path is not None:
79
+ old = os.environ.get("DB_PATH")
80
+ os.environ["DB_PATH"] = path
81
+ try:
82
+ async with _conn():
83
+ pass
84
+ finally:
85
+ if path is not None:
86
+ if old is None:
87
+ del os.environ["DB_PATH"]
88
+ else:
89
+ os.environ["DB_PATH"] = old
90
+
91
+
92
+ # ---------------------------------------------------------------------------
93
+ # Public: CRUD — downloads
94
+ # ---------------------------------------------------------------------------
95
+
96
+ async def insert_download_start(
97
+ *,
98
+ job_id: str,
99
+ user_id: Optional[int],
100
+ username: Optional[str],
101
+ chat_id: Optional[int],
102
+ url: str,
103
+ platform: Optional[str] = None,
104
+ ) -> None:
105
+ """Record the start of a download job."""
106
+ started_at = _utc_now_str()
107
+ async with _conn() as conn:
108
+ await conn.execute(
109
+ """
110
+ INSERT INTO downloads (job_id, user_id, username, chat_id, url, platform, started_at)
111
+ VALUES (?, ?, ?, ?, ?, ?, ?)
112
+ """,
113
+ (job_id, user_id, username, chat_id, url, platform, started_at),
114
+ )
115
+ await conn.commit()
116
+
117
+
118
+ async def update_download_done(
119
+ *,
120
+ job_id: str,
121
+ title: Optional[str] = None,
122
+ format: Optional[str] = None,
123
+ quality: Optional[str] = None,
124
+ file_size_bytes: Optional[int] = None,
125
+ download_duration_sec: Optional[float] = None,
126
+ ) -> None:
127
+ """Mark a download as successfully completed."""
128
+ completed_at = _utc_now_str()
129
+ async with _conn() as conn:
130
+ await conn.execute(
131
+ """
132
+ UPDATE downloads
133
+ SET status='done', title=?, format=?, quality=?,
134
+ file_size_bytes=?, download_duration_sec=?, completed_at=?
135
+ WHERE job_id=?
136
+ """,
137
+ (title, format, quality, file_size_bytes, download_duration_sec,
138
+ completed_at, job_id),
139
+ )
140
+ await conn.commit()
141
+
142
+
143
+ async def update_download_error(*, job_id: str, error_message: str) -> None:
144
+ """Mark a download as failed."""
145
+ completed_at = _utc_now_str()
146
+ async with _conn() as conn:
147
+ await conn.execute(
148
+ """
149
+ UPDATE downloads
150
+ SET status='error', error_message=?, completed_at=?
151
+ WHERE job_id=?
152
+ """,
153
+ (error_message, completed_at, job_id),
154
+ )
155
+ await conn.commit()
156
+
157
+
158
+ async def get_download_by_job_id(job_id: str) -> Optional[aiosqlite.Row]:
159
+ """Return a single download row by job_id, or None."""
160
+ async with _conn() as conn:
161
+ async with conn.execute(
162
+ "SELECT * FROM downloads WHERE job_id=?", (job_id,)
163
+ ) as cur:
164
+ return await cur.fetchone()
165
+
166
+
167
+ # ---------------------------------------------------------------------------
168
+ # Public: stats queries
169
+ # ---------------------------------------------------------------------------
170
+
171
+ async def get_dashboard_stats() -> Dict[str, Any]:
172
+ """Return high-level dashboard statistics."""
173
+ now = datetime.now(timezone.utc)
174
+ today_str = now.strftime("%Y-%m-%d")
175
+ yesterday_str = (now - timedelta(days=1)).strftime("%Y-%m-%d")
176
+
177
+ async with _conn() as conn:
178
+ async with conn.execute(
179
+ "SELECT COUNT(*) FROM downloads WHERE status='done' AND started_at LIKE ?",
180
+ (f"{today_str}%",),
181
+ ) as cur:
182
+ downloads_today = (await cur.fetchone())[0]
183
+
184
+ async with conn.execute(
185
+ "SELECT COUNT(*) FROM downloads WHERE status='done' AND started_at LIKE ?",
186
+ (f"{yesterday_str}%",),
187
+ ) as cur:
188
+ downloads_yesterday = (await cur.fetchone())[0]
189
+
190
+ async with conn.execute(
191
+ "SELECT COUNT(*) FROM downloads WHERE started_at LIKE ?",
192
+ (f"{today_str}%",),
193
+ ) as cur:
194
+ total_today = (await cur.fetchone())[0]
195
+
196
+ async with conn.execute(
197
+ "SELECT COUNT(*) FROM downloads WHERE status='error' AND started_at LIKE ?",
198
+ (f"{today_str}%",),
199
+ ) as cur:
200
+ errors_today = (await cur.fetchone())[0]
201
+
202
+ async with conn.execute(
203
+ "SELECT COUNT(*) FROM downloads WHERE status='error' AND started_at LIKE ?",
204
+ (f"{yesterday_str}%",),
205
+ ) as cur:
206
+ errors_yesterday = (await cur.fetchone())[0]
207
+
208
+ async with conn.execute(
209
+ "SELECT COUNT(*) FROM downloads WHERE started_at LIKE ?",
210
+ (f"{yesterday_str}%",),
211
+ ) as cur:
212
+ total_yesterday = (await cur.fetchone())[0]
213
+
214
+ since_24h = (now - timedelta(hours=24)).strftime("%Y-%m-%d %H:%M:%S")
215
+ async with conn.execute(
216
+ "SELECT COUNT(DISTINCT user_id) FROM downloads WHERE started_at >= ?",
217
+ (since_24h,),
218
+ ) as cur:
219
+ active_users_24h = (await cur.fetchone())[0]
220
+
221
+ error_rate = (errors_today / total_today * 100) if total_today else 0.0
222
+ error_rate_yesterday = (errors_yesterday / total_yesterday * 100) if total_yesterday else 0.0
223
+
224
+ return {
225
+ "downloads_today": downloads_today,
226
+ "downloads_yesterday": downloads_yesterday,
227
+ "active_users_24h": active_users_24h,
228
+ "error_rate": round(error_rate, 2),
229
+ "error_rate_yesterday": round(error_rate_yesterday, 2),
230
+ "errors_today": errors_today,
231
+ "total_today": total_today,
232
+ }
233
+
234
+
235
+ async def get_chart_data(range_key: str) -> Dict[str, Any]:
236
+ """Return time-series chart data for the given range.
237
+
238
+ range_key:
239
+ "1D" -> hourly buckets for the past 24 hours
240
+ "7D" -> daily buckets for the past 7 days
241
+ "1M" -> daily buckets for the past 30 days
242
+ "1Y" -> monthly buckets for the past 12 months
243
+ """
244
+ now = datetime.now(timezone.utc)
245
+
246
+ if range_key == "1D":
247
+ # Build list of datetime objects, one per hour, newest last
248
+ bucket_dts = [(now - timedelta(hours=i)) for i in range(23, -1, -1)]
249
+ labels = [dt.strftime("%H:00") for dt in bucket_dts]
250
+ bucket_keys = [dt.strftime("%Y-%m-%d %H") for dt in bucket_dts]
251
+ start_str = (now - timedelta(hours=24)).strftime("%Y-%m-%d %H:%M:%S")
252
+ trunc_fmt = "%Y-%m-%d %H"
253
+ elif range_key == "7D":
254
+ bucket_dts = [(now - timedelta(days=i)) for i in range(6, -1, -1)]
255
+ labels = [dt.strftime("%a %d") for dt in bucket_dts]
256
+ bucket_keys = [dt.strftime("%Y-%m-%d") for dt in bucket_dts]
257
+ start_str = (now - timedelta(days=7)).strftime("%Y-%m-%d %H:%M:%S")
258
+ trunc_fmt = "%Y-%m-%d"
259
+ elif range_key == "1M":
260
+ bucket_dts = [(now - timedelta(days=i)) for i in range(29, -1, -1)]
261
+ labels = [dt.strftime("%b %d") for dt in bucket_dts]
262
+ bucket_keys = [dt.strftime("%Y-%m-%d") for dt in bucket_dts]
263
+ start_str = (now - timedelta(days=30)).strftime("%Y-%m-%d %H:%M:%S")
264
+ trunc_fmt = "%Y-%m-%d"
265
+ else: # 1Y
266
+ # Monthly: go back 12 months (approximate with 30-day steps)
267
+ bucket_dts = [
268
+ (now.replace(day=1) - timedelta(days=30 * i))
269
+ for i in range(11, -1, -1)
270
+ ]
271
+ labels = [dt.strftime("%b %Y") for dt in bucket_dts]
272
+ bucket_keys = [dt.strftime("%Y-%m") for dt in bucket_dts]
273
+ start_str = (now - timedelta(days=365)).strftime("%Y-%m-%d %H:%M:%S")
274
+ trunc_fmt = "%Y-%m"
275
+
276
+ async with _conn() as conn:
277
+ async with conn.execute(
278
+ f"""
279
+ SELECT strftime('{trunc_fmt}', started_at) AS bucket, COUNT(*) AS cnt
280
+ FROM downloads
281
+ WHERE started_at >= ? AND status='done'
282
+ GROUP BY bucket
283
+ """,
284
+ (start_str,),
285
+ ) as cur:
286
+ rows = await cur.fetchall()
287
+
288
+ bucket_map: Dict[str, int] = {r["bucket"]: r["cnt"] for r in rows}
289
+ values = [bucket_map.get(k, 0) for k in bucket_keys]
290
+
291
+ async with conn.execute(
292
+ """
293
+ SELECT platform, COUNT(*) AS cnt
294
+ FROM downloads
295
+ WHERE started_at >= ? AND platform IS NOT NULL
296
+ GROUP BY platform
297
+ ORDER BY cnt DESC
298
+ LIMIT 5
299
+ """,
300
+ (start_str,),
301
+ ) as cur:
302
+ platform_rows = await cur.fetchall()
303
+ platforms = [{"platform": r["platform"], "count": r["cnt"]} for r in platform_rows]
304
+
305
+ async with conn.execute(
306
+ """
307
+ SELECT username, COUNT(*) AS cnt
308
+ FROM downloads
309
+ WHERE started_at >= ? AND username IS NOT NULL
310
+ GROUP BY username
311
+ ORDER BY cnt DESC
312
+ LIMIT 5
313
+ """,
314
+ (start_str,),
315
+ ) as cur:
316
+ user_rows = await cur.fetchall()
317
+ top_users = [{"username": r["username"], "count": r["cnt"]} for r in user_rows]
318
+
319
+ return {
320
+ "labels": labels,
321
+ "values": values,
322
+ "platforms": platforms,
323
+ "top_users": top_users,
324
+ }
325
+
326
+
327
+ async def get_downloads_page(
328
+ page: int = 1,
329
+ per_page: int = 20,
330
+ platform: Optional[str] = None,
331
+ status: Optional[str] = None,
332
+ user: Optional[str] = None,
333
+ date_from: Optional[str] = None,
334
+ date_to: Optional[str] = None,
335
+ ) -> Dict[str, Any]:
336
+ """Return paginated download history with optional filters."""
337
+ conditions: List[str] = []
338
+ params: List[Any] = []
339
+
340
+ if platform:
341
+ conditions.append("platform = ?")
342
+ params.append(platform)
343
+ if status:
344
+ conditions.append("status = ?")
345
+ params.append(status)
346
+ if user:
347
+ conditions.append("username = ?")
348
+ params.append(user)
349
+ if date_from:
350
+ conditions.append("started_at >= ?")
351
+ params.append(f"{date_from} 00:00:00")
352
+ if date_to:
353
+ conditions.append("started_at <= ?")
354
+ params.append(f"{date_to} 23:59:59")
355
+
356
+ where = ("WHERE " + " AND ".join(conditions)) if conditions else ""
357
+ offset = (page - 1) * per_page
358
+
359
+ async with _conn() as conn:
360
+ async with conn.execute(
361
+ f"SELECT COUNT(*) FROM downloads {where}", params
362
+ ) as cur:
363
+ total = (await cur.fetchone())[0]
364
+
365
+ async with conn.execute(
366
+ f"SELECT * FROM downloads {where} ORDER BY started_at DESC LIMIT ? OFFSET ?",
367
+ params + [per_page, offset],
368
+ ) as cur:
369
+ rows = await cur.fetchall()
370
+
371
+ pages = (total + per_page - 1) // per_page if total else 1
372
+ return {
373
+ "rows": [dict(r) for r in rows],
374
+ "total": total,
375
+ "pages": pages,
376
+ "page": page,
377
+ }
378
+
379
+
380
+ async def get_error_downloads(
381
+ date_from: Optional[str] = None,
382
+ date_to: Optional[str] = None,
383
+ ) -> List[Dict[str, Any]]:
384
+ """Return all failed downloads, optionally filtered by date range."""
385
+ conditions = ["status = 'error'"]
386
+ params: List[Any] = []
387
+
388
+ if date_from:
389
+ conditions.append("started_at >= ?")
390
+ params.append(f"{date_from} 00:00:00")
391
+ if date_to:
392
+ conditions.append("started_at <= ?")
393
+ params.append(f"{date_to} 23:59:59")
394
+
395
+ where = "WHERE " + " AND ".join(conditions)
396
+
397
+ async with _conn() as conn:
398
+ async with conn.execute(
399
+ f"SELECT * FROM downloads {where} ORDER BY started_at DESC",
400
+ params,
401
+ ) as cur:
402
+ rows = await cur.fetchall()
403
+
404
+ return [dict(r) for r in rows]
405
+
406
+
407
+ # ---------------------------------------------------------------------------
408
+ # Public: disk snapshots
409
+ # ---------------------------------------------------------------------------
410
+
411
+ async def insert_disk_snapshot(total_bytes: int, file_count: int) -> None:
412
+ """Record a disk usage snapshot."""
413
+ snapshot_at = _utc_now_str()
414
+ async with _conn() as conn:
415
+ await conn.execute(
416
+ "INSERT INTO disk_snapshots (total_bytes, file_count, snapshot_at) VALUES (?, ?, ?)",
417
+ (total_bytes, file_count, snapshot_at),
418
+ )
419
+ await conn.commit()
420
+
421
+
422
+ async def get_latest_disk_snapshot() -> Optional[Dict[str, Any]]:
423
+ """Return the most recent disk snapshot, or None."""
424
+ async with _conn() as conn:
425
+ async with conn.execute(
426
+ "SELECT * FROM disk_snapshots ORDER BY id DESC LIMIT 1"
427
+ ) as cur:
428
+ row = await cur.fetchone()
429
+ return dict(row) if row else None
dashboard/main.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FastAPI application factory for the reclip_bot admin dashboard."""
2
+ import asyncio
3
+ import os
4
+ from contextlib import asynccontextmanager
5
+ from pathlib import Path
6
+
7
+ from fastapi import FastAPI
8
+ from fastapi.staticfiles import StaticFiles
9
+
10
+ import db
11
+ from routes.api import router as api_router
12
+ from routes.pages import router as pages_router
13
+
14
+ _STATIC_DIR = Path(__file__).parent / "static"
15
+ _DOWNLOADS_PATH = Path(os.environ.get("DOWNLOADS_PATH", "/downloads"))
16
+
17
+
18
+ async def _disk_snapshot_loop() -> None:
19
+ """Background task: scan DOWNLOADS_PATH every 5 minutes and record snapshot."""
20
+ while True:
21
+ try:
22
+ downloads_path = Path(os.environ.get("DOWNLOADS_PATH", "/downloads"))
23
+ total_bytes = 0
24
+ file_count = 0
25
+ if downloads_path.exists():
26
+ for item in downloads_path.rglob("*"):
27
+ if item.is_file():
28
+ try:
29
+ total_bytes += item.stat().st_size
30
+ file_count += 1
31
+ except OSError:
32
+ pass
33
+ await db.insert_disk_snapshot(total_bytes=total_bytes, file_count=file_count)
34
+ except Exception:
35
+ pass
36
+ await asyncio.sleep(300) # 5 minutes
37
+
38
+
39
+ @asynccontextmanager
40
+ async def _lifespan(app: FastAPI):
41
+ await db.init_db()
42
+ task = asyncio.create_task(_disk_snapshot_loop())
43
+ try:
44
+ yield
45
+ finally:
46
+ task.cancel()
47
+ try:
48
+ await task
49
+ except asyncio.CancelledError:
50
+ pass
51
+
52
+
53
+ def create_app() -> FastAPI:
54
+ """Application factory."""
55
+ app = FastAPI(title="reclip admin", lifespan=_lifespan)
56
+
57
+ if _STATIC_DIR.exists():
58
+ app.mount("/static", StaticFiles(directory=str(_STATIC_DIR)), name="static")
59
+
60
+ app.include_router(api_router)
61
+ app.include_router(pages_router)
62
+
63
+ return app
64
+
65
+
66
+ app = create_app()
dashboard/requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi>=0.110.0
2
+ uvicorn>=0.27.0
3
+ jinja2>=3.1.0
4
+ aiosqlite>=0.20.0
5
+ itsdangerous>=2.1.0
6
+ httpx>=0.25.0
7
+ python-multipart>=0.0.6
dashboard/routes/__init__.py ADDED
File without changes
dashboard/routes/api.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """API routes for the reclip_bot admin dashboard."""
2
+ import os
3
+ import shutil
4
+ from pathlib import Path
5
+ from typing import Any, Dict, List, Optional
6
+
7
+ from fastapi import APIRouter, Depends, HTTPException, Request
8
+ from fastapi.responses import JSONResponse
9
+ from pydantic import BaseModel
10
+
11
+ import db
12
+ from auth import get_current_user
13
+
14
+ router = APIRouter()
15
+
16
+ # In-memory active downloads dict: job_id -> dict
17
+ _active_downloads: Dict[str, Dict[str, Any]] = {}
18
+
19
+
20
+ def _downloads_path() -> Path:
21
+ return Path(os.environ.get("DOWNLOADS_PATH", "/downloads"))
22
+
23
+
24
+ # ---------------------------------------------------------------------------
25
+ # Auth dependency
26
+ # ---------------------------------------------------------------------------
27
+
28
+ def require_auth(request: Request) -> str:
29
+ user = get_current_user(request)
30
+ if not user:
31
+ raise HTTPException(status_code=401, detail="Not authenticated")
32
+ return user
33
+
34
+
35
+ # ---------------------------------------------------------------------------
36
+ # Event ingestion (no auth — internal Docker network only)
37
+ # ---------------------------------------------------------------------------
38
+
39
+ @router.post("/api/events")
40
+ async def ingest_event(request: Request) -> Dict[str, str]:
41
+ """Accept download lifecycle events. No auth — internal network only.
42
+
43
+ Bot sends flat JSON: {"type": "download_start", "job_id": "...", ...}
44
+ """
45
+ data = await request.json()
46
+ event_type = data.get("type")
47
+
48
+ if event_type == "download_start":
49
+ await db.insert_download_start(
50
+ job_id=data["job_id"],
51
+ user_id=data.get("user_id"),
52
+ username=data.get("username"),
53
+ chat_id=data.get("chat_id"),
54
+ url=data.get("url", ""),
55
+ platform=data.get("platform"),
56
+ )
57
+ _active_downloads[data["job_id"]] = {
58
+ "job_id": data["job_id"],
59
+ "user_id": data.get("user_id"),
60
+ "username": data.get("username"),
61
+ "url": data.get("url"),
62
+ "platform": data.get("platform"),
63
+ "title": data.get("title"),
64
+ "percent": 0,
65
+ "speed": 0,
66
+ "eta": 0,
67
+ }
68
+
69
+ elif event_type == "download_progress":
70
+ job_id = data.get("job_id")
71
+ if job_id and job_id in _active_downloads:
72
+ _active_downloads[job_id].update({
73
+ "percent": data.get("percent", 0),
74
+ "speed": data.get("speed", 0),
75
+ "eta": data.get("eta", 0),
76
+ "downloaded_bytes": data.get("downloaded_bytes", 0),
77
+ "total_bytes": data.get("total_bytes", 0),
78
+ })
79
+
80
+ elif event_type == "download_done":
81
+ await db.update_download_done(
82
+ job_id=data["job_id"],
83
+ file_size_bytes=data.get("file_size_bytes"),
84
+ download_duration_sec=data.get("duration_seconds"),
85
+ )
86
+ _active_downloads.pop(data["job_id"], None)
87
+
88
+ elif event_type == "download_error":
89
+ await db.update_download_error(
90
+ job_id=data["job_id"],
91
+ error_message=data.get("error_message", "Unknown error"),
92
+ )
93
+ _active_downloads.pop(data["job_id"], None)
94
+
95
+ return {"status": "ok"}
96
+
97
+
98
+ # ---------------------------------------------------------------------------
99
+ # Dashboard stats (auth required)
100
+ # ---------------------------------------------------------------------------
101
+
102
+ @router.get("/api/dashboard-stats")
103
+ async def dashboard_stats(user: str = Depends(require_auth)) -> Dict[str, Any]:
104
+ stats = await db.get_dashboard_stats()
105
+ disk = await db.get_latest_disk_snapshot()
106
+ return {"stats": stats, "disk": disk}
107
+
108
+
109
+ @router.get("/api/chart-data")
110
+ async def chart_data(
111
+ range: str = "1D",
112
+ user: str = Depends(require_auth),
113
+ ) -> Dict[str, Any]:
114
+ valid_ranges = {"1D", "7D", "1M", "1Y"}
115
+ if range not in valid_ranges:
116
+ raise HTTPException(status_code=400, detail=f"Invalid range. Must be one of {valid_ranges}")
117
+ return await db.get_chart_data(range)
118
+
119
+
120
+ @router.get("/api/active-downloads")
121
+ async def active_downloads(user: str = Depends(require_auth)) -> List[Dict[str, Any]]:
122
+ return list(_active_downloads.values())
123
+
124
+
125
+ # ---------------------------------------------------------------------------
126
+ # Admin file operations (auth required) — Task 5
127
+ # ---------------------------------------------------------------------------
128
+
129
+ class DeleteFilesBody(BaseModel):
130
+ paths: List[str]
131
+
132
+
133
+ class PurgeBody(BaseModel):
134
+ confirm: Optional[str] = None
135
+
136
+
137
+ @router.delete("/api/files")
138
+ async def delete_files(
139
+ body: DeleteFilesBody,
140
+ user: str = Depends(require_auth),
141
+ ) -> Dict[str, Any]:
142
+ """Delete selected files. Uses filename only to prevent path traversal."""
143
+ downloads_path = _downloads_path()
144
+ deleted = []
145
+ errors = []
146
+ for p in body.paths:
147
+ # Prevent path traversal: use only the filename component
148
+ safe_path = downloads_path / Path(p).name
149
+ try:
150
+ if safe_path.exists():
151
+ safe_path.unlink()
152
+ deleted.append(str(safe_path.name))
153
+ else:
154
+ errors.append({"file": p, "error": "not found"})
155
+ except Exception as exc:
156
+ errors.append({"file": p, "error": str(exc)})
157
+ return {"deleted": deleted, "errors": errors}
158
+
159
+
160
+ @router.delete("/api/files/all")
161
+ async def purge_all_files(
162
+ body: PurgeBody,
163
+ user: str = Depends(require_auth),
164
+ ) -> Dict[str, Any]:
165
+ """Purge all files in DOWNLOADS_PATH. Requires confirm='PURGE' in body."""
166
+ if body.confirm != "PURGE":
167
+ raise HTTPException(status_code=400, detail='Body must contain {"confirm": "PURGE"}')
168
+ downloads_path = _downloads_path()
169
+ deleted_count = 0
170
+ if downloads_path.exists():
171
+ for item in downloads_path.iterdir():
172
+ try:
173
+ if item.is_file():
174
+ item.unlink()
175
+ deleted_count += 1
176
+ elif item.is_dir():
177
+ shutil.rmtree(item)
178
+ deleted_count += 1
179
+ except Exception:
180
+ pass
181
+ return {"deleted_count": deleted_count}
dashboard/routes/pages.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Page routes for the reclip_bot admin dashboard."""
2
+ from fastapi import APIRouter, Request, Depends
3
+ from fastapi.responses import HTMLResponse, RedirectResponse
4
+ from fastapi.templating import Jinja2Templates
5
+ from pathlib import Path
6
+
7
+ import db
8
+ from auth import verify_credentials, create_session_cookie, get_current_user, COOKIE_NAME
9
+
10
+ router = APIRouter()
11
+
12
+ _templates_dir = Path(__file__).parent.parent / "templates"
13
+ templates = Jinja2Templates(directory=str(_templates_dir))
14
+
15
+
16
+ def _require_auth(request: Request) -> str:
17
+ user = get_current_user(request)
18
+ if not user:
19
+ return None
20
+ return user
21
+
22
+
23
+ # ---------------------------------------------------------------------------
24
+ # Login / Logout
25
+ # ---------------------------------------------------------------------------
26
+
27
+ @router.get("/login", response_class=HTMLResponse)
28
+ async def login_page(request: Request) -> HTMLResponse:
29
+ return templates.TemplateResponse(request, "login.html", {"error": None})
30
+
31
+
32
+ @router.post("/login")
33
+ async def login_submit(request: Request) -> HTMLResponse:
34
+ form = await request.form()
35
+ username = form.get("username", "")
36
+ password = form.get("password", "")
37
+
38
+ if verify_credentials(username, password):
39
+ response = RedirectResponse(url="/", status_code=303)
40
+ create_session_cookie(response, username)
41
+ return response
42
+
43
+ return templates.TemplateResponse(
44
+ request, "login.html",
45
+ {"error": "Invalid credentials"},
46
+ status_code=401,
47
+ )
48
+
49
+
50
+ @router.get("/logout")
51
+ async def logout() -> RedirectResponse:
52
+ response = RedirectResponse(url="/login", status_code=303)
53
+ response.delete_cookie(COOKIE_NAME)
54
+ return response
55
+
56
+
57
+ # ---------------------------------------------------------------------------
58
+ # Protected pages
59
+ # ---------------------------------------------------------------------------
60
+
61
+ @router.get("/", response_class=HTMLResponse)
62
+ async def dashboard_page(request: Request) -> HTMLResponse:
63
+ user = get_current_user(request)
64
+ if not user:
65
+ return RedirectResponse(url="/login", status_code=303)
66
+ return templates.TemplateResponse(request, "dashboard.html", {"user": user})
67
+
68
+
69
+ @router.get("/history", response_class=HTMLResponse)
70
+ async def history_page(
71
+ request: Request,
72
+ page: int = 1,
73
+ platform: str = None,
74
+ status: str = None,
75
+ user_filter: str = None,
76
+ date_from: str = None,
77
+ date_to: str = None,
78
+ ) -> HTMLResponse:
79
+ user = get_current_user(request)
80
+ if not user:
81
+ return RedirectResponse(url="/login", status_code=303)
82
+
83
+ data = await db.get_downloads_page(
84
+ page=page,
85
+ platform=platform,
86
+ status=status,
87
+ user=user_filter,
88
+ date_from=date_from,
89
+ date_to=date_to,
90
+ )
91
+ return templates.TemplateResponse(
92
+ request, "history.html",
93
+ {
94
+ "user": user,
95
+ "data": data,
96
+ "platform": platform,
97
+ "status": status,
98
+ "user_filter": user_filter,
99
+ "date_from": date_from,
100
+ "date_to": date_to,
101
+ },
102
+ )
103
+
104
+
105
+ @router.get("/errors", response_class=HTMLResponse)
106
+ async def errors_page(
107
+ request: Request,
108
+ date_from: str = None,
109
+ date_to: str = None,
110
+ ) -> HTMLResponse:
111
+ user = get_current_user(request)
112
+ if not user:
113
+ return RedirectResponse(url="/login", status_code=303)
114
+
115
+ errors = await db.get_error_downloads(date_from=date_from, date_to=date_to)
116
+ return templates.TemplateResponse(
117
+ request, "errors.html",
118
+ {"user": user, "errors": errors},
119
+ )
120
+
121
+
122
+ @router.get("/admin", response_class=HTMLResponse)
123
+ async def admin_page(request: Request) -> HTMLResponse:
124
+ import os
125
+ import shutil
126
+
127
+ user = get_current_user(request)
128
+ if not user:
129
+ return RedirectResponse(url="/login", status_code=303)
130
+
131
+ downloads_path = Path(os.environ.get("DOWNLOADS_PATH", "/downloads"))
132
+ files = []
133
+ downloads_bytes = 0
134
+ if downloads_path.exists():
135
+ for f in sorted(downloads_path.iterdir()):
136
+ try:
137
+ stat = f.stat()
138
+ if f.is_file():
139
+ downloads_bytes += stat.st_size
140
+ files.append({
141
+ "name": f.name,
142
+ "size": stat.st_size,
143
+ "is_dir": f.is_dir(),
144
+ })
145
+ except OSError:
146
+ pass
147
+
148
+ max_disk_mb = int(os.environ.get("CLEANUP_MAX_DISK_MB", "5000"))
149
+ max_disk_bytes = max_disk_mb * 1024 * 1024
150
+ disk_pct = round(downloads_bytes / max_disk_bytes * 100, 1) if max_disk_bytes else 0
151
+
152
+ return templates.TemplateResponse(
153
+ request, "admin.html",
154
+ {
155
+ "user": user,
156
+ "files": files,
157
+ "downloads_bytes": downloads_bytes,
158
+ "max_disk_mb": max_disk_mb,
159
+ "disk_pct": disk_pct,
160
+ },
161
+ )
dashboard/static/dashboard.js ADDED
@@ -0,0 +1,510 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* =========================================================
2
+ reclip admin — dashboard.js
3
+ Chart.js polling + admin actions
4
+ ========================================================= */
5
+
6
+ 'use strict';
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Utilities
10
+ // ---------------------------------------------------------------------------
11
+
12
+ function fmt(n) {
13
+ if (n == null) return '--';
14
+ return Number(n).toLocaleString();
15
+ }
16
+
17
+ function showToast(message, type = 'info') {
18
+ const container = document.getElementById('toast-container');
19
+ if (!container) return;
20
+
21
+ const toast = document.createElement('div');
22
+ toast.className = `toast toast-${type}`;
23
+ toast.innerHTML = `
24
+ <span>${message}</span>
25
+ <button class="toast-close" aria-label="Dismiss">&times;</button>
26
+ `;
27
+
28
+ const closeBtn = toast.querySelector('.toast-close');
29
+ closeBtn.addEventListener('click', () => removeToast(toast));
30
+
31
+ container.appendChild(toast);
32
+
33
+ // Auto-remove after 5 seconds
34
+ setTimeout(() => removeToast(toast), 5000);
35
+ }
36
+
37
+ function removeToast(toast) {
38
+ if (!toast.parentElement) return;
39
+ toast.style.opacity = '0';
40
+ toast.style.transform = 'translateX(20px)';
41
+ toast.style.transition = 'opacity 0.2s ease-in, transform 0.2s ease-in';
42
+ setTimeout(() => toast.remove(), 200);
43
+ }
44
+
45
+ // ---------------------------------------------------------------------------
46
+ // Chart configuration helpers
47
+ // ---------------------------------------------------------------------------
48
+
49
+ const CHART_DEFAULTS = {
50
+ color: '#ff6b35',
51
+ gridColor: '#2a2a40',
52
+ tickColor: '#8888a0',
53
+ fontFamily: 'Inter, system-ui, -apple-system, sans-serif',
54
+ };
55
+
56
+ function commonScaleOptions() {
57
+ return {
58
+ grid: { color: CHART_DEFAULTS.gridColor },
59
+ ticks: {
60
+ color: CHART_DEFAULTS.tickColor,
61
+ font: { family: CHART_DEFAULTS.fontFamily, size: 11 },
62
+ },
63
+ border: { color: CHART_DEFAULTS.gridColor },
64
+ };
65
+ }
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // Chart instances (module-level so we can update them)
69
+ // ---------------------------------------------------------------------------
70
+
71
+ let downloadsChart = null;
72
+ let platformChart = null;
73
+
74
+ function initCharts() {
75
+ const dlCtx = document.getElementById('downloads-chart');
76
+ const plCtx = document.getElementById('platform-chart');
77
+
78
+ if (dlCtx) {
79
+ downloadsChart = new Chart(dlCtx, {
80
+ type: 'line',
81
+ data: {
82
+ labels: [],
83
+ datasets: [{
84
+ label: 'Downloads',
85
+ data: [],
86
+ borderColor: CHART_DEFAULTS.color,
87
+ backgroundColor: 'rgba(255, 107, 53, 0.08)',
88
+ borderWidth: 2,
89
+ pointRadius: 3,
90
+ pointHoverRadius: 5,
91
+ tension: 0.3,
92
+ fill: true,
93
+ }],
94
+ },
95
+ options: {
96
+ responsive: true,
97
+ maintainAspectRatio: false,
98
+ animation: { duration: 400, easing: 'easeOut' },
99
+ plugins: {
100
+ legend: { display: false },
101
+ tooltip: {
102
+ backgroundColor: '#1a1a2e',
103
+ borderColor: '#2a2a40',
104
+ borderWidth: 1,
105
+ titleColor: '#e0e0e0',
106
+ bodyColor: '#8888a0',
107
+ titleFont: { family: CHART_DEFAULTS.fontFamily, size: 12 },
108
+ bodyFont: { family: CHART_DEFAULTS.fontFamily, size: 12 },
109
+ },
110
+ },
111
+ scales: {
112
+ x: commonScaleOptions(),
113
+ y: {
114
+ ...commonScaleOptions(),
115
+ beginAtZero: true,
116
+ ticks: {
117
+ ...commonScaleOptions().ticks,
118
+ precision: 0,
119
+ },
120
+ },
121
+ },
122
+ },
123
+ });
124
+ }
125
+
126
+ if (plCtx) {
127
+ platformChart = new Chart(plCtx, {
128
+ type: 'bar',
129
+ data: {
130
+ labels: [],
131
+ datasets: [{
132
+ label: 'Downloads',
133
+ data: [],
134
+ backgroundColor: 'rgba(255, 107, 53, 0.7)',
135
+ borderColor: CHART_DEFAULTS.color,
136
+ borderWidth: 1,
137
+ borderRadius: 3,
138
+ }],
139
+ },
140
+ options: {
141
+ indexAxis: 'y',
142
+ responsive: true,
143
+ maintainAspectRatio: false,
144
+ animation: { duration: 400, easing: 'easeOut' },
145
+ plugins: {
146
+ legend: { display: false },
147
+ tooltip: {
148
+ backgroundColor: '#1a1a2e',
149
+ borderColor: '#2a2a40',
150
+ borderWidth: 1,
151
+ titleColor: '#e0e0e0',
152
+ bodyColor: '#8888a0',
153
+ titleFont: { family: CHART_DEFAULTS.fontFamily, size: 12 },
154
+ bodyFont: { family: CHART_DEFAULTS.fontFamily, size: 12 },
155
+ },
156
+ },
157
+ scales: {
158
+ x: {
159
+ ...commonScaleOptions(),
160
+ beginAtZero: true,
161
+ ticks: { ...commonScaleOptions().ticks, precision: 0 },
162
+ },
163
+ y: commonScaleOptions(),
164
+ },
165
+ },
166
+ });
167
+ }
168
+ }
169
+
170
+ // ---------------------------------------------------------------------------
171
+ // API fetchers
172
+ // ---------------------------------------------------------------------------
173
+
174
+ async function fetchStats() {
175
+ try {
176
+ const res = await fetch('/api/dashboard-stats', { credentials: 'same-origin' });
177
+ if (!res.ok) return;
178
+ const json = await res.json();
179
+ const stats = json.stats || {};
180
+ const disk = json.disk || null;
181
+
182
+ // Downloads today
183
+ const dlEl = document.getElementById('kpi-downloads');
184
+ if (dlEl) dlEl.textContent = fmt(stats.downloads_today);
185
+
186
+ const dlTrend = document.getElementById('kpi-downloads-trend');
187
+ if (dlTrend) {
188
+ const today = stats.downloads_today ?? 0;
189
+ const yesterday = stats.downloads_yesterday ?? 0;
190
+ if (yesterday === 0 && today === 0) {
191
+ dlTrend.textContent = '';
192
+ dlTrend.className = 'kpi-trend';
193
+ } else if (today >= yesterday) {
194
+ dlTrend.textContent = `↑ vs ${fmt(yesterday)} yesterday`;
195
+ dlTrend.className = 'kpi-trend up';
196
+ } else {
197
+ dlTrend.textContent = `↓ vs ${fmt(yesterday)} yesterday`;
198
+ dlTrend.className = 'kpi-trend down';
199
+ }
200
+ }
201
+
202
+ // Active users
203
+ const usersEl = document.getElementById('kpi-users');
204
+ if (usersEl) usersEl.textContent = fmt(stats.active_users_24h);
205
+
206
+ // Error rate
207
+ const errEl = document.getElementById('kpi-errors');
208
+ if (errEl) {
209
+ const rate = stats.error_rate != null ? stats.error_rate.toFixed(1) : '--';
210
+ errEl.textContent = rate !== '--' ? rate + '%' : '--';
211
+ }
212
+
213
+ const errTrend = document.getElementById('kpi-errors-trend');
214
+ if (errTrend) {
215
+ const today = stats.error_rate ?? 0;
216
+ const yesterday = stats.error_rate_yesterday ?? 0;
217
+ if (yesterday === 0 && today === 0) {
218
+ errTrend.textContent = '';
219
+ errTrend.className = 'kpi-trend';
220
+ } else if (today <= yesterday) {
221
+ // lower error rate is better
222
+ errTrend.textContent = `↓ vs ${yesterday.toFixed(1)}% yesterday`;
223
+ errTrend.className = 'kpi-trend up';
224
+ } else {
225
+ errTrend.textContent = `↑ vs ${yesterday.toFixed(1)}% yesterday`;
226
+ errTrend.className = 'kpi-trend down';
227
+ }
228
+ }
229
+
230
+ // Disk
231
+ const diskEl = document.getElementById('kpi-disk');
232
+ const diskBar = document.getElementById('disk-bar');
233
+ if (disk && diskEl) {
234
+ const totalBytes = disk.total_bytes || 0;
235
+ // Use a configured max of 100GB, or actual disk_total from system info
236
+ // The disk snapshot records downloads folder size; compare against a 100GB ceiling
237
+ const MAX_BYTES = 100 * 1073741824; // 100 GB ceiling
238
+ const pct = totalBytes > 0 ? Math.min((totalBytes / MAX_BYTES) * 100, 100) : 0;
239
+ diskEl.textContent = pct.toFixed(1) + '%';
240
+
241
+ if (diskBar) {
242
+ diskBar.style.width = pct + '%';
243
+ diskBar.className = 'disk-bar';
244
+ if (pct >= 85) diskBar.classList.add('red');
245
+ else if (pct >= 60) diskBar.classList.add('amber');
246
+ }
247
+ }
248
+
249
+ } catch (e) {
250
+ console.warn('fetchStats error', e);
251
+ }
252
+ }
253
+
254
+ async function fetchChartData(range = '1D') {
255
+ try {
256
+ const res = await fetch(`/api/chart-data?range=${range}`, { credentials: 'same-origin' });
257
+ if (!res.ok) return;
258
+ const data = await res.json();
259
+
260
+ // Downloads over time chart
261
+ if (downloadsChart && data.labels && data.values) {
262
+ downloadsChart.data.labels = data.labels;
263
+ downloadsChart.data.datasets[0].data = data.values;
264
+ downloadsChart.update();
265
+ }
266
+
267
+ // Platform chart
268
+ if (platformChart && data.platforms) {
269
+ platformChart.data.labels = data.platforms.map(p => p.platform);
270
+ platformChart.data.datasets[0].data = data.platforms.map(p => p.count);
271
+ platformChart.update();
272
+ }
273
+
274
+ // Top users table
275
+ const tbody = document.getElementById('top-users');
276
+ if (tbody && data.top_users) {
277
+ if (data.top_users.length === 0) {
278
+ tbody.innerHTML = '<tr><td colspan="2" class="empty-state">No data yet.</td></tr>';
279
+ } else {
280
+ tbody.innerHTML = data.top_users.map(u => `
281
+ <tr>
282
+ <td>${escapeHtml(u.username || 'unknown')}</td>
283
+ <td class="mono">${fmt(u.count)}</td>
284
+ </tr>
285
+ `).join('');
286
+ }
287
+ }
288
+
289
+ } catch (e) {
290
+ console.warn('fetchChartData error', e);
291
+ }
292
+ }
293
+
294
+ async function fetchActiveDownloads() {
295
+ try {
296
+ const res = await fetch('/api/active-downloads', { credentials: 'same-origin' });
297
+ if (!res.ok) return;
298
+ const active = await res.json();
299
+
300
+ const section = document.getElementById('active-downloads');
301
+ if (!section) return;
302
+
303
+ if (!active || active.length === 0) {
304
+ section.style.display = 'none';
305
+ return;
306
+ }
307
+
308
+ section.style.display = '';
309
+
310
+ const countBadge = document.getElementById('active-downloads-count');
311
+ if (countBadge) countBadge.textContent = active.length;
312
+
313
+ const tbody = document.getElementById('active-downloads-body');
314
+ if (tbody) {
315
+ tbody.innerHTML = active.map(dl => `
316
+ <tr>
317
+ <td>${escapeHtml(dl.username || dl.user_id || '—')}</td>
318
+ <td class="url-cell">${escapeHtml((dl.url || '').slice(0, 50))}${(dl.url || '').length > 50 ? '…' : ''}</td>
319
+ <td>${dl.platform ? `<span class="badge badge-platform">${escapeHtml(dl.platform)}</span>` : '—'}</td>
320
+ <td class="mono">${dl.progress != null ? dl.progress.toFixed(0) + '%' : '—'}</td>
321
+ </tr>
322
+ `).join('');
323
+ }
324
+ } catch (e) {
325
+ console.warn('fetchActiveDownloads error', e);
326
+ }
327
+ }
328
+
329
+ function escapeHtml(str) {
330
+ return String(str)
331
+ .replace(/&/g, '&amp;')
332
+ .replace(/</g, '&lt;')
333
+ .replace(/>/g, '&gt;')
334
+ .replace(/"/g, '&quot;');
335
+ }
336
+
337
+ // ---------------------------------------------------------------------------
338
+ // Range button handlers
339
+ // ---------------------------------------------------------------------------
340
+
341
+ function initRangeButtons() {
342
+ const buttons = document.querySelectorAll('.range-btn');
343
+ buttons.forEach(btn => {
344
+ btn.addEventListener('click', () => {
345
+ buttons.forEach(b => b.classList.remove('active'));
346
+ btn.classList.add('active');
347
+ fetchChartData(btn.dataset.range);
348
+ });
349
+ });
350
+ }
351
+
352
+ // ---------------------------------------------------------------------------
353
+ // Admin actions
354
+ // ---------------------------------------------------------------------------
355
+
356
+ function deleteSelected() {
357
+ const checked = Array.from(document.querySelectorAll('.file-check:checked'));
358
+ if (checked.length === 0) {
359
+ showToast('No files selected.', 'warning');
360
+ return;
361
+ }
362
+
363
+ const paths = checked.map(el => el.value);
364
+
365
+ fetch('/api/files', {
366
+ method: 'DELETE',
367
+ credentials: 'same-origin',
368
+ headers: { 'Content-Type': 'application/json' },
369
+ body: JSON.stringify({ paths }),
370
+ })
371
+ .then(res => res.json())
372
+ .then(data => {
373
+ const deleted = data.deleted || [];
374
+ const errors = data.errors || [];
375
+
376
+ if (deleted.length > 0) {
377
+ showToast(`Deleted ${deleted.length} file${deleted.length > 1 ? 's' : ''}.`, 'success');
378
+ // Remove rows from DOM
379
+ deleted.forEach(name => {
380
+ const checkbox = document.querySelector(`.file-check[value="${CSS.escape(name)}"]`);
381
+ if (checkbox) {
382
+ const row = checkbox.closest('tr');
383
+ if (row) row.remove();
384
+ }
385
+ });
386
+ }
387
+
388
+ if (errors.length > 0) {
389
+ showToast(`${errors.length} file${errors.length > 1 ? 's' : ''} could not be deleted.`, 'danger');
390
+ }
391
+ })
392
+ .catch(err => {
393
+ console.error('deleteSelected error', err);
394
+ showToast('Delete failed. See console.', 'danger');
395
+ });
396
+ }
397
+
398
+ function showPurgeModal() {
399
+ const modal = document.getElementById('purge-modal');
400
+ if (modal) {
401
+ modal.style.display = 'flex';
402
+ const input = document.getElementById('purge-confirm');
403
+ if (input) {
404
+ input.value = '';
405
+ input.focus();
406
+ }
407
+ }
408
+ }
409
+
410
+ function closePurgeModal() {
411
+ const modal = document.getElementById('purge-modal');
412
+ if (modal) modal.style.display = 'none';
413
+ }
414
+
415
+ function executePurge() {
416
+ const input = document.getElementById('purge-confirm');
417
+ if (!input || input.value !== 'PURGE') {
418
+ showToast('Type PURGE to confirm.', 'warning');
419
+ return;
420
+ }
421
+
422
+ fetch('/api/files/all', {
423
+ method: 'DELETE',
424
+ credentials: 'same-origin',
425
+ headers: { 'Content-Type': 'application/json' },
426
+ body: JSON.stringify({ confirm: 'PURGE' }),
427
+ })
428
+ .then(res => {
429
+ if (!res.ok) throw new Error('HTTP ' + res.status);
430
+ return res.json();
431
+ })
432
+ .then(() => {
433
+ showToast('All files purged.', 'success');
434
+ closePurgeModal();
435
+ setTimeout(() => location.reload(), 1200);
436
+ })
437
+ .catch(err => {
438
+ console.error('executePurge error', err);
439
+ showToast('Purge failed. See console.', 'danger');
440
+ });
441
+ }
442
+
443
+ // ---------------------------------------------------------------------------
444
+ // Select-all checkbox
445
+ // ---------------------------------------------------------------------------
446
+
447
+ function initSelectAll() {
448
+ const selectAll = document.getElementById('select-all');
449
+ if (!selectAll) return;
450
+
451
+ selectAll.addEventListener('change', () => {
452
+ const checkboxes = document.querySelectorAll('.file-check');
453
+ checkboxes.forEach(cb => { cb.checked = selectAll.checked; });
454
+ });
455
+
456
+ // Update select-all state when individual checkboxes change
457
+ document.addEventListener('change', (e) => {
458
+ if (e.target.classList.contains('file-check')) {
459
+ const all = document.querySelectorAll('.file-check');
460
+ const checked = document.querySelectorAll('.file-check:checked');
461
+ selectAll.indeterminate = checked.length > 0 && checked.length < all.length;
462
+ selectAll.checked = checked.length === all.length && all.length > 0;
463
+ }
464
+ });
465
+ }
466
+
467
+ // ---------------------------------------------------------------------------
468
+ // Polling loop
469
+ // ---------------------------------------------------------------------------
470
+
471
+ let _pollInterval = null;
472
+
473
+ function startPolling() {
474
+ // Run immediately
475
+ fetchStats();
476
+ fetchChartData('1D');
477
+ fetchActiveDownloads();
478
+
479
+ // Then every 30 seconds
480
+ _pollInterval = setInterval(() => {
481
+ fetchStats();
482
+ fetchActiveDownloads();
483
+ // Chart data only re-fetched on range button click (not auto-polled)
484
+ }, 30000);
485
+ }
486
+
487
+ // ---------------------------------------------------------------------------
488
+ // Entry point
489
+ // ---------------------------------------------------------------------------
490
+
491
+ document.addEventListener('DOMContentLoaded', () => {
492
+ const isDashboard = document.getElementById('downloads-chart');
493
+ const isAdmin = document.getElementById('file-table');
494
+
495
+ if (isDashboard) {
496
+ initCharts();
497
+ initRangeButtons();
498
+ startPolling();
499
+ }
500
+
501
+ if (isAdmin) {
502
+ initSelectAll();
503
+ }
504
+ });
505
+
506
+ // Expose admin functions to inline onclick handlers in templates
507
+ window.deleteSelected = deleteSelected;
508
+ window.showPurgeModal = showPurgeModal;
509
+ window.closePurgeModal = closePurgeModal;
510
+ window.executePurge = executePurge;
dashboard/static/style.css ADDED
@@ -0,0 +1,892 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* =========================================================
2
+ reclip admin — design system
3
+ Based on DESIGN.md: industrial/utilitarian dark theme
4
+ ========================================================= */
5
+
6
+ /* ----- CSS Custom Properties ----- */
7
+ :root {
8
+ --bg-primary: #0f0f1a;
9
+ --bg-card: #1a1a2e;
10
+ --bg-sidebar: #141425;
11
+ --bg-hover: #22223a;
12
+ --text-primary: #e0e0e0;
13
+ --text-secondary: #8888a0;
14
+ --accent: #ff6b35;
15
+ --accent-hover: #ff8555;
16
+ --success: #22c55e;
17
+ --warning: #f59e0b;
18
+ --danger: #ef4444;
19
+ --border: #2a2a40;
20
+
21
+ --sidebar-width: 200px;
22
+ --sidebar-collapsed: 56px;
23
+ }
24
+
25
+ /* ----- Reset ----- */
26
+ *, *::before, *::after {
27
+ box-sizing: border-box;
28
+ margin: 0;
29
+ padding: 0;
30
+ }
31
+
32
+ html, body {
33
+ height: 100%;
34
+ }
35
+
36
+ body {
37
+ background: var(--bg-primary);
38
+ color: var(--text-primary);
39
+ font-family: Inter, system-ui, -apple-system, sans-serif;
40
+ font-size: 14px;
41
+ line-height: 1.5;
42
+ }
43
+
44
+ a {
45
+ color: var(--accent);
46
+ text-decoration: none;
47
+ transition: color 0.1s ease;
48
+ }
49
+
50
+ a:hover {
51
+ color: var(--accent-hover);
52
+ }
53
+
54
+ /* ----- Hamburger (mobile only) ----- */
55
+ .hamburger {
56
+ display: none;
57
+ position: fixed;
58
+ top: 12px;
59
+ left: 12px;
60
+ z-index: 300;
61
+ background: var(--bg-card);
62
+ border: 1px solid var(--border);
63
+ border-radius: 4px;
64
+ padding: 6px 8px;
65
+ cursor: pointer;
66
+ flex-direction: column;
67
+ gap: 4px;
68
+ }
69
+
70
+ .hamburger span {
71
+ display: block;
72
+ width: 18px;
73
+ height: 2px;
74
+ background: var(--text-primary);
75
+ border-radius: 1px;
76
+ transition: background 0.1s ease;
77
+ }
78
+
79
+ .hamburger:hover span {
80
+ background: var(--accent);
81
+ }
82
+
83
+ /* ----- Sidebar ----- */
84
+ .sidebar {
85
+ position: fixed;
86
+ top: 0;
87
+ left: 0;
88
+ height: 100vh;
89
+ width: var(--sidebar-width);
90
+ background: var(--bg-sidebar);
91
+ border-right: 1px solid var(--border);
92
+ display: flex;
93
+ flex-direction: column;
94
+ z-index: 200;
95
+ overflow: hidden;
96
+ }
97
+
98
+ .sidebar-logo {
99
+ padding: 20px 16px 16px;
100
+ font-size: 1.125rem;
101
+ font-weight: 700;
102
+ color: var(--accent);
103
+ letter-spacing: -0.01em;
104
+ border-bottom: 1px solid var(--border);
105
+ white-space: nowrap;
106
+ }
107
+
108
+ .sidebar-nav {
109
+ flex: 1;
110
+ padding: 8px 0;
111
+ overflow-y: auto;
112
+ }
113
+
114
+ .nav-item {
115
+ display: flex;
116
+ align-items: center;
117
+ gap: 10px;
118
+ padding: 9px 16px;
119
+ font-size: 0.875rem;
120
+ color: var(--text-secondary);
121
+ text-decoration: none;
122
+ white-space: nowrap;
123
+ transition: background 0.1s ease, color 0.1s ease;
124
+ border-left: 3px solid transparent;
125
+ }
126
+
127
+ .nav-item:hover {
128
+ background: var(--bg-hover);
129
+ color: var(--text-primary);
130
+ }
131
+
132
+ .nav-item.active {
133
+ border-left-color: var(--accent);
134
+ color: var(--accent);
135
+ background: var(--bg-hover);
136
+ font-weight: 500;
137
+ }
138
+
139
+ .nav-item svg {
140
+ flex-shrink: 0;
141
+ opacity: 0.7;
142
+ }
143
+
144
+ .nav-item.active svg,
145
+ .nav-item:hover svg {
146
+ opacity: 1;
147
+ }
148
+
149
+ .sidebar-bottom {
150
+ border-top: 1px solid var(--border);
151
+ padding: 8px 0;
152
+ }
153
+
154
+ /* Sidebar overlay (mobile) */
155
+ .sidebar-overlay {
156
+ display: none;
157
+ position: fixed;
158
+ inset: 0;
159
+ background: rgba(0, 0, 0, 0.6);
160
+ z-index: 150;
161
+ }
162
+
163
+ .sidebar-overlay.visible {
164
+ display: block;
165
+ }
166
+
167
+ /* ----- Main Content ----- */
168
+ .content {
169
+ margin-left: var(--sidebar-width);
170
+ min-height: 100vh;
171
+ }
172
+
173
+ .content-inner {
174
+ max-width: 1400px;
175
+ padding: 32px;
176
+ margin: 0 auto;
177
+ }
178
+
179
+ /* ----- Page title ----- */
180
+ .page-title {
181
+ font-size: 1.25rem;
182
+ font-weight: 600;
183
+ color: var(--text-primary);
184
+ margin-bottom: 24px;
185
+ }
186
+
187
+ /* ----- KPI Grid ----- */
188
+ .kpi-grid {
189
+ display: grid;
190
+ grid-template-columns: repeat(4, 1fr);
191
+ gap: 16px;
192
+ margin-bottom: 24px;
193
+ }
194
+
195
+ .kpi-card {
196
+ background: var(--bg-card);
197
+ border: 1px solid var(--border);
198
+ border-radius: 6px;
199
+ padding: 16px;
200
+ text-decoration: none;
201
+ display: block;
202
+ transition: border-color 0.1s ease;
203
+ }
204
+
205
+ .kpi-card:hover {
206
+ border-color: var(--accent);
207
+ color: inherit;
208
+ }
209
+
210
+ .kpi-label {
211
+ font-size: 0.75rem;
212
+ font-weight: 500;
213
+ text-transform: uppercase;
214
+ letter-spacing: 0.05em;
215
+ color: var(--text-secondary);
216
+ margin-bottom: 8px;
217
+ }
218
+
219
+ .kpi-value {
220
+ font-size: 2rem;
221
+ font-weight: 600;
222
+ color: var(--text-primary);
223
+ line-height: 1.1;
224
+ margin-bottom: 6px;
225
+ }
226
+
227
+ .kpi-trend {
228
+ font-size: 0.75rem;
229
+ color: var(--text-secondary);
230
+ min-height: 1em;
231
+ }
232
+
233
+ .kpi-trend.up { color: var(--success); }
234
+ .kpi-trend.down { color: var(--danger); }
235
+
236
+ /* Disk bar */
237
+ .disk-bar-wrap {
238
+ height: 4px;
239
+ background: var(--bg-hover);
240
+ border-radius: 2px;
241
+ margin-top: 8px;
242
+ overflow: hidden;
243
+ }
244
+
245
+ .disk-bar {
246
+ height: 100%;
247
+ border-radius: 2px;
248
+ background: var(--success);
249
+ width: 0%;
250
+ transition: width 0.4s ease-out, background 0.2s ease;
251
+ }
252
+
253
+ .disk-bar.amber { background: var(--warning); }
254
+ .disk-bar.red { background: var(--danger); }
255
+
256
+ /* ----- Cards ----- */
257
+ .card {
258
+ background: var(--bg-card);
259
+ border: 1px solid var(--border);
260
+ border-radius: 6px;
261
+ padding: 16px;
262
+ margin-bottom: 24px;
263
+ }
264
+
265
+ .section-card {
266
+ margin-bottom: 24px;
267
+ }
268
+
269
+ .table-card {
270
+ padding: 0;
271
+ overflow: hidden;
272
+ }
273
+
274
+ .card-header {
275
+ display: flex;
276
+ align-items: center;
277
+ justify-content: space-between;
278
+ margin-bottom: 16px;
279
+ flex-wrap: wrap;
280
+ gap: 8px;
281
+ }
282
+
283
+ .table-card .card-header {
284
+ padding: 16px 16px 0;
285
+ }
286
+
287
+ .card-title {
288
+ font-size: 0.875rem;
289
+ font-weight: 600;
290
+ color: var(--text-primary);
291
+ text-transform: uppercase;
292
+ letter-spacing: 0.03em;
293
+ }
294
+
295
+ .card-actions {
296
+ display: flex;
297
+ gap: 8px;
298
+ flex-wrap: wrap;
299
+ }
300
+
301
+ /* ----- Chart ----- */
302
+ .chart-wrap {
303
+ height: 240px;
304
+ position: relative;
305
+ }
306
+
307
+ .chart-wrap--short {
308
+ height: 180px;
309
+ }
310
+
311
+ /* ----- Bottom grid ----- */
312
+ .bottom-grid {
313
+ display: grid;
314
+ grid-template-columns: 1fr 1fr;
315
+ gap: 16px;
316
+ margin-bottom: 24px;
317
+ }
318
+
319
+ /* ----- Range buttons ----- */
320
+ .range-buttons {
321
+ display: flex;
322
+ gap: 4px;
323
+ }
324
+
325
+ .range-btn {
326
+ padding: 4px 10px;
327
+ font-size: 0.75rem;
328
+ font-family: inherit;
329
+ font-weight: 500;
330
+ background: transparent;
331
+ border: 1px solid var(--border);
332
+ border-radius: 4px;
333
+ color: var(--text-secondary);
334
+ cursor: pointer;
335
+ transition: background 0.1s ease, color 0.1s ease, border-color 0.1s ease;
336
+ }
337
+
338
+ .range-btn:hover,
339
+ .range-btn.active {
340
+ background: var(--accent);
341
+ border-color: var(--accent);
342
+ color: var(--bg-primary);
343
+ }
344
+
345
+ /* ----- Data Tables ----- */
346
+ .table-scroll {
347
+ overflow-x: auto;
348
+ }
349
+
350
+ .data-table {
351
+ width: 100%;
352
+ border-collapse: collapse;
353
+ font-size: 0.875rem;
354
+ }
355
+
356
+ .data-table thead {
357
+ position: sticky;
358
+ top: 0;
359
+ z-index: 1;
360
+ }
361
+
362
+ .data-table th {
363
+ font-size: 0.75rem;
364
+ font-weight: 500;
365
+ text-transform: uppercase;
366
+ letter-spacing: 0.05em;
367
+ color: var(--text-secondary);
368
+ background: var(--bg-card);
369
+ padding: 10px 12px;
370
+ text-align: left;
371
+ border-bottom: 1px solid var(--border);
372
+ white-space: nowrap;
373
+ }
374
+
375
+ .data-table td {
376
+ padding: 9px 12px;
377
+ border-bottom: 1px solid var(--border);
378
+ color: var(--text-primary);
379
+ vertical-align: middle;
380
+ }
381
+
382
+ .data-table tr:last-child td {
383
+ border-bottom: none;
384
+ }
385
+
386
+ /* Striped rows */
387
+ .data-table tbody tr:nth-child(even) td {
388
+ background: var(--bg-card);
389
+ }
390
+
391
+ .data-table tbody tr:nth-child(odd) td {
392
+ background: var(--bg-primary);
393
+ }
394
+
395
+ .data-table tbody tr:hover td {
396
+ background: var(--bg-hover);
397
+ }
398
+
399
+ /* Column check */
400
+ .col-check {
401
+ width: 36px;
402
+ text-align: center !important;
403
+ }
404
+
405
+ input[type="checkbox"] {
406
+ cursor: pointer;
407
+ width: 14px;
408
+ height: 14px;
409
+ accent-color: var(--accent);
410
+ }
411
+
412
+ /* ----- Info table (key-value) ----- */
413
+ .info-table {
414
+ width: 100%;
415
+ border-collapse: collapse;
416
+ }
417
+
418
+ .info-table td {
419
+ padding: 8px 12px;
420
+ border-bottom: 1px solid var(--border);
421
+ font-size: 0.875rem;
422
+ }
423
+
424
+ .info-table tr:last-child td {
425
+ border-bottom: none;
426
+ }
427
+
428
+ .info-key {
429
+ color: var(--text-secondary);
430
+ font-size: 0.75rem;
431
+ font-weight: 500;
432
+ text-transform: uppercase;
433
+ letter-spacing: 0.05em;
434
+ width: 200px;
435
+ }
436
+
437
+ /* ----- Empty state ----- */
438
+ .empty-state {
439
+ text-align: center;
440
+ color: var(--text-secondary);
441
+ padding: 32px 12px !important;
442
+ font-style: italic;
443
+ }
444
+
445
+ .empty-state--success {
446
+ color: var(--success);
447
+ font-style: normal;
448
+ }
449
+
450
+ .empty-check {
451
+ font-size: 1.1rem;
452
+ margin-right: 6px;
453
+ }
454
+
455
+ /* ----- Badges ----- */
456
+ .badge {
457
+ display: inline-block;
458
+ font-size: 0.6875rem;
459
+ font-weight: 500;
460
+ padding: 2px 8px;
461
+ border-radius: 4px;
462
+ vertical-align: middle;
463
+ line-height: 1.4;
464
+ white-space: nowrap;
465
+ background: rgba(136, 136, 160, 0.2);
466
+ color: var(--text-secondary);
467
+ }
468
+
469
+ .badge-success {
470
+ background: rgba(34, 197, 94, 0.2);
471
+ color: var(--success);
472
+ }
473
+
474
+ .badge-danger {
475
+ background: rgba(239, 68, 68, 0.2);
476
+ color: var(--danger);
477
+ }
478
+
479
+ .badge-warning {
480
+ background: rgba(245, 158, 11, 0.2);
481
+ color: var(--warning);
482
+ }
483
+
484
+ .badge-platform {
485
+ background: rgba(255, 107, 53, 0.2);
486
+ color: var(--accent);
487
+ }
488
+
489
+ /* ----- Buttons ----- */
490
+ .btn {
491
+ display: inline-flex;
492
+ align-items: center;
493
+ justify-content: center;
494
+ padding: 8px 16px;
495
+ font-size: 0.875rem;
496
+ font-family: inherit;
497
+ font-weight: 500;
498
+ border-radius: 4px;
499
+ border: 1px solid transparent;
500
+ cursor: pointer;
501
+ text-decoration: none;
502
+ white-space: nowrap;
503
+ transition: background 0.1s ease, border-color 0.1s ease, color 0.1s ease;
504
+ }
505
+
506
+ .btn-primary {
507
+ background: var(--accent);
508
+ color: var(--bg-primary);
509
+ border-color: var(--accent);
510
+ }
511
+
512
+ .btn-primary:hover {
513
+ background: var(--accent-hover);
514
+ border-color: var(--accent-hover);
515
+ color: var(--bg-primary);
516
+ }
517
+
518
+ .btn-secondary {
519
+ background: transparent;
520
+ color: var(--accent);
521
+ border-color: var(--accent);
522
+ }
523
+
524
+ .btn-secondary:hover {
525
+ background: rgba(255, 107, 53, 0.1);
526
+ color: var(--accent-hover);
527
+ }
528
+
529
+ .btn-danger {
530
+ background: var(--danger);
531
+ color: #fff;
532
+ border-color: var(--danger);
533
+ }
534
+
535
+ .btn-danger:hover {
536
+ background: #dc2626;
537
+ border-color: #dc2626;
538
+ color: #fff;
539
+ }
540
+
541
+ /* ----- Filter bar ----- */
542
+ .filter-bar {
543
+ display: flex;
544
+ flex-wrap: wrap;
545
+ gap: 8px;
546
+ margin-bottom: 16px;
547
+ align-items: center;
548
+ }
549
+
550
+ .filter-input,
551
+ .filter-select {
552
+ padding: 7px 10px;
553
+ font-size: 0.875rem;
554
+ font-family: inherit;
555
+ background: var(--bg-card);
556
+ border: 1px solid var(--border);
557
+ border-radius: 4px;
558
+ color: var(--text-primary);
559
+ outline: none;
560
+ transition: border-color 0.1s ease;
561
+ }
562
+
563
+ .filter-input:focus,
564
+ .filter-select:focus {
565
+ border-color: var(--accent);
566
+ }
567
+
568
+ .filter-select option {
569
+ background: var(--bg-card);
570
+ }
571
+
572
+ /* ----- Results info ----- */
573
+ .results-info {
574
+ font-size: 0.8125rem;
575
+ color: var(--text-secondary);
576
+ margin-bottom: 12px;
577
+ }
578
+
579
+ /* ----- URL cell ----- */
580
+ .url-cell {
581
+ font-family: 'JetBrains Mono', ui-monospace, monospace;
582
+ font-size: 0.75rem;
583
+ color: var(--accent);
584
+ word-break: break-all;
585
+ }
586
+
587
+ /* ----- Error message cell ----- */
588
+ .error-message {
589
+ color: var(--danger);
590
+ font-size: 0.8125rem;
591
+ max-width: 400px;
592
+ }
593
+
594
+ /* ----- Text utilities ----- */
595
+ .mono {
596
+ font-family: 'JetBrains Mono', ui-monospace, 'Cascadia Code', 'Fira Code', monospace;
597
+ }
598
+
599
+ .text-secondary {
600
+ color: var(--text-secondary);
601
+ }
602
+
603
+ /* ----- Pagination ----- */
604
+ .pagination {
605
+ display: flex;
606
+ align-items: center;
607
+ gap: 6px;
608
+ flex-wrap: wrap;
609
+ margin-top: 16px;
610
+ }
611
+
612
+ .page-num {
613
+ display: inline-flex;
614
+ align-items: center;
615
+ justify-content: center;
616
+ min-width: 32px;
617
+ height: 32px;
618
+ padding: 0 8px;
619
+ font-size: 0.8125rem;
620
+ border: 1px solid var(--border);
621
+ border-radius: 4px;
622
+ color: var(--text-secondary);
623
+ text-decoration: none;
624
+ transition: background 0.1s ease, color 0.1s ease;
625
+ }
626
+
627
+ .page-num:hover {
628
+ background: var(--bg-hover);
629
+ color: var(--text-primary);
630
+ }
631
+
632
+ .page-num.active {
633
+ background: var(--accent);
634
+ border-color: var(--accent);
635
+ color: var(--bg-primary);
636
+ font-weight: 600;
637
+ }
638
+
639
+ .page-ellipsis {
640
+ color: var(--text-secondary);
641
+ padding: 0 4px;
642
+ }
643
+
644
+ /* ----- Disk summary ----- */
645
+ .disk-summary {
646
+ display: flex;
647
+ gap: 24px;
648
+ font-size: 0.8125rem;
649
+ margin-bottom: 16px;
650
+ flex-wrap: wrap;
651
+ }
652
+
653
+ /* ----- Modal ----- */
654
+ .modal-overlay {
655
+ position: fixed;
656
+ inset: 0;
657
+ background: rgba(0, 0, 0, 0.7);
658
+ z-index: 500;
659
+ display: flex;
660
+ align-items: center;
661
+ justify-content: center;
662
+ padding: 16px;
663
+ }
664
+
665
+ .modal-card {
666
+ background: var(--bg-card);
667
+ border: 1px solid var(--border);
668
+ border-radius: 6px;
669
+ padding: 24px;
670
+ width: 100%;
671
+ max-width: 440px;
672
+ }
673
+
674
+ .modal-card--danger {
675
+ border-color: var(--danger);
676
+ }
677
+
678
+ .modal-title {
679
+ font-size: 1.1rem;
680
+ font-weight: 600;
681
+ color: var(--text-primary);
682
+ margin-bottom: 12px;
683
+ }
684
+
685
+ .modal-body {
686
+ font-size: 0.875rem;
687
+ color: var(--text-secondary);
688
+ margin-bottom: 12px;
689
+ line-height: 1.6;
690
+ }
691
+
692
+ .modal-body strong {
693
+ color: var(--text-primary);
694
+ }
695
+
696
+ .modal-actions {
697
+ display: flex;
698
+ gap: 8px;
699
+ justify-content: flex-end;
700
+ margin-top: 16px;
701
+ }
702
+
703
+ /* ----- Toasts ----- */
704
+ .toast-container {
705
+ position: fixed;
706
+ top: 16px;
707
+ right: 16px;
708
+ z-index: 600;
709
+ display: flex;
710
+ flex-direction: column;
711
+ gap: 8px;
712
+ max-width: 360px;
713
+ }
714
+
715
+ .toast {
716
+ background: var(--bg-card);
717
+ border: 1px solid var(--border);
718
+ border-radius: 6px;
719
+ padding: 12px 16px;
720
+ font-size: 0.875rem;
721
+ color: var(--text-primary);
722
+ display: flex;
723
+ align-items: flex-start;
724
+ gap: 10px;
725
+ animation: toast-in 0.2s ease-out;
726
+ }
727
+
728
+ .toast.toast-success { border-color: var(--success); }
729
+ .toast.toast-danger { border-color: var(--danger); }
730
+ .toast.toast-warning { border-color: var(--warning); }
731
+
732
+ .toast-close {
733
+ margin-left: auto;
734
+ background: none;
735
+ border: none;
736
+ color: var(--text-secondary);
737
+ cursor: pointer;
738
+ font-size: 1rem;
739
+ line-height: 1;
740
+ padding: 0;
741
+ flex-shrink: 0;
742
+ transition: color 0.1s ease;
743
+ }
744
+
745
+ .toast-close:hover {
746
+ color: var(--text-primary);
747
+ }
748
+
749
+ @keyframes toast-in {
750
+ from {
751
+ opacity: 0;
752
+ transform: translateX(20px);
753
+ }
754
+ to {
755
+ opacity: 1;
756
+ transform: translateX(0);
757
+ }
758
+ }
759
+
760
+ /* ----- Responsive: Tablet (768–1023px) ----- */
761
+ @media (max-width: 1023px) {
762
+ .sidebar {
763
+ width: var(--sidebar-collapsed);
764
+ }
765
+
766
+ .sidebar-logo {
767
+ font-size: 0.875rem;
768
+ padding: 16px 8px;
769
+ text-align: center;
770
+ overflow: hidden;
771
+ white-space: nowrap;
772
+ text-overflow: clip;
773
+ /* Hide text, show first char only as icon */
774
+ }
775
+
776
+ .sidebar-logo::after {
777
+ content: 'rc';
778
+ font-size: 0.75rem;
779
+ }
780
+
781
+ .sidebar-logo {
782
+ font-size: 0;
783
+ }
784
+
785
+ .sidebar-logo::after {
786
+ font-size: 0.75rem;
787
+ color: var(--accent);
788
+ font-weight: 700;
789
+ }
790
+
791
+ .nav-item {
792
+ justify-content: center;
793
+ padding: 12px 8px;
794
+ gap: 0;
795
+ }
796
+
797
+ .nav-item span {
798
+ display: none;
799
+ }
800
+
801
+ .nav-item svg {
802
+ width: 20px;
803
+ height: 20px;
804
+ }
805
+
806
+ .content {
807
+ margin-left: var(--sidebar-collapsed);
808
+ }
809
+
810
+ .kpi-grid {
811
+ grid-template-columns: repeat(2, 1fr);
812
+ }
813
+ }
814
+
815
+ /* ----- Responsive: Mobile (≤767px) ----- */
816
+ @media (max-width: 767px) {
817
+ .hamburger {
818
+ display: flex;
819
+ }
820
+
821
+ .sidebar {
822
+ width: var(--sidebar-width);
823
+ transform: translateX(-100%);
824
+ transition: transform 0.2s ease-out;
825
+ }
826
+
827
+ .sidebar.open {
828
+ transform: translateX(0);
829
+ }
830
+
831
+ /* Restore full sidebar look on mobile overlay */
832
+ .sidebar .sidebar-logo {
833
+ font-size: 0;
834
+ }
835
+
836
+ .sidebar .sidebar-logo::after {
837
+ font-size: 1.125rem;
838
+ content: 'reclip';
839
+ }
840
+
841
+ .sidebar .nav-item {
842
+ justify-content: flex-start;
843
+ padding: 9px 16px;
844
+ gap: 10px;
845
+ }
846
+
847
+ .sidebar .nav-item span {
848
+ display: inline;
849
+ }
850
+
851
+ .content {
852
+ margin-left: 0;
853
+ }
854
+
855
+ .content-inner {
856
+ padding: 60px 16px 24px;
857
+ }
858
+
859
+ .kpi-grid {
860
+ grid-template-columns: 1fr;
861
+ }
862
+
863
+ .bottom-grid {
864
+ grid-template-columns: 1fr;
865
+ }
866
+
867
+ .table-scroll {
868
+ overflow-x: auto;
869
+ }
870
+
871
+ .data-table {
872
+ min-width: 600px;
873
+ }
874
+
875
+ .filter-bar {
876
+ flex-direction: column;
877
+ align-items: stretch;
878
+ }
879
+
880
+ .filter-input,
881
+ .filter-select {
882
+ width: 100%;
883
+ }
884
+ }
885
+
886
+ /* ----- Reduced motion ----- */
887
+ @media (prefers-reduced-motion: reduce) {
888
+ *, *::before, *::after {
889
+ animation: none !important;
890
+ transition: none !important;
891
+ }
892
+ }
dashboard/templates/admin.html ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends "base.html" %}
2
+ {% set active = "admin" %}
3
+
4
+ {% block title %} — admin{% endblock %}
5
+
6
+ {% block content %}
7
+ <h1 class="page-title">Admin</h1>
8
+
9
+ <!-- Disk Management -->
10
+ <div class="card section-card">
11
+ <div class="card-header">
12
+ <h2 class="card-title">Disk Management</h2>
13
+ <div class="card-actions">
14
+ <button class="btn btn-secondary" onclick="deleteSelected()">Delete Selected</button>
15
+ <button class="btn btn-danger" onclick="showPurgeModal()">Purge All</button>
16
+ </div>
17
+ </div>
18
+
19
+ <div class="disk-summary">
20
+ <span class="text-secondary">Downloads: <span class="mono">{% if downloads_bytes >= 1073741824 %}{{ "%.1f"|format(downloads_bytes / 1073741824) }} GB{% elif downloads_bytes >= 1048576 %}{{ "%.1f"|format(downloads_bytes / 1048576) }} MB{% else %}{{ "%.1f"|format(downloads_bytes / 1024) }} KB{% endif %}</span></span>
21
+ <span class="text-secondary">Limit: <span class="mono">{{ "%.1f"|format(max_disk_mb / 1024) }} GB</span></span>
22
+ <span class="text-secondary">Usage: <span class="mono">{{ disk_pct }}%</span></span>
23
+ </div>
24
+
25
+ <div class="table-scroll">
26
+ <table class="data-table" id="file-table">
27
+ <thead>
28
+ <tr>
29
+ <th class="col-check">
30
+ <input type="checkbox" id="select-all" />
31
+ </th>
32
+ <th>Filename</th>
33
+ <th>Size</th>
34
+ <th>Type</th>
35
+ </tr>
36
+ </thead>
37
+ <tbody>
38
+ {% if files %}
39
+ {% for f in files %}
40
+ <tr>
41
+ <td class="col-check">
42
+ <input type="checkbox" class="file-check" value="{{ f.name }}" />
43
+ </td>
44
+ <td class="mono">{{ f.name }}</td>
45
+ <td class="mono">
46
+ {% if f.size >= 1073741824 %}
47
+ {{ "%.1f"|format(f.size / 1073741824) }} GB
48
+ {% elif f.size >= 1048576 %}
49
+ {{ "%.1f"|format(f.size / 1048576) }} MB
50
+ {% elif f.size >= 1024 %}
51
+ {{ "%.1f"|format(f.size / 1024) }} KB
52
+ {% else %}
53
+ {{ f.size }} B
54
+ {% endif %}
55
+ </td>
56
+ <td>
57
+ {% if f.is_dir %}
58
+ <span class="badge badge-platform">dir</span>
59
+ {% else %}
60
+ <span class="text-secondary">file</span>
61
+ {% endif %}
62
+ </td>
63
+ </tr>
64
+ {% endfor %}
65
+ {% else %}
66
+ <tr>
67
+ <td colspan="4" class="empty-state">No files in downloads directory.</td>
68
+ </tr>
69
+ {% endif %}
70
+ </tbody>
71
+ </table>
72
+ </div>
73
+ </div>
74
+
75
+ <!-- System Info -->
76
+ <div class="card section-card">
77
+ <div class="card-header">
78
+ <h2 class="card-title">System Info</h2>
79
+ </div>
80
+ <table class="info-table">
81
+ <tbody>
82
+ <tr>
83
+ <td class="info-key">Files in downloads</td>
84
+ <td class="mono">{{ files|length }}</td>
85
+ </tr>
86
+ <tr>
87
+ <td class="info-key">Space used by downloads</td>
88
+ <td class="mono">{% if downloads_bytes >= 1073741824 %}{{ "%.2f"|format(downloads_bytes / 1073741824) }} GB{% elif downloads_bytes >= 1048576 %}{{ "%.1f"|format(downloads_bytes / 1048576) }} MB{% else %}{{ downloads_bytes }} B{% endif %}</td>
89
+ </tr>
90
+ <tr>
91
+ <td class="info-key">Cleanup limit</td>
92
+ <td class="mono">{{ max_disk_mb }} MB ({{ "%.1f"|format(max_disk_mb / 1024) }} GB)</td>
93
+ </tr>
94
+ <tr>
95
+ <td class="info-key">Usage</td>
96
+ <td class="mono">{{ disk_pct }}%</td>
97
+ </tr>
98
+ </tbody>
99
+ </table>
100
+ </div>
101
+
102
+ <!-- Purge Modal -->
103
+ <div class="modal-overlay" id="purge-modal" style="display:none" role="dialog" aria-modal="true" aria-labelledby="purge-modal-title">
104
+ <div class="modal-card modal-card--danger">
105
+ <h2 class="modal-title" id="purge-modal-title">Purge All Files</h2>
106
+ <p class="modal-body">This will permanently delete <strong>all files</strong> in the downloads directory. This action cannot be undone.</p>
107
+ <p class="modal-body">Type <strong>PURGE</strong> to confirm:</p>
108
+ <input type="text" id="purge-confirm" class="filter-input" placeholder="PURGE" autocomplete="off" />
109
+ <div class="modal-actions">
110
+ <button class="btn btn-secondary" onclick="closePurgeModal()">Cancel</button>
111
+ <button class="btn btn-danger" onclick="executePurge()">Purge All Files</button>
112
+ </div>
113
+ </div>
114
+ </div>
115
+ {% endblock %}
dashboard/templates/base.html ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>reclip admin{% block title %}{% endblock %}</title>
7
+ <link rel="preconnect" href="https://fonts.googleapis.com" />
8
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
9
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet" />
10
+ <script src="https://cdn.jsdelivr.net/npm/chart.js@4/dist/chart.umd.min.js"></script>
11
+ <link rel="stylesheet" href="/static/style.css" />
12
+ </head>
13
+ <body>
14
+
15
+ <!-- Hamburger button (mobile only) -->
16
+ <button class="hamburger" id="hamburger" aria-label="Toggle sidebar">
17
+ <span></span><span></span><span></span>
18
+ </button>
19
+
20
+ <!-- Sidebar -->
21
+ <aside class="sidebar" id="sidebar">
22
+ <div class="sidebar-logo">reclip</div>
23
+ <nav class="sidebar-nav">
24
+ <a href="/" class="nav-item{% if active == 'dashboard' %} active{% endif %}">
25
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
26
+ <span>Dashboard</span>
27
+ </a>
28
+ <a href="/history" class="nav-item{% if active == 'history' %} active{% endif %}">
29
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
30
+ <span>History</span>
31
+ </a>
32
+ <a href="/errors" class="nav-item{% if active == 'errors' %} active{% endif %}">
33
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
34
+ <span>Errors</span>
35
+ </a>
36
+ <a href="/admin" class="nav-item{% if active == 'admin' %} active{% endif %}">
37
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.07 4.93a10 10 0 010 14.14M4.93 4.93a10 10 0 000 14.14"/></svg>
38
+ <span>Admin</span>
39
+ </a>
40
+ </nav>
41
+ <div class="sidebar-bottom">
42
+ <a href="/logout" class="nav-item">
43
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>
44
+ <span>Logout</span>
45
+ </a>
46
+ </div>
47
+ </aside>
48
+
49
+ <!-- Sidebar overlay (mobile) -->
50
+ <div class="sidebar-overlay" id="sidebar-overlay"></div>
51
+
52
+ <!-- Main content -->
53
+ <main class="content">
54
+ <div class="content-inner">
55
+ {% block content %}{% endblock %}
56
+ </div>
57
+ </main>
58
+
59
+ <!-- Toast container -->
60
+ <div id="toast-container" class="toast-container"></div>
61
+
62
+ <script src="/static/dashboard.js"></script>
63
+ <script>
64
+ // Hamburger toggle
65
+ const hamburger = document.getElementById('hamburger');
66
+ const sidebar = document.getElementById('sidebar');
67
+ const overlay = document.getElementById('sidebar-overlay');
68
+ if (hamburger) {
69
+ hamburger.addEventListener('click', () => {
70
+ sidebar.classList.toggle('open');
71
+ overlay.classList.toggle('visible');
72
+ });
73
+ overlay.addEventListener('click', () => {
74
+ sidebar.classList.remove('open');
75
+ overlay.classList.remove('visible');
76
+ });
77
+ }
78
+ </script>
79
+ </body>
80
+ </html>
dashboard/templates/dashboard.html ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends "base.html" %}
2
+ {% set active = "dashboard" %}
3
+
4
+ {% block title %} — dashboard{% endblock %}
5
+
6
+ {% block content %}
7
+ <h1 class="page-title">Dashboard</h1>
8
+
9
+ <!-- KPI Grid -->
10
+ <div class="kpi-grid">
11
+ <a href="/history?date=today" class="kpi-card">
12
+ <div class="kpi-label">Total Downloads Today</div>
13
+ <div class="kpi-value mono" id="kpi-downloads">--</div>
14
+ <div class="kpi-trend" id="kpi-downloads-trend"></div>
15
+ </a>
16
+ <a href="/history?date=today" class="kpi-card">
17
+ <div class="kpi-label">Active Users (24h)</div>
18
+ <div class="kpi-value mono" id="kpi-users">--</div>
19
+ <div class="kpi-trend" id="kpi-users-trend"></div>
20
+ </a>
21
+ <a href="/admin" class="kpi-card">
22
+ <div class="kpi-label">Disk Usage</div>
23
+ <div class="kpi-value mono" id="kpi-disk">--</div>
24
+ <div class="disk-bar-wrap">
25
+ <div class="disk-bar" id="disk-bar"></div>
26
+ </div>
27
+ </a>
28
+ <a href="/errors" class="kpi-card">
29
+ <div class="kpi-label">Error Rate</div>
30
+ <div class="kpi-value mono" id="kpi-errors">--</div>
31
+ <div class="kpi-trend" id="kpi-errors-trend"></div>
32
+ </a>
33
+ </div>
34
+
35
+ <!-- Downloads Over Time -->
36
+ <div class="card section-card">
37
+ <div class="card-header">
38
+ <h2 class="card-title">Downloads Over Time</h2>
39
+ <div class="range-buttons">
40
+ <button class="range-btn active" data-range="1D">1D</button>
41
+ <button class="range-btn" data-range="7D">7D</button>
42
+ <button class="range-btn" data-range="1M">1M</button>
43
+ <button class="range-btn" data-range="1Y">1Y</button>
44
+ </div>
45
+ </div>
46
+ <div class="chart-wrap">
47
+ <canvas id="downloads-chart"></canvas>
48
+ </div>
49
+ </div>
50
+
51
+ <!-- Bottom grid -->
52
+ <div class="bottom-grid">
53
+ <div class="card">
54
+ <div class="card-header">
55
+ <h2 class="card-title">Downloads by Platform</h2>
56
+ </div>
57
+ <div class="chart-wrap chart-wrap--short">
58
+ <canvas id="platform-chart"></canvas>
59
+ </div>
60
+ </div>
61
+ <div class="card">
62
+ <div class="card-header">
63
+ <h2 class="card-title">Top Users</h2>
64
+ </div>
65
+ <table class="data-table">
66
+ <thead>
67
+ <tr>
68
+ <th>User</th>
69
+ <th class="mono">Downloads</th>
70
+ </tr>
71
+ </thead>
72
+ <tbody id="top-users">
73
+ <tr><td colspan="2" class="empty-state">Loading...</td></tr>
74
+ </tbody>
75
+ </table>
76
+ </div>
77
+ </div>
78
+
79
+ <!-- Active Downloads (hidden by default) -->
80
+ <div class="card" id="active-downloads" style="display:none">
81
+ <div class="card-header">
82
+ <h2 class="card-title">Active Downloads</h2>
83
+ <span class="badge badge-warning" id="active-downloads-count">0</span>
84
+ </div>
85
+ <table class="data-table">
86
+ <thead>
87
+ <tr>
88
+ <th>User</th>
89
+ <th>URL</th>
90
+ <th>Platform</th>
91
+ <th>Progress</th>
92
+ </tr>
93
+ </thead>
94
+ <tbody id="active-downloads-body"></tbody>
95
+ </table>
96
+ </div>
97
+ {% endblock %}
dashboard/templates/errors.html ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends "base.html" %}
2
+ {% set active = "errors" %}
3
+
4
+ {% block title %} — errors{% endblock %}
5
+
6
+ {% block content %}
7
+ <h1 class="page-title">Download Errors</h1>
8
+
9
+ <!-- Date filter -->
10
+ <form class="filter-bar" method="get" action="/errors">
11
+ <input type="date" name="date_from" class="filter-input" value="{{ request.query_params.get('date_from', '') }}" />
12
+ <input type="date" name="date_to" class="filter-input" value="{{ request.query_params.get('date_to', '') }}" />
13
+ <button type="submit" class="btn btn-primary">Filter</button>
14
+ <a href="/errors" class="btn btn-secondary">Clear</a>
15
+ </form>
16
+
17
+ <div class="card table-card">
18
+ <div class="table-scroll">
19
+ <table class="data-table">
20
+ <thead>
21
+ <tr>
22
+ <th>Timestamp</th>
23
+ <th>User</th>
24
+ <th>URL</th>
25
+ <th>Platform</th>
26
+ <th>Error</th>
27
+ </tr>
28
+ </thead>
29
+ <tbody>
30
+ {% if errors %}
31
+ {% for row in errors %}
32
+ <tr>
33
+ <td class="mono text-secondary">{{ row.started_at[:19] if row.started_at else '—' }}</td>
34
+ <td>{{ row.username or row.user_id or '—' }}</td>
35
+ <td>
36
+ {% if row.url %}
37
+ <a href="{{ row.url }}" target="_blank" rel="noopener" class="url-cell" title="{{ row.url }}">
38
+ {{ row.url[:60] }}{% if row.url|length > 60 %}…{% endif %}
39
+ </a>
40
+ {% else %}—{% endif %}
41
+ </td>
42
+ <td>
43
+ {% if row.platform %}
44
+ <span class="badge badge-platform">{{ row.platform }}</span>
45
+ {% else %}—{% endif %}
46
+ </td>
47
+ <td class="error-message">{{ row.error_message or '—' }}</td>
48
+ </tr>
49
+ {% endfor %}
50
+ {% else %}
51
+ <tr>
52
+ <td colspan="5" class="empty-state empty-state--success">
53
+ <span class="empty-check">✓</span> No errors in the selected period. Nice.
54
+ </td>
55
+ </tr>
56
+ {% endif %}
57
+ </tbody>
58
+ </table>
59
+ </div>
60
+ </div>
61
+ {% endblock %}
dashboard/templates/history.html ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends "base.html" %}
2
+ {% set active = "history" %}
3
+
4
+ {% block title %} — history{% endblock %}
5
+
6
+ {% block content %}
7
+ <h1 class="page-title">Download History</h1>
8
+
9
+ <!-- Filter bar -->
10
+ <form class="filter-bar" method="get" action="/history">
11
+ <input
12
+ type="text"
13
+ name="user_filter"
14
+ class="filter-input"
15
+ placeholder="Search user..."
16
+ value="{{ user_filter or '' }}"
17
+ />
18
+ <select name="platform" class="filter-select">
19
+ <option value="">All platforms</option>
20
+ {% for p in ['youtube', 'instagram', 'twitter', 'tiktok', 'reddit', 'twitch', 'other'] %}
21
+ <option value="{{ p }}"{% if platform == p %} selected{% endif %}>{{ p }}</option>
22
+ {% endfor %}
23
+ </select>
24
+ <select name="status" class="filter-select">
25
+ <option value="">All statuses</option>
26
+ <option value="done"{% if status == 'done' %} selected{% endif %}>Done</option>
27
+ <option value="error"{% if status == 'error' %} selected{% endif %}>Error</option>
28
+ <option value="started"{% if status == 'started' %} selected{% endif %}>Started</option>
29
+ </select>
30
+ <input type="date" name="date_from" class="filter-input" value="{{ date_from or '' }}" />
31
+ <input type="date" name="date_to" class="filter-input" value="{{ date_to or '' }}" />
32
+ <button type="submit" class="btn btn-primary">Filter</button>
33
+ <a href="/history" class="btn btn-secondary">Clear</a>
34
+ </form>
35
+
36
+ <!-- Results info -->
37
+ {% if data %}
38
+ <div class="results-info">
39
+ Showing {{ data.rows|length }} of {{ data.total }} downloads &mdash; page {{ data.page }} of {{ data.pages }}
40
+ </div>
41
+ {% endif %}
42
+
43
+ <!-- Table -->
44
+ <div class="card table-card">
45
+ <div class="table-scroll">
46
+ <table class="data-table">
47
+ <thead>
48
+ <tr>
49
+ <th>Timestamp</th>
50
+ <th>User</th>
51
+ <th>URL</th>
52
+ <th>Platform</th>
53
+ <th>Format</th>
54
+ <th>Quality</th>
55
+ <th>Size (MB)</th>
56
+ <th>Status</th>
57
+ <th>Duration</th>
58
+ </tr>
59
+ </thead>
60
+ <tbody>
61
+ {% if data and data.rows %}
62
+ {% for row in data.rows %}
63
+ <tr>
64
+ <td class="mono text-secondary">{{ row.started_at[:19] if row.started_at else '—' }}</td>
65
+ <td>{{ row.username or row.user_id or '—' }}</td>
66
+ <td>
67
+ {% if row.url %}
68
+ <a href="{{ row.url }}" target="_blank" rel="noopener" class="url-cell" title="{{ row.url }}">
69
+ {{ row.url[:50] }}{% if row.url|length > 50 %}…{% endif %}
70
+ </a>
71
+ {% else %}—{% endif %}
72
+ </td>
73
+ <td>
74
+ {% if row.platform %}
75
+ <span class="badge badge-platform">{{ row.platform }}</span>
76
+ {% else %}—{% endif %}
77
+ </td>
78
+ <td class="mono">{{ row.format or '—' }}</td>
79
+ <td class="mono">{{ row.quality or '—' }}</td>
80
+ <td class="mono">
81
+ {% if row.file_size_bytes %}
82
+ {{ "%.1f"|format(row.file_size_bytes / 1048576) }}
83
+ {% else %}—{% endif %}
84
+ </td>
85
+ <td>
86
+ {% if row.status == 'done' %}
87
+ <span class="badge badge-success">done</span>
88
+ {% elif row.status == 'error' %}
89
+ <span class="badge badge-danger">error</span>
90
+ {% elif row.status == 'started' %}
91
+ <span class="badge badge-warning">started</span>
92
+ {% else %}
93
+ <span class="badge">{{ row.status }}</span>
94
+ {% endif %}
95
+ </td>
96
+ <td class="mono">
97
+ {% if row.download_duration_sec %}
98
+ {{ "%.1f"|format(row.download_duration_sec) }}s
99
+ {% else %}—{% endif %}
100
+ </td>
101
+ </tr>
102
+ {% endfor %}
103
+ {% else %}
104
+ <tr>
105
+ <td colspan="9" class="empty-state">No downloads recorded yet.</td>
106
+ </tr>
107
+ {% endif %}
108
+ </tbody>
109
+ </table>
110
+ </div>
111
+ </div>
112
+
113
+ <!-- Pagination -->
114
+ {% if data and data.pages > 1 %}
115
+ <div class="pagination">
116
+ {% if data.page > 1 %}
117
+ <a href="?page={{ data.page - 1 }}{% if user_filter %}&user_filter={{ user_filter }}{% endif %}{% if platform %}&platform={{ platform }}{% endif %}{% if status %}&status={{ status }}{% endif %}" class="btn btn-secondary">← Prev</a>
118
+ {% endif %}
119
+
120
+ {% for p in range(1, data.pages + 1) %}
121
+ {% if p == data.page %}
122
+ <span class="page-num active">{{ p }}</span>
123
+ {% elif p == 1 or p == data.pages or (p >= data.page - 2 and p <= data.page + 2) %}
124
+ <a href="?page={{ p }}{% if user_filter %}&user_filter={{ user_filter }}{% endif %}{% if platform %}&platform={{ platform }}{% endif %}{% if status %}&status={{ status }}{% endif %}" class="page-num">{{ p }}</a>
125
+ {% elif p == data.page - 3 or p == data.page + 3 %}
126
+ <span class="page-ellipsis">…</span>
127
+ {% endif %}
128
+ {% endfor %}
129
+
130
+ {% if data.page < data.pages %}
131
+ <a href="?page={{ data.page + 1 }}{% if user_filter %}&user_filter={{ user_filter }}{% endif %}{% if platform %}&platform={{ platform }}{% endif %}{% if status %}&status={{ status }}{% endif %}" class="btn btn-secondary">Next →</a>
132
+ {% endif %}
133
+ </div>
134
+ {% endif %}
135
+ {% endblock %}
dashboard/templates/login.html ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>reclip admin — login</title>
7
+ <style>
8
+ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
9
+
10
+ body {
11
+ background: #0f0f1a;
12
+ color: #e2e2f0;
13
+ font-family: Inter, system-ui, -apple-system, sans-serif;
14
+ min-height: 100vh;
15
+ display: flex;
16
+ align-items: center;
17
+ justify-content: center;
18
+ }
19
+
20
+ .card {
21
+ background: #1a1a2e;
22
+ border: 1px solid #2a2a40;
23
+ border-radius: 12px;
24
+ padding: 2.5rem 2rem;
25
+ width: 100%;
26
+ max-width: 380px;
27
+ }
28
+
29
+ .card h1 {
30
+ font-size: 1.75rem;
31
+ font-weight: 700;
32
+ letter-spacing: -0.02em;
33
+ color: #ffffff;
34
+ margin-bottom: 0.25rem;
35
+ }
36
+
37
+ .card .subtitle {
38
+ font-size: 0.85rem;
39
+ color: #8888aa;
40
+ margin-bottom: 2rem;
41
+ }
42
+
43
+ .form-group {
44
+ margin-bottom: 1.25rem;
45
+ }
46
+
47
+ label {
48
+ display: block;
49
+ font-size: 0.8rem;
50
+ font-weight: 500;
51
+ color: #9999bb;
52
+ margin-bottom: 0.4rem;
53
+ text-transform: uppercase;
54
+ letter-spacing: 0.05em;
55
+ }
56
+
57
+ input[type="text"],
58
+ input[type="password"] {
59
+ width: 100%;
60
+ padding: 0.65rem 0.85rem;
61
+ background: #0f0f1a;
62
+ border: 1px solid #2a2a40;
63
+ border-radius: 6px;
64
+ color: #e2e2f0;
65
+ font-size: 0.95rem;
66
+ font-family: inherit;
67
+ outline: none;
68
+ transition: border-color 0.15s;
69
+ }
70
+
71
+ input[type="text"]:focus,
72
+ input[type="password"]:focus {
73
+ border-color: #ff6b35;
74
+ }
75
+
76
+ .error {
77
+ background: rgba(239, 68, 68, 0.12);
78
+ border: 1px solid rgba(239, 68, 68, 0.35);
79
+ color: #ef4444;
80
+ border-radius: 6px;
81
+ padding: 0.65rem 0.85rem;
82
+ font-size: 0.875rem;
83
+ margin-bottom: 1.25rem;
84
+ }
85
+
86
+ button[type="submit"] {
87
+ width: 100%;
88
+ padding: 0.7rem;
89
+ background: #ff6b35;
90
+ color: #ffffff;
91
+ border: none;
92
+ border-radius: 6px;
93
+ font-size: 1rem;
94
+ font-weight: 600;
95
+ font-family: inherit;
96
+ cursor: pointer;
97
+ transition: background 0.15s, transform 0.1s;
98
+ margin-top: 0.5rem;
99
+ }
100
+
101
+ button[type="submit"]:hover {
102
+ background: #ff7f4f;
103
+ }
104
+
105
+ button[type="submit"]:active {
106
+ transform: scale(0.98);
107
+ }
108
+ </style>
109
+ </head>
110
+ <body>
111
+ <div class="card">
112
+ <h1>reclip admin</h1>
113
+ <p class="subtitle">Self-hosted Telegram media download bot</p>
114
+
115
+ {% if error %}
116
+ <div class="error" role="alert">{{ error }}</div>
117
+ {% endif %}
118
+
119
+ <form method="post" action="/login" autocomplete="on">
120
+ <div class="form-group">
121
+ <label for="username">Username</label>
122
+ <input
123
+ type="text"
124
+ id="username"
125
+ name="username"
126
+ autocomplete="username"
127
+ required
128
+ autofocus
129
+ />
130
+ </div>
131
+ <div class="form-group">
132
+ <label for="password">Password</label>
133
+ <input
134
+ type="password"
135
+ id="password"
136
+ name="password"
137
+ autocomplete="current-password"
138
+ required
139
+ />
140
+ </div>
141
+ <button type="submit">Sign in</button>
142
+ </form>
143
+ </div>
144
+ </body>
145
+ </html>
dashboard/tests/__init__.py ADDED
File without changes
dashboard/tests/test_api.py ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for the admin dashboard API routes."""
2
+ import json as _json
3
+ import os
4
+ import tempfile
5
+ import pytest
6
+
7
+ # Set env vars BEFORE any dashboard imports so db.py picks them up
8
+ _tmpdir = tempfile.mkdtemp()
9
+ os.environ.setdefault("ADMIN_PASSWORD", "testpass123")
10
+ os.environ.setdefault("SECRET_KEY", "test-secret")
11
+ os.environ["DB_PATH"] = os.path.join(_tmpdir, "test.db")
12
+ os.environ["DOWNLOADS_PATH"] = _tmpdir
13
+
14
+ from fastapi.testclient import TestClient
15
+ from main import create_app
16
+
17
+ app = create_app()
18
+ client = TestClient(app, raise_server_exceptions=True)
19
+
20
+
21
+ def _delete(path: str, body: dict, cookies: dict | None = None):
22
+ """Helper: send DELETE with JSON body using client.request()."""
23
+ kwargs = {
24
+ "content": _json.dumps(body).encode(),
25
+ "headers": {"content-type": "application/json"},
26
+ }
27
+ if cookies:
28
+ kwargs["cookies"] = cookies
29
+ return client.request("DELETE", path, **kwargs)
30
+
31
+
32
+ # ---------------------------------------------------------------------------
33
+ # Helpers
34
+ # ---------------------------------------------------------------------------
35
+
36
+ def _login() -> dict:
37
+ """POST /login with correct creds and return the response cookies."""
38
+ resp = client.post(
39
+ "/login",
40
+ data={"username": "admin", "password": "testpass123"},
41
+ follow_redirects=False,
42
+ )
43
+ assert resp.status_code == 303, f"Login failed: {resp.status_code} {resp.text}"
44
+ return resp.cookies
45
+
46
+
47
+ # ---------------------------------------------------------------------------
48
+ # Event ingestion (no auth)
49
+ # ---------------------------------------------------------------------------
50
+
51
+ def test_event_download_start():
52
+ resp = client.post("/api/events", json={
53
+ "type": "download_start",
54
+ "data": {
55
+ "job_id": "job-start-1",
56
+ "user_id": 42,
57
+ "username": "alice",
58
+ "chat_id": 99,
59
+ "url": "https://example.com/video.mp4",
60
+ "platform": "youtube",
61
+ },
62
+ })
63
+ assert resp.status_code == 204
64
+
65
+
66
+ def test_event_download_progress():
67
+ # Start one first
68
+ client.post("/api/events", json={
69
+ "type": "download_start",
70
+ "data": {
71
+ "job_id": "job-progress-1",
72
+ "user_id": 1,
73
+ "username": "bob",
74
+ "chat_id": 1,
75
+ "url": "https://example.com/v.mp4",
76
+ },
77
+ })
78
+ resp = client.post("/api/events", json={
79
+ "type": "download_progress",
80
+ "data": {
81
+ "job_id": "job-progress-1",
82
+ "progress": 50.0,
83
+ "speed": "1 MB/s",
84
+ "eta": "5s",
85
+ },
86
+ })
87
+ assert resp.status_code == 204
88
+
89
+
90
+ def test_event_download_done():
91
+ client.post("/api/events", json={
92
+ "type": "download_start",
93
+ "data": {
94
+ "job_id": "job-done-1",
95
+ "user_id": 2,
96
+ "username": "carol",
97
+ "chat_id": 2,
98
+ "url": "https://example.com/done.mp4",
99
+ "platform": "tiktok",
100
+ },
101
+ })
102
+ resp = client.post("/api/events", json={
103
+ "type": "download_done",
104
+ "data": {
105
+ "job_id": "job-done-1",
106
+ "title": "My Video",
107
+ "format": "mp4",
108
+ "quality": "1080p",
109
+ "file_size_bytes": 1024000,
110
+ "download_duration_sec": 3.5,
111
+ },
112
+ })
113
+ assert resp.status_code == 204
114
+
115
+
116
+ def test_event_download_error():
117
+ client.post("/api/events", json={
118
+ "type": "download_start",
119
+ "data": {
120
+ "job_id": "job-err-1",
121
+ "user_id": 3,
122
+ "username": "dave",
123
+ "chat_id": 3,
124
+ "url": "https://example.com/err.mp4",
125
+ },
126
+ })
127
+ resp = client.post("/api/events", json={
128
+ "type": "download_error",
129
+ "data": {
130
+ "job_id": "job-err-1",
131
+ "error_message": "HTTP 403 forbidden",
132
+ },
133
+ })
134
+ assert resp.status_code == 204
135
+
136
+
137
+ # ---------------------------------------------------------------------------
138
+ # Auth: dashboard-stats
139
+ # ---------------------------------------------------------------------------
140
+
141
+ def test_dashboard_stats_without_auth_returns_401():
142
+ resp = client.get("/api/dashboard-stats")
143
+ assert resp.status_code == 401
144
+
145
+
146
+ def test_dashboard_stats_with_auth_returns_200():
147
+ cookies = _login()
148
+ resp = client.get("/api/dashboard-stats", cookies=cookies)
149
+ assert resp.status_code == 200
150
+ body = resp.json()
151
+ assert "stats" in body
152
+ stats = body["stats"]
153
+ assert "downloads_today" in stats
154
+ assert "active_users_24h" in stats
155
+ assert "error_rate" in stats
156
+
157
+
158
+ # ---------------------------------------------------------------------------
159
+ # Chart data
160
+ # ---------------------------------------------------------------------------
161
+
162
+ def test_chart_data_without_auth_returns_401():
163
+ fresh = TestClient(app, raise_server_exceptions=True)
164
+ resp = fresh.get("/api/chart-data?range=1D")
165
+ assert resp.status_code == 401
166
+
167
+
168
+ def test_chart_data_with_auth():
169
+ cookies = _login()
170
+ for range_key in ("1D", "7D", "1M", "1Y"):
171
+ resp = client.get(f"/api/chart-data?range={range_key}", cookies=cookies)
172
+ assert resp.status_code == 200, f"Failed for range={range_key}"
173
+ body = resp.json()
174
+ assert "labels" in body
175
+ assert "values" in body
176
+
177
+
178
+ # ---------------------------------------------------------------------------
179
+ # Active downloads
180
+ # ---------------------------------------------------------------------------
181
+
182
+ def test_active_downloads_without_auth_returns_401():
183
+ fresh = TestClient(app, raise_server_exceptions=True)
184
+ resp = fresh.get("/api/active-downloads")
185
+ assert resp.status_code == 401
186
+
187
+
188
+ def test_active_downloads_with_auth():
189
+ # Start a job to make sure at least one is active
190
+ client.post("/api/events", json={
191
+ "type": "download_start",
192
+ "data": {
193
+ "job_id": "job-active-1",
194
+ "user_id": 10,
195
+ "username": "eve",
196
+ "chat_id": 10,
197
+ "url": "https://example.com/active.mp4",
198
+ "platform": "instagram",
199
+ },
200
+ })
201
+ cookies = _login()
202
+ resp = client.get("/api/active-downloads", cookies=cookies)
203
+ assert resp.status_code == 200
204
+ body = resp.json()
205
+ assert isinstance(body, list)
206
+ job_ids = [d["job_id"] for d in body]
207
+ assert "job-active-1" in job_ids
208
+
209
+
210
+ # ---------------------------------------------------------------------------
211
+ # Task 5: Delete files
212
+ # ---------------------------------------------------------------------------
213
+
214
+ def test_delete_files():
215
+ """Create a temp file, delete it via API, verify it's gone."""
216
+ import pathlib
217
+
218
+ downloads_path = pathlib.Path(_tmpdir)
219
+ test_file = downloads_path / "test_delete_me.mp4"
220
+ test_file.write_bytes(b"fake video content")
221
+ assert test_file.exists()
222
+
223
+ cookies = _login()
224
+ resp = _delete("/api/files", {"paths": ["test_delete_me.mp4"]}, cookies=cookies)
225
+ assert resp.status_code == 200
226
+ body = resp.json()
227
+ assert "test_delete_me.mp4" in body["deleted"]
228
+ assert not test_file.exists()
229
+
230
+
231
+ def test_delete_files_without_auth_returns_401():
232
+ fresh = TestClient(app, raise_server_exceptions=True)
233
+ resp = fresh.request(
234
+ "DELETE",
235
+ "/api/files",
236
+ content=_json.dumps({"paths": ["something.mp4"]}).encode(),
237
+ headers={"content-type": "application/json"},
238
+ )
239
+ assert resp.status_code == 401
240
+
241
+
242
+ def test_purge_requires_confirm():
243
+ """Sending empty/missing confirm should return 400."""
244
+ cookies = _login()
245
+ resp = _delete("/api/files/all", {}, cookies=cookies)
246
+ assert resp.status_code == 400
247
+
248
+
249
+ def test_purge_with_confirm():
250
+ """Create a temp file, purge all, verify it's gone."""
251
+ import pathlib
252
+
253
+ downloads_path = pathlib.Path(_tmpdir)
254
+ test_file = downloads_path / "purge_me.mp4"
255
+ test_file.write_bytes(b"content to be purged")
256
+ assert test_file.exists()
257
+
258
+ cookies = _login()
259
+ resp = _delete("/api/files/all", {"confirm": "PURGE"}, cookies=cookies)
260
+ assert resp.status_code == 200
261
+ body = resp.json()
262
+ assert "deleted_count" in body
263
+ assert not test_file.exists()
dashboard/tests/test_db.py ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for dashboard/db.py"""
2
+ import asyncio
3
+ import os
4
+ import tempfile
5
+ import time
6
+
7
+ # Must set DB_PATH before importing db
8
+ _tmpdir = tempfile.mkdtemp()
9
+ os.environ["DB_PATH"] = os.path.join(_tmpdir, "test_reclip.db")
10
+
11
+ import db # noqa: E402
12
+
13
+
14
+ _loop = asyncio.new_event_loop()
15
+
16
+
17
+ def run(coro):
18
+ return _loop.run_until_complete(coro)
19
+
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Helpers
23
+ # ---------------------------------------------------------------------------
24
+
25
+ def _job(suffix=""):
26
+ return f"job-{suffix or int(time.time() * 1000)}"
27
+
28
+
29
+ async def _seed_download(job_id, platform="youtube", status="done", user_id=1,
30
+ username="alice", chat_id=100, url="https://example.com/v"):
31
+ await db.insert_download_start(
32
+ job_id=job_id, user_id=user_id, username=username,
33
+ chat_id=chat_id, url=url, platform=platform,
34
+ )
35
+ if status == "done":
36
+ await db.update_download_done(
37
+ job_id=job_id, title="Test Video", format="mp4",
38
+ quality="720p", file_size_bytes=1_000_000,
39
+ download_duration_sec=3.5,
40
+ )
41
+ elif status == "error":
42
+ await db.update_download_error(job_id=job_id, error_message="network timeout")
43
+
44
+
45
+ # ---------------------------------------------------------------------------
46
+ # Task 1 tests: CRUD
47
+ # ---------------------------------------------------------------------------
48
+
49
+ def test_insert_download_start():
50
+ job_id = _job("start1")
51
+ run(db.insert_download_start(
52
+ job_id=job_id, user_id=42, username="bob",
53
+ chat_id=999, url="https://youtube.com/watch?v=abc",
54
+ platform="youtube",
55
+ ))
56
+ row = run(db.get_download_by_job_id(job_id))
57
+ assert row is not None
58
+ assert row["job_id"] == job_id
59
+ assert row["status"] == "started"
60
+ assert row["user_id"] == 42
61
+ assert row["username"] == "bob"
62
+ assert row["platform"] == "youtube"
63
+ assert row["url"] == "https://youtube.com/watch?v=abc"
64
+
65
+
66
+ def test_update_download_done():
67
+ job_id = _job("done1")
68
+ run(db.insert_download_start(
69
+ job_id=job_id, user_id=1, username="alice",
70
+ chat_id=10, url="https://example.com",
71
+ platform="tiktok",
72
+ ))
73
+ run(db.update_download_done(
74
+ job_id=job_id, title="Cool Clip", format="mp4",
75
+ quality="1080p", file_size_bytes=5_000_000,
76
+ download_duration_sec=10.2,
77
+ ))
78
+ row = run(db.get_download_by_job_id(job_id))
79
+ assert row["status"] == "done"
80
+ assert row["title"] == "Cool Clip"
81
+ assert row["format"] == "mp4"
82
+ assert row["quality"] == "1080p"
83
+ assert row["file_size_bytes"] == 5_000_000
84
+ assert abs(row["download_duration_sec"] - 10.2) < 0.01
85
+ assert row["completed_at"] is not None
86
+
87
+
88
+ def test_update_download_error():
89
+ job_id = _job("err1")
90
+ run(db.insert_download_start(
91
+ job_id=job_id, user_id=7, username="charlie",
92
+ chat_id=77, url="https://example.com",
93
+ platform="instagram",
94
+ ))
95
+ run(db.update_download_error(job_id=job_id, error_message="403 Forbidden"))
96
+ row = run(db.get_download_by_job_id(job_id))
97
+ assert row["status"] == "error"
98
+ assert row["error_message"] == "403 Forbidden"
99
+ assert row["completed_at"] is not None
100
+
101
+
102
+ def test_get_download_by_job_id_missing():
103
+ row = run(db.get_download_by_job_id("nonexistent-job-id"))
104
+ assert row is None
105
+
106
+
107
+ # ---------------------------------------------------------------------------
108
+ # Task 2 tests: stats queries
109
+ # ---------------------------------------------------------------------------
110
+
111
+ def test_get_dashboard_stats_empty():
112
+ """Stats on a fresh DB (after inserts above, but we want structure check)."""
113
+ stats = run(db.get_dashboard_stats())
114
+ assert "downloads_today" in stats
115
+ assert "downloads_yesterday" in stats
116
+ assert "active_users_24h" in stats
117
+ assert "error_rate" in stats
118
+ assert "error_rate_yesterday" in stats
119
+ assert "errors_today" in stats
120
+ assert "total_today" in stats
121
+
122
+
123
+ def test_get_dashboard_stats_with_data():
124
+ job_done = _job("stats-done")
125
+ job_err = _job("stats-err")
126
+ run(_seed_download(job_done, platform="youtube", status="done"))
127
+ run(_seed_download(job_err, platform="youtube", status="error"))
128
+ stats = run(db.get_dashboard_stats())
129
+ # At least the downloads we inserted are counted
130
+ assert stats["downloads_today"] >= 1
131
+ assert stats["errors_today"] >= 1
132
+ assert stats["total_today"] >= 2
133
+
134
+
135
+ def test_get_chart_data_ranges():
136
+ for rng in ("1D", "7D", "1M", "1Y"):
137
+ data = run(db.get_chart_data(rng))
138
+ assert "labels" in data
139
+ assert "values" in data
140
+ assert "platforms" in data
141
+ assert "top_users" in data
142
+ assert isinstance(data["labels"], list)
143
+ assert isinstance(data["values"], list)
144
+ assert isinstance(data["platforms"], list)
145
+ assert isinstance(data["top_users"], list)
146
+ assert len(data["labels"]) == len(data["values"])
147
+
148
+
149
+ def test_get_chart_data_platform_top5():
150
+ data = run(db.get_chart_data("7D"))
151
+ assert len(data["platforms"]) <= 5
152
+ for p in data["platforms"]:
153
+ assert "platform" in p
154
+ assert "count" in p
155
+
156
+
157
+ def test_get_chart_data_top_users():
158
+ data = run(db.get_chart_data("7D"))
159
+ assert len(data["top_users"]) <= 5
160
+ for u in data["top_users"]:
161
+ assert "username" in u
162
+ assert "count" in u
163
+
164
+
165
+ def test_get_downloads_page_basic():
166
+ result = run(db.get_downloads_page(page=1, per_page=10))
167
+ assert "rows" in result
168
+ assert "total" in result
169
+ assert "pages" in result
170
+ assert "page" in result
171
+ assert result["page"] == 1
172
+ assert isinstance(result["rows"], list)
173
+
174
+
175
+ def test_get_downloads_page_pagination():
176
+ # Seed 5 more downloads
177
+ for i in range(5):
178
+ run(_seed_download(_job(f"page-{i}"), platform="youtube", status="done",
179
+ user_id=10 + i, username=f"user{i}"))
180
+ result_p1 = run(db.get_downloads_page(page=1, per_page=3))
181
+ assert len(result_p1["rows"]) == 3
182
+ assert result_p1["total"] >= 5
183
+
184
+ result_p2 = run(db.get_downloads_page(page=2, per_page=3))
185
+ assert len(result_p2["rows"]) >= 1
186
+ # No overlap between pages
187
+ ids_p1 = {r["id"] for r in result_p1["rows"]}
188
+ ids_p2 = {r["id"] for r in result_p2["rows"]}
189
+ assert ids_p1.isdisjoint(ids_p2)
190
+
191
+
192
+ def test_get_downloads_page_filters():
193
+ run(_seed_download(_job("filter-tw"), platform="twitter", status="done",
194
+ user_id=999, username="twitteruser"))
195
+ result = run(db.get_downloads_page(page=1, per_page=50, platform="twitter"))
196
+ assert all(r["platform"] == "twitter" for r in result["rows"])
197
+
198
+ result_err = run(db.get_downloads_page(page=1, per_page=50, status="error"))
199
+ assert all(r["status"] == "error" for r in result_err["rows"])
200
+
201
+ result_user = run(db.get_downloads_page(page=1, per_page=50, user="twitteruser"))
202
+ assert any(r["username"] == "twitteruser" for r in result_user["rows"])
203
+
204
+
205
+ def test_get_error_downloads():
206
+ errors = run(db.get_error_downloads())
207
+ assert isinstance(errors, list)
208
+ for e in errors:
209
+ assert e["status"] == "error"
210
+
211
+
212
+ def test_get_error_downloads_date_filter():
213
+ from datetime import datetime, timezone, timedelta
214
+ yesterday = (datetime.now(timezone.utc) - timedelta(days=1)).strftime("%Y-%m-%d")
215
+ tomorrow = (datetime.now(timezone.utc) + timedelta(days=1)).strftime("%Y-%m-%d")
216
+ errors = run(db.get_error_downloads(date_from=yesterday, date_to=tomorrow))
217
+ assert isinstance(errors, list)
218
+ # All errors we seeded today should appear
219
+ for e in errors:
220
+ assert e["status"] == "error"
221
+
222
+
223
+ def test_insert_and_get_disk_snapshot():
224
+ run(db.insert_disk_snapshot(total_bytes=1_000_000_000, file_count=42))
225
+ snap = run(db.get_latest_disk_snapshot())
226
+ assert snap is not None
227
+ assert snap["total_bytes"] == 1_000_000_000
228
+ assert snap["file_count"] == 42
229
+ assert snap["snapshot_at"] is not None
230
+
231
+
232
+ def test_get_latest_disk_snapshot_empty():
233
+ # Use a separate temp DB to test empty state
234
+ with tempfile.TemporaryDirectory() as d:
235
+ old_path = os.environ["DB_PATH"]
236
+ empty_path = os.path.join(d, "empty.db")
237
+ os.environ["DB_PATH"] = empty_path
238
+ # init_db will create schema on empty_path
239
+ run(db.init_db())
240
+ snap = run(db.get_latest_disk_snapshot())
241
+ assert snap is None
242
+ os.environ["DB_PATH"] = old_path
243
+
244
+
245
+ def test_disk_snapshot_latest_is_newest():
246
+ run(db.insert_disk_snapshot(total_bytes=100, file_count=1))
247
+ run(db.insert_disk_snapshot(total_bytes=200, file_count=2))
248
+ run(db.insert_disk_snapshot(total_bytes=300, file_count=3))
249
+ snap = run(db.get_latest_disk_snapshot())
250
+ assert snap["total_bytes"] == 300
251
+ assert snap["file_count"] == 3
docker-compose.yml ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ reclip:
3
+ image: ghcr.io/${GHCR_OWNER:-gth-ai}/reclip-telegram-bot/reclip:${IMAGE_TAG:-latest}
4
+ build: ./reclip
5
+ environment:
6
+ - HOST=0.0.0.0
7
+ - PORT=8899
8
+ - MAX_CONCURRENT_DOWNLOADS=${MAX_CONCURRENT_DOWNLOADS:-3}
9
+ - DOWNLOAD_TIMEOUT=${DOWNLOAD_TIMEOUT:-900}
10
+ - DOWNLOADS_PATH=/downloads
11
+ volumes:
12
+ - downloads:/downloads
13
+ networks:
14
+ - internal
15
+ restart: unless-stopped
16
+
17
+ bot:
18
+ image: ghcr.io/${GHCR_OWNER:-gth-ai}/reclip-telegram-bot/bot:${IMAGE_TAG:-latest}
19
+ build: ./bot
20
+ environment:
21
+ - BOT_TOKEN=${BOT_TOKEN}
22
+ - RECLIP_URL=http://reclip:8899
23
+ - TELEGRAM_BOT_API_URL=http://telegram-bot-api:8081
24
+ - DOWNLOADS_PATH=/downloads
25
+ - CLEANUP_MAX_AGE_HOURS=${CLEANUP_MAX_AGE_HOURS:-1}
26
+ - CLEANUP_MAX_DISK_MB=${CLEANUP_MAX_DISK_MB:-5000}
27
+ - CLEANUP_INTERVAL_SECONDS=${CLEANUP_INTERVAL_SECONDS:-300}
28
+ - DASHBOARD_URL=http://dashboard:8080
29
+ volumes:
30
+ - downloads:/downloads
31
+ depends_on:
32
+ - reclip
33
+ - telegram-bot-api
34
+ networks:
35
+ - internal
36
+ restart: unless-stopped
37
+
38
+ telegram-bot-api:
39
+ image: aiogram/telegram-bot-api:latest
40
+ environment:
41
+ - TELEGRAM_API_ID=${TELEGRAM_API_ID}
42
+ - TELEGRAM_API_HASH=${TELEGRAM_API_HASH}
43
+ - TELEGRAM_LOCAL=true
44
+ volumes:
45
+ - telegram-bot-api-data:/var/lib/telegram-bot-api
46
+ networks:
47
+ - internal
48
+ restart: unless-stopped
49
+
50
+ dashboard:
51
+ image: ghcr.io/${GHCR_OWNER:-gth-ai}/reclip-telegram-bot/dashboard:${IMAGE_TAG:-latest}
52
+ build: ./dashboard
53
+ environment:
54
+ - ADMIN_USER=${DASHBOARD_USER:-admin}
55
+ - ADMIN_PASSWORD=${DASHBOARD_PASSWORD}
56
+ - DB_PATH=/data/reclip_bot.db
57
+ - DOWNLOADS_PATH=/downloads
58
+ - RECLIP_URL=http://reclip:8899
59
+ - SECRET_KEY=${DASHBOARD_SECRET_KEY:-change-me-in-production}
60
+ volumes:
61
+ - dashboard-data:/data
62
+ - downloads:/downloads:ro
63
+ ports:
64
+ - "${DASHBOARD_PORT:-8080}:8080"
65
+ depends_on:
66
+ - reclip
67
+ networks:
68
+ - internal
69
+ restart: unless-stopped
70
+
71
+ # Optional: expose reclip web UI on host
72
+ # Uncomment to access web UI at http://localhost:8899
73
+ # reclip-web:
74
+ # extends:
75
+ # service: reclip
76
+ # ports:
77
+ # - "${RECLIP_WEB_PORT:-8899}:8899"
78
+ # networks:
79
+ # - internal
80
+
81
+ volumes:
82
+ downloads:
83
+ telegram-bot-api-data:
84
+ dashboard-data:
85
+
86
+ networks:
87
+ internal:
88
+ driver: bridge
reclip/Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ RUN apt-get update && \
4
+ apt-get install -y --no-install-recommends ffmpeg && \
5
+ rm -rf /var/lib/apt/lists/*
6
+
7
+ WORKDIR /app
8
+
9
+ RUN pip install --no-cache-dir flask yt-dlp
10
+
11
+ COPY . .
12
+
13
+ EXPOSE 8899
14
+ ENV HOST=0.0.0.0
15
+ ENV DOWNLOADS_PATH=/downloads
16
+ CMD ["python", "app.py"]
reclip/app.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import glob
4
+ import json
5
+ import subprocess
6
+ import threading
7
+ from flask import Flask, request, jsonify, send_file, render_template
8
+
9
+ app = Flask(__name__)
10
+ DOWNLOAD_DIR = os.environ.get("DOWNLOADS_PATH", os.path.join(os.path.dirname(__file__), "downloads"))
11
+ os.makedirs(DOWNLOAD_DIR, exist_ok=True)
12
+
13
+ MAX_CONCURRENT_DOWNLOADS = int(os.environ.get("MAX_CONCURRENT_DOWNLOADS", 3))
14
+ DOWNLOAD_TIMEOUT = int(os.environ.get("DOWNLOAD_TIMEOUT", 900))
15
+ download_semaphore = threading.Semaphore(MAX_CONCURRENT_DOWNLOADS)
16
+
17
+ jobs = {}
18
+
19
+ PROGRESS_TEMPLATE = (
20
+ 'download:{"downloaded_bytes":%(progress.downloaded_bytes)s,'
21
+ '"total_bytes":%(progress.total_bytes)s,'
22
+ '"speed":%(progress.speed)s,'
23
+ '"eta":%(progress.eta)s}'
24
+ )
25
+
26
+
27
+ def run_download(job_id, url, format_choice, format_id):
28
+ job = jobs[job_id]
29
+
30
+ if not download_semaphore.acquire(timeout=30):
31
+ job["status"] = "error"
32
+ job["error"] = "Too many concurrent downloads, please try again later"
33
+ return
34
+
35
+ try:
36
+ _do_download(job_id, url, format_choice, format_id)
37
+ finally:
38
+ download_semaphore.release()
39
+
40
+
41
+ def _do_download(job_id, url, format_choice, format_id):
42
+ job = jobs[job_id]
43
+ out_template = os.path.join(DOWNLOAD_DIR, f"{job_id}.%(ext)s")
44
+
45
+ cmd = [
46
+ "yt-dlp", "--no-playlist", "-o", out_template,
47
+ "--progress-template", PROGRESS_TEMPLATE,
48
+ "--force-ipv4",
49
+ "--downloader", "hls:ffmpeg",
50
+ "--concurrent-fragments", "4",
51
+ "--socket-timeout", "20",
52
+ "--retries", "5",
53
+ "--fragment-retries", "10",
54
+ "--throttled-rate", "50K",
55
+ ]
56
+
57
+ if format_choice == "audio":
58
+ cmd += ["-x", "--audio-format", "mp3"]
59
+ elif format_id:
60
+ cmd += ["-f", f"{format_id}+bestaudio/best", "--merge-output-format", "mp4"]
61
+ else:
62
+ cmd += ["-f", "bv*[vcodec~='^(avc|h264)']+ba/b[vcodec~='^(avc|h264)']/bv*+ba/b", "--merge-output-format", "mp4"]
63
+
64
+ cmd.append(url)
65
+
66
+ process = subprocess.Popen(
67
+ cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
68
+ )
69
+
70
+ timed_out = threading.Event()
71
+
72
+ def _kill_on_timeout():
73
+ timed_out.set()
74
+ try:
75
+ process.kill()
76
+ except Exception:
77
+ pass
78
+
79
+ deadline_timer = threading.Timer(DOWNLOAD_TIMEOUT, _kill_on_timeout)
80
+ deadline_timer.daemon = True
81
+ deadline_timer.start()
82
+
83
+ stderr_lines = []
84
+
85
+ def _read_stderr():
86
+ for line in process.stderr:
87
+ line = line.rstrip("\n")
88
+ stderr_lines.append(line)
89
+ try:
90
+ json_str = line.removeprefix("download:")
91
+ progress_data = json.loads(json_str)
92
+ total = progress_data.get("total_bytes")
93
+ downloaded = progress_data.get("downloaded_bytes")
94
+ percent = None
95
+ if total and downloaded and total > 0:
96
+ percent = round(downloaded / total * 100, 1)
97
+ job["progress"] = {
98
+ "percent": percent,
99
+ "downloaded_bytes": downloaded,
100
+ "total_bytes": total,
101
+ "speed": progress_data.get("speed"),
102
+ "eta": progress_data.get("eta"),
103
+ }
104
+ except (json.JSONDecodeError, ValueError):
105
+ pass
106
+
107
+ stderr_reader = threading.Thread(target=_read_stderr, daemon=True)
108
+ stderr_reader.start()
109
+
110
+ try:
111
+ returncode = process.wait()
112
+ finally:
113
+ deadline_timer.cancel()
114
+ stderr_reader.join(timeout=5)
115
+
116
+ if timed_out.is_set():
117
+ job["status"] = "error"
118
+ job["error"] = f"Download timed out ({DOWNLOAD_TIMEOUT // 60} min limit)"
119
+ return
120
+
121
+ try:
122
+ if returncode != 0:
123
+ job["status"] = "error"
124
+ last_line = stderr_lines[-1] if stderr_lines else "Unknown error"
125
+ job["error"] = last_line
126
+ return
127
+
128
+ files = glob.glob(os.path.join(DOWNLOAD_DIR, f"{job_id}.*"))
129
+ if not files:
130
+ job["status"] = "error"
131
+ job["error"] = "Download completed but no file was found"
132
+ return
133
+
134
+ if format_choice == "audio":
135
+ target = [f for f in files if f.endswith(".mp3")]
136
+ chosen = target[0] if target else files[0]
137
+ else:
138
+ target = [f for f in files if f.endswith(".mp4")]
139
+ chosen = target[0] if target else files[0]
140
+
141
+ for f in files:
142
+ if f != chosen:
143
+ try:
144
+ os.remove(f)
145
+ except OSError:
146
+ pass
147
+
148
+ if chosen.endswith(".mp4"):
149
+ try:
150
+ codec_probe = subprocess.run(
151
+ ["ffprobe", "-v", "error", "-select_streams", "v:0",
152
+ "-show_entries", "stream=codec_name",
153
+ "-of", "default=noprint_wrappers=1:nokey=1", chosen],
154
+ capture_output=True, text=True, timeout=10,
155
+ )
156
+ vcodec = (codec_probe.stdout or "").strip().lower()
157
+ except Exception:
158
+ vcodec = ""
159
+
160
+ if vcodec in ("av1", "vp9", "vp8"):
161
+ transcoded = chosen + ".h264.mp4"
162
+ try:
163
+ r = subprocess.run(
164
+ ["ffmpeg", "-y", "-i", chosen,
165
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
166
+ "-c:a", "aac", "-b:a", "128k",
167
+ "-movflags", "+faststart",
168
+ transcoded],
169
+ capture_output=True, timeout=600,
170
+ )
171
+ if r.returncode == 0 and os.path.exists(transcoded) and os.path.getsize(transcoded) > 0:
172
+ os.replace(transcoded, chosen)
173
+ elif os.path.exists(transcoded):
174
+ os.remove(transcoded)
175
+ except (subprocess.TimeoutExpired, OSError):
176
+ if os.path.exists(transcoded):
177
+ try: os.remove(transcoded)
178
+ except OSError: pass
179
+ else:
180
+ faststart_tmp = chosen + ".fs.mp4"
181
+ try:
182
+ subprocess.run(
183
+ ["ffmpeg", "-y", "-i", chosen, "-c", "copy",
184
+ "-movflags", "+faststart", faststart_tmp],
185
+ capture_output=True, timeout=120,
186
+ )
187
+ if os.path.exists(faststart_tmp) and os.path.getsize(faststart_tmp) > 0:
188
+ os.replace(faststart_tmp, chosen)
189
+ elif os.path.exists(faststart_tmp):
190
+ os.remove(faststart_tmp)
191
+ except (subprocess.TimeoutExpired, OSError):
192
+ if os.path.exists(faststart_tmp):
193
+ try: os.remove(faststart_tmp)
194
+ except OSError: pass
195
+
196
+ if chosen.endswith(".mp4"):
197
+ try:
198
+ probe = subprocess.run(
199
+ ["ffprobe", "-v", "error", "-select_streams", "v:0",
200
+ "-show_entries", "stream=width,height:format=duration",
201
+ "-of", "json", chosen],
202
+ capture_output=True, text=True, timeout=10,
203
+ )
204
+ info = json.loads(probe.stdout)
205
+ stream = (info.get("streams") or [{}])[0]
206
+ fmt = info.get("format") or {}
207
+ job["width"] = stream.get("width")
208
+ job["height"] = stream.get("height")
209
+ dur = fmt.get("duration")
210
+ job["duration"] = float(dur) if dur else None
211
+ except Exception:
212
+ pass
213
+
214
+ job["status"] = "done"
215
+ job["file"] = chosen
216
+ job["file_path"] = os.path.abspath(chosen)
217
+ ext = os.path.splitext(chosen)[1]
218
+ title = job.get("title", "").strip()
219
+ # Sanitize title for filename
220
+ if title:
221
+ safe_title = "".join(c for c in title if c not in r'\/:*?"<>|').strip()[:20].strip()
222
+ job["filename"] = f"{safe_title}{ext}" if safe_title else os.path.basename(chosen)
223
+ else:
224
+ job["filename"] = os.path.basename(chosen)
225
+ except Exception as e:
226
+ job["status"] = "error"
227
+ job["error"] = str(e)
228
+
229
+
230
+ @app.route("/")
231
+ def index():
232
+ return render_template("index.html")
233
+
234
+
235
+ @app.route("/api/info", methods=["POST"])
236
+ def get_info():
237
+ data = request.json
238
+ url = data.get("url", "").strip()
239
+ if not url:
240
+ return jsonify({"error": "No URL provided"}), 400
241
+
242
+ cmd = ["yt-dlp", "--no-playlist", "-j", url]
243
+ try:
244
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
245
+ if result.returncode != 0:
246
+ return jsonify({"error": result.stderr.strip().split("\n")[-1]}), 400
247
+
248
+ info = json.loads(result.stdout)
249
+
250
+ # Build quality options — keep best format per resolution
251
+ best_by_height = {}
252
+ for f in info.get("formats", []):
253
+ height = f.get("height")
254
+ if height and f.get("vcodec", "none") != "none":
255
+ tbr = f.get("tbr") or 0
256
+ if height not in best_by_height or tbr > (best_by_height[height].get("tbr") or 0):
257
+ best_by_height[height] = f
258
+
259
+ formats = []
260
+ for height, f in best_by_height.items():
261
+ formats.append({
262
+ "id": f["format_id"],
263
+ "label": f"{height}p",
264
+ "height": height,
265
+ })
266
+ formats.sort(key=lambda x: x["height"], reverse=True)
267
+
268
+ return jsonify({
269
+ "title": info.get("title", ""),
270
+ "thumbnail": info.get("thumbnail", ""),
271
+ "duration": info.get("duration"),
272
+ "uploader": info.get("uploader", ""),
273
+ "extractor": info.get("extractor", ""),
274
+ "formats": formats,
275
+ })
276
+ except subprocess.TimeoutExpired:
277
+ return jsonify({"error": "Timed out fetching video info"}), 400
278
+ except Exception as e:
279
+ return jsonify({"error": str(e)}), 400
280
+
281
+
282
+ @app.route("/api/download", methods=["POST"])
283
+ def start_download():
284
+ data = request.json
285
+ url = data.get("url", "").strip()
286
+ format_choice = data.get("format", "video")
287
+ format_id = data.get("format_id")
288
+ title = data.get("title", "")
289
+
290
+ if not url:
291
+ return jsonify({"error": "No URL provided"}), 400
292
+
293
+ job_id = uuid.uuid4().hex[:10]
294
+ jobs[job_id] = {"status": "downloading", "url": url, "title": title}
295
+
296
+ thread = threading.Thread(target=run_download, args=(job_id, url, format_choice, format_id))
297
+ thread.daemon = True
298
+ thread.start()
299
+
300
+ return jsonify({"job_id": job_id})
301
+
302
+
303
+ @app.route("/api/status/<job_id>")
304
+ def check_status(job_id):
305
+ job = jobs.get(job_id)
306
+ if not job:
307
+ return jsonify({"error": "Job not found"}), 404
308
+ return jsonify({
309
+ "status": job["status"],
310
+ "error": job.get("error"),
311
+ "filename": job.get("filename"),
312
+ "progress": job.get("progress"),
313
+ "file_path": job.get("file_path"),
314
+ "width": job.get("width"),
315
+ "height": job.get("height"),
316
+ "duration": job.get("duration"),
317
+ })
318
+
319
+
320
+ @app.route("/api/file/<job_id>")
321
+ def download_file(job_id):
322
+ job = jobs.get(job_id)
323
+ if not job or job["status"] != "done":
324
+ return jsonify({"error": "File not ready"}), 404
325
+ return send_file(job["file"], as_attachment=True, download_name=job["filename"])
326
+
327
+
328
+ if __name__ == "__main__":
329
+ port = int(os.environ.get("PORT", 8899))
330
+ host = os.environ.get("HOST", "127.0.0.1")
331
+ app.run(host=host, port=port)
reclip/requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ flask
2
+ yt-dlp
reclip/static/favicon.svg ADDED
reclip/templates/index.html ADDED
@@ -0,0 +1,689 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>ReClip — Free Media Downloader</title>
7
+ <link rel="icon" href="/static/favicon.svg" type="image/svg+xml">
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=DM+Mono:wght@400;500&display=swap" rel="stylesheet">
10
+ <style>
11
+ :root {
12
+ --bg: #f4f1eb;
13
+ --fg: #3a3a38;
14
+ --accent: #e85d2a;
15
+ --accent-hover: #d04e1f;
16
+ --muted: #9c9889;
17
+ --card: #fff;
18
+ --card-border: #e2ded6;
19
+ --success: #2d8a4e;
20
+ --error: #c43d3d;
21
+ --radius: 14px;
22
+ --shimmer: linear-gradient(90deg, #ebe8e2 25%, #f0ede7 50%, #ebe8e2 75%);
23
+ }
24
+
25
+ * { margin: 0; padding: 0; box-sizing: border-box; }
26
+
27
+ body {
28
+ font-family: 'DM Mono', monospace;
29
+ background: var(--bg);
30
+ color: var(--fg);
31
+ min-height: 100vh;
32
+ overflow-x: hidden;
33
+ }
34
+
35
+ body::after {
36
+ content: '';
37
+ position: fixed;
38
+ inset: 0;
39
+ opacity: 0.03;
40
+ pointer-events: none;
41
+ background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
42
+ background-size: 128px;
43
+ }
44
+
45
+ .page {
46
+ max-width: 620px;
47
+ margin: 0 auto;
48
+ padding: 60px 24px 80px;
49
+ }
50
+
51
+ .brand { margin-bottom: 48px; }
52
+ .brand h1 {
53
+ font-family: 'Instrument Serif', serif;
54
+ font-size: 4.2rem;
55
+ font-weight: 400;
56
+ letter-spacing: -0.03em;
57
+ line-height: 1;
58
+ color: var(--fg);
59
+ }
60
+ .brand h1 em { font-style: italic; color: var(--accent); }
61
+ .brand p {
62
+ margin-top: 10px;
63
+ font-size: 0.78rem;
64
+ color: var(--muted);
65
+ letter-spacing: 0.04em;
66
+ text-transform: uppercase;
67
+ }
68
+
69
+ textarea {
70
+ width: 100%;
71
+ padding: 18px 20px;
72
+ border: 1.5px solid var(--card-border);
73
+ border-radius: var(--radius);
74
+ background: var(--card);
75
+ color: var(--fg);
76
+ font-family: 'DM Mono', monospace;
77
+ font-size: 0.88rem;
78
+ outline: none;
79
+ resize: none;
80
+ min-height: 56px;
81
+ transition: border-color 0.25s, box-shadow 0.25s;
82
+ margin-bottom: 6px;
83
+ }
84
+ textarea:focus {
85
+ border-color: var(--accent);
86
+ box-shadow: 0 0 0 3px rgba(232, 93, 42, 0.08);
87
+ }
88
+ textarea::placeholder { color: var(--muted); }
89
+
90
+ .hint {
91
+ font-size: 0.7rem;
92
+ color: var(--muted);
93
+ margin-bottom: 20px;
94
+ letter-spacing: 0.02em;
95
+ }
96
+
97
+ .controls {
98
+ display: flex;
99
+ gap: 10px;
100
+ margin-bottom: 6px;
101
+ align-items: center;
102
+ }
103
+ .pill-group {
104
+ display: flex;
105
+ border: 1.5px solid var(--card-border);
106
+ border-radius: 10px;
107
+ overflow: hidden;
108
+ background: var(--card);
109
+ }
110
+ .pill {
111
+ padding: 9px 18px;
112
+ font-family: 'DM Mono', monospace;
113
+ font-size: 0.76rem;
114
+ border: none;
115
+ background: transparent;
116
+ color: var(--muted);
117
+ cursor: pointer;
118
+ transition: all 0.2s;
119
+ letter-spacing: 0.03em;
120
+ text-transform: uppercase;
121
+ }
122
+ .pill.active { background: var(--fg); color: var(--bg); }
123
+ .pill:not(.active):hover { color: var(--fg); }
124
+
125
+ .fetch-btn {
126
+ flex: 1;
127
+ padding: 10px 20px;
128
+ border: none;
129
+ border-radius: 10px;
130
+ background: var(--accent);
131
+ color: #fff;
132
+ font-family: 'DM Mono', monospace;
133
+ font-size: 0.82rem;
134
+ font-weight: 500;
135
+ cursor: pointer;
136
+ transition: background 0.2s, transform 0.1s;
137
+ letter-spacing: 0.04em;
138
+ text-transform: uppercase;
139
+ }
140
+ .fetch-btn:hover { background: var(--accent-hover); }
141
+ .fetch-btn:active { transform: scale(0.98); }
142
+ .fetch-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
143
+
144
+ /* Cards */
145
+ .cards { margin-top: 24px; }
146
+
147
+ .card {
148
+ display: flex;
149
+ gap: 14px;
150
+ border: 1.5px solid var(--card-border);
151
+ border-radius: var(--radius);
152
+ background: var(--card);
153
+ padding: 14px;
154
+ margin-bottom: 10px;
155
+ animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1);
156
+ }
157
+ @keyframes slideUp {
158
+ from { opacity: 0; transform: translateY(12px); }
159
+ to { opacity: 1; transform: translateY(0); }
160
+ }
161
+
162
+ .card-thumb {
163
+ width: 120px;
164
+ min-width: 120px;
165
+ height: 80px;
166
+ border-radius: 8px;
167
+ overflow: hidden;
168
+ background: var(--card-border);
169
+ flex-shrink: 0;
170
+ position: relative;
171
+ }
172
+ .card-thumb img {
173
+ width: 100%;
174
+ height: 100%;
175
+ object-fit: cover;
176
+ display: block;
177
+ }
178
+ /* No-thumbnail icon */
179
+ .card-thumb .no-thumb {
180
+ width: 100%;
181
+ height: 100%;
182
+ display: flex;
183
+ align-items: center;
184
+ justify-content: center;
185
+ color: var(--muted);
186
+ }
187
+ .card-thumb .no-thumb svg {
188
+ width: 28px;
189
+ height: 28px;
190
+ opacity: 0.5;
191
+ }
192
+
193
+ /* Shimmer skeleton for loading state */
194
+ .card-thumb.loading {
195
+ background: var(--shimmer);
196
+ background-size: 200% 100%;
197
+ animation: shimmer 1.5s infinite;
198
+ }
199
+ .skeleton-line {
200
+ height: 12px;
201
+ border-radius: 4px;
202
+ background: var(--shimmer);
203
+ background-size: 200% 100%;
204
+ animation: shimmer 1.5s infinite;
205
+ margin-bottom: 6px;
206
+ }
207
+ .skeleton-line.short { width: 40%; height: 10px; }
208
+ .skeleton-line.medium { width: 75%; }
209
+ @keyframes shimmer {
210
+ 0% { background-position: 200% 0; }
211
+ 100% { background-position: -200% 0; }
212
+ }
213
+
214
+ .card-body {
215
+ flex: 1;
216
+ min-width: 0;
217
+ display: flex;
218
+ flex-direction: column;
219
+ justify-content: center;
220
+ }
221
+ .card-title {
222
+ font-family: 'Instrument Serif', serif;
223
+ font-size: 1rem;
224
+ line-height: 1.3;
225
+ color: var(--fg);
226
+ margin-bottom: 2px;
227
+ overflow: hidden;
228
+ text-overflow: ellipsis;
229
+ white-space: nowrap;
230
+ }
231
+ .card-meta {
232
+ font-size: 0.68rem;
233
+ color: var(--muted);
234
+ letter-spacing: 0.03em;
235
+ text-transform: uppercase;
236
+ margin-bottom: 8px;
237
+ }
238
+
239
+ .card-actions {
240
+ display: flex;
241
+ gap: 6px;
242
+ align-items: center;
243
+ flex-wrap: wrap;
244
+ }
245
+
246
+ .card-dl-btn {
247
+ padding: 6px 14px;
248
+ border: none;
249
+ border-radius: 7px;
250
+ background: var(--fg);
251
+ color: var(--bg);
252
+ font-family: 'DM Mono', monospace;
253
+ font-size: 0.7rem;
254
+ font-weight: 500;
255
+ cursor: pointer;
256
+ transition: all 0.2s;
257
+ letter-spacing: 0.03em;
258
+ text-transform: uppercase;
259
+ }
260
+ .card-dl-btn:hover { background: #2d2d2a; }
261
+ .card-dl-btn:disabled { opacity: 0.4; cursor: not-allowed; }
262
+ .card-dl-btn.done { background: var(--success); color: #fff; }
263
+ .card-dl-btn.error { background: var(--error); color: #fff; }
264
+
265
+ .card-status {
266
+ font-size: 0.68rem;
267
+ color: var(--muted);
268
+ }
269
+ .card-status.downloading { color: var(--accent); }
270
+ .card-status.done { color: var(--success); }
271
+ .card-status.error { color: var(--error); }
272
+
273
+ .q-chip {
274
+ padding: 4px 10px;
275
+ border: 1.5px solid var(--card-border);
276
+ border-radius: 6px;
277
+ background: transparent;
278
+ color: var(--muted);
279
+ font-family: 'DM Mono', monospace;
280
+ font-size: 0.65rem;
281
+ cursor: pointer;
282
+ transition: all 0.2s;
283
+ }
284
+ .q-chip.active {
285
+ border-color: var(--fg);
286
+ color: var(--fg);
287
+ background: rgba(26,26,24,0.04);
288
+ }
289
+ .q-chip:hover:not(.active) { border-color: var(--muted); }
290
+
291
+ /* Error card */
292
+ .card.card-error {
293
+ border-color: rgba(196, 61, 61, 0.2);
294
+ background: rgba(196, 61, 61, 0.03);
295
+ }
296
+ .card-error-icon {
297
+ width: 100%;
298
+ height: 100%;
299
+ display: flex;
300
+ align-items: center;
301
+ justify-content: center;
302
+ background: rgba(196, 61, 61, 0.08);
303
+ color: var(--error);
304
+ }
305
+ .card-error-icon svg {
306
+ width: 24px;
307
+ height: 24px;
308
+ }
309
+ .card-error-msg {
310
+ font-size: 0.72rem;
311
+ color: var(--error);
312
+ margin-top: 2px;
313
+ line-height: 1.4;
314
+ }
315
+ .card-error-url {
316
+ font-size: 0.65rem;
317
+ color: var(--muted);
318
+ word-break: break-all;
319
+ margin-top: 4px;
320
+ }
321
+
322
+ /* Download all bar */
323
+ .dl-all-bar {
324
+ display: flex;
325
+ gap: 10px;
326
+ margin-top: 16px;
327
+ }
328
+ .dl-all-btn {
329
+ flex: 1;
330
+ padding: 12px;
331
+ border: none;
332
+ border-radius: 10px;
333
+ background: var(--fg);
334
+ color: var(--bg);
335
+ font-family: 'DM Mono', monospace;
336
+ font-size: 0.82rem;
337
+ font-weight: 500;
338
+ cursor: pointer;
339
+ transition: all 0.2s;
340
+ letter-spacing: 0.04em;
341
+ text-transform: uppercase;
342
+ }
343
+ .dl-all-btn:hover { background: #2d2d2a; }
344
+ .dl-all-btn:disabled { opacity: 0.4; cursor: not-allowed; }
345
+
346
+ .spin {
347
+ display: inline-block;
348
+ width: 10px; height: 10px;
349
+ border: 2px solid currentColor;
350
+ border-right-color: transparent;
351
+ border-radius: 50%;
352
+ animation: spin 0.6s linear infinite;
353
+ vertical-align: middle;
354
+ }
355
+ @keyframes spin { to { transform: rotate(360deg); } }
356
+
357
+ .footer {
358
+ margin-top: 48px;
359
+ text-align: center;
360
+ font-size: 0.68rem;
361
+ color: var(--muted);
362
+ letter-spacing: 0.05em;
363
+ text-transform: uppercase;
364
+ line-height: 1.8;
365
+ }
366
+
367
+ /* Mobile */
368
+ @media (max-width: 500px) {
369
+ .brand h1 { font-size: 3rem; }
370
+ .page { padding: 40px 16px 60px; }
371
+ .card { flex-direction: column; gap: 10px; }
372
+ .card-thumb {
373
+ width: 100%;
374
+ min-width: unset;
375
+ height: 160px;
376
+ }
377
+ .card-title { white-space: normal; }
378
+ .controls { flex-wrap: wrap; }
379
+ .fetch-btn { width: 100%; }
380
+ }
381
+ </style>
382
+ </head>
383
+ <body>
384
+ <div class="page">
385
+ <div class="brand">
386
+ <h1>Re<em>Clip</em></h1>
387
+ <p>Free media downloader</p>
388
+ </div>
389
+
390
+ <textarea id="urls" placeholder="Paste one or more URLs..." rows="2"></textarea>
391
+ <p class="hint">Multiple links? Separate with spaces, commas, or newlines.</p>
392
+
393
+ <div class="controls">
394
+ <div class="pill-group">
395
+ <button class="pill active" data-format="video" onclick="setFormat(this)">MP4</button>
396
+ <button class="pill" data-format="audio" onclick="setFormat(this)">MP3</button>
397
+ </div>
398
+ <button class="fetch-btn" id="goBtn" onclick="go()">Fetch</button>
399
+ </div>
400
+
401
+ <div id="cards" class="cards"></div>
402
+
403
+ <div class="footer">
404
+ YouTube &middot; TikTok &middot; Instagram &middot; Twitter/X &middot; Reddit &middot; Facebook<br>
405
+ Vimeo &middot; Twitch &middot; Dailymotion &middot; SoundCloud &middot; Loom &middot; Streamable<br>
406
+ Pinterest &middot; Tumblr &middot; Threads &middot; LinkedIn &middot; 1000+ more
407
+ </div>
408
+ </div>
409
+
410
+ <script>
411
+ let currentFormat = 'video';
412
+ let cardData = [];
413
+
414
+ function setFormat(btn) {
415
+ document.querySelectorAll('.pill').forEach(b => b.classList.remove('active'));
416
+ btn.classList.add('active');
417
+ currentFormat = btn.dataset.format;
418
+ }
419
+
420
+ function parseUrls(text) {
421
+ return [...new Set(text.split(/[\s,]+/).map(u => u.trim()).filter(u => u.startsWith('http')))];
422
+ }
423
+
424
+ function fmtDur(s) {
425
+ if (!s) return '';
426
+ const m = Math.floor(s / 60);
427
+ const sec = Math.floor(s % 60);
428
+ return `${m}:${sec.toString().padStart(2, '0')}`;
429
+ }
430
+
431
+ function esc(s) {
432
+ const d = document.createElement('div');
433
+ d.textContent = s;
434
+ return d.innerHTML;
435
+ }
436
+
437
+ function friendlyError(err) {
438
+ if (err.includes('Unsupported URL')) return 'This URL is not supported';
439
+ if (err.includes('Video unavailable')) return 'Video is unavailable or private';
440
+ if (err.includes('Private video')) return 'This video is private';
441
+ if (err.includes('HTTP Error 403')) return 'Access denied by the platform';
442
+ if (err.includes('HTTP Error 404')) return 'Video not found';
443
+ if (err.includes('copyright')) return 'Video blocked due to copyright';
444
+ if (err.includes('geo')) return 'Video not available in your region';
445
+ if (err.includes('timed out') || err.includes('Timed out')) return 'Request timed out — try again';
446
+ if (err.includes('network') || err.includes('Network')) return 'Network error — check your connection';
447
+ return err.length > 80 ? err.slice(0, 80) + '...' : err;
448
+ }
449
+
450
+ document.getElementById('urls').addEventListener('keydown', e => {
451
+ if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); go(); }
452
+ });
453
+
454
+ async function go() {
455
+ const urls = parseUrls(document.getElementById('urls').value);
456
+ if (!urls.length) return;
457
+
458
+ const btn = document.getElementById('goBtn');
459
+ const container = document.getElementById('cards');
460
+ btn.disabled = true;
461
+ btn.textContent = 'Loading...';
462
+ container.innerHTML = '';
463
+ cardData = [];
464
+
465
+ for (let i = 0; i < urls.length; i++) {
466
+ const url = urls[i];
467
+ const idx = cardData.length;
468
+ cardData.push({ url, status: 'loading' });
469
+ renderCard(idx);
470
+
471
+ try {
472
+ const res = await fetch('/api/info', {
473
+ method: 'POST',
474
+ headers: { 'Content-Type': 'application/json' },
475
+ body: JSON.stringify({ url }),
476
+ });
477
+ const data = await res.json();
478
+ if (data.error) {
479
+ cardData[idx] = { ...cardData[idx], status: 'info-error', error: data.error };
480
+ } else {
481
+ cardData[idx] = {
482
+ ...cardData[idx],
483
+ status: 'ready',
484
+ title: data.title || '',
485
+ thumbnail: data.thumbnail || '',
486
+ duration: data.duration,
487
+ uploader: data.uploader || '',
488
+ formats: data.formats || [],
489
+ selectedFormatId: data.formats?.[0]?.id || null,
490
+ };
491
+ }
492
+ } catch (err) {
493
+ cardData[idx] = { ...cardData[idx], status: 'info-error', error: err.message };
494
+ }
495
+ renderCard(idx);
496
+ }
497
+
498
+ if (cardData.filter(c => c.status === 'ready').length > 1) {
499
+ renderDownloadAll();
500
+ }
501
+
502
+ btn.disabled = false;
503
+ btn.textContent = 'Fetch';
504
+ }
505
+
506
+ function renderCard(idx) {
507
+ const c = cardData[idx];
508
+ let el = document.getElementById(`card-${idx}`);
509
+ if (!el) {
510
+ el = document.createElement('div');
511
+ el.id = `card-${idx}`;
512
+ el.className = 'card';
513
+ document.getElementById('cards').appendChild(el);
514
+ }
515
+
516
+ // Loading skeleton
517
+ if (c.status === 'loading') {
518
+ el.className = 'card';
519
+ el.innerHTML = `
520
+ <div class="card-thumb loading"></div>
521
+ <div class="card-body">
522
+ <div class="skeleton-line medium"></div>
523
+ <div class="skeleton-line short"></div>
524
+ </div>
525
+ `;
526
+ return;
527
+ }
528
+
529
+ // Error state
530
+ if (c.status === 'info-error') {
531
+ el.className = 'card card-error';
532
+ el.innerHTML = `
533
+ <div class="card-thumb">
534
+ <div class="card-error-icon">
535
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
536
+ </div>
537
+ </div>
538
+ <div class="card-body">
539
+ <div class="card-title" style="color:var(--error)">Could not fetch video</div>
540
+ <div class="card-error-msg">${esc(friendlyError(c.error || ''))}</div>
541
+ <div class="card-error-url">${esc(c.url)}</div>
542
+ </div>
543
+ `;
544
+ return;
545
+ }
546
+
547
+ // Ready / downloading / done / error states
548
+ el.className = 'card';
549
+ const isAudio = currentFormat === 'audio';
550
+
551
+ let thumbHtml;
552
+ if (isAudio) {
553
+ thumbHtml = `<div class="no-thumb" style="color:var(--accent)"><svg viewBox="0 0 24 24" fill="currentColor" stroke="none"><polygon points="5 3 19 12 5 21 5 3"/></svg></div>`;
554
+ } else if (c.thumbnail) {
555
+ thumbHtml = `<img src="${c.thumbnail}" alt="">`;
556
+ } else {
557
+ thumbHtml = `<div class="no-thumb"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="2" width="20" height="20" rx="2"/><circle cx="8" cy="8" r="1.5"/><path d="m21 15-5-5L5 21"/></svg></div>`;
558
+ }
559
+
560
+ let qualityChips = '';
561
+ if (!isAudio && c.formats && c.formats.length > 1) {
562
+ qualityChips = c.formats.map(f =>
563
+ `<button class="q-chip${f.id === c.selectedFormatId ? ' active' : ''}" onclick="pickFormat(${idx}, '${f.id}')">${f.label}</button>`
564
+ ).join('');
565
+ }
566
+
567
+ let actionHtml = '';
568
+ if (c.status === 'ready') {
569
+ actionHtml = `<button class="card-dl-btn" onclick="dlCard(${idx})">Download</button>${qualityChips}`;
570
+ } else if (c.status === 'downloading') {
571
+ actionHtml = `<span class="card-status downloading"><span class="spin"></span> Downloading...</span>`;
572
+ } else if (c.status === 'done') {
573
+ actionHtml = `<button class="card-dl-btn done" onclick="saveCard(${idx})">Save</button>
574
+ <span class="card-status done">${esc(c.filename || '')}</span>`;
575
+ } else if (c.status === 'error') {
576
+ actionHtml = `<button class="card-dl-btn" onclick="dlCard(${idx})">Retry</button>
577
+ <span class="card-status error">${esc(friendlyError(c.error || 'Download failed'))}</span>`;
578
+ }
579
+
580
+ el.innerHTML = `
581
+ <div class="card-thumb">${thumbHtml}</div>
582
+ <div class="card-body">
583
+ <div class="card-title">${esc(c.title || 'Untitled')}</div>
584
+ <div class="card-meta">${esc(c.uploader)}${c.duration ? ' · ' + fmtDur(c.duration) : ''}</div>
585
+ <div class="card-actions">${actionHtml}</div>
586
+ </div>
587
+ `;
588
+ }
589
+
590
+ function renderDownloadAll() {
591
+ const existing = document.getElementById('dl-all-bar');
592
+ if (existing) existing.remove();
593
+
594
+ const bar = document.createElement('div');
595
+ bar.id = 'dl-all-bar';
596
+ bar.className = 'dl-all-bar';
597
+ bar.innerHTML = `<button class="dl-all-btn" onclick="dlAll()">Download All</button>`;
598
+ document.getElementById('cards').appendChild(bar);
599
+ }
600
+
601
+ function pickFormat(idx, formatId) {
602
+ cardData[idx].selectedFormatId = formatId;
603
+ renderCard(idx);
604
+ }
605
+
606
+ async function dlCard(idx) {
607
+ const c = cardData[idx];
608
+ c.status = 'downloading';
609
+ c.error = null;
610
+ renderCard(idx);
611
+
612
+ try {
613
+ const res = await fetch('/api/download', {
614
+ method: 'POST',
615
+ headers: { 'Content-Type': 'application/json' },
616
+ body: JSON.stringify({
617
+ url: c.url,
618
+ format: currentFormat,
619
+ format_id: c.selectedFormatId,
620
+ title: c.title || '',
621
+ }),
622
+ });
623
+ const data = await res.json();
624
+ if (data.error) {
625
+ c.status = 'error';
626
+ c.error = data.error;
627
+ renderCard(idx);
628
+ return;
629
+ }
630
+ c.jobId = data.job_id;
631
+ pollCard(idx);
632
+ } catch (err) {
633
+ c.status = 'error';
634
+ c.error = err.message;
635
+ renderCard(idx);
636
+ }
637
+ }
638
+
639
+ function pollCard(idx) {
640
+ const c = cardData[idx];
641
+ const iv = setInterval(async () => {
642
+ try {
643
+ const res = await fetch(`/api/status/${c.jobId}`);
644
+ const data = await res.json();
645
+ if (data.status === 'done') {
646
+ clearInterval(iv);
647
+ c.status = 'done';
648
+ c.filename = data.filename;
649
+ renderCard(idx);
650
+ saveCard(idx);
651
+ } else if (data.status === 'error') {
652
+ clearInterval(iv);
653
+ c.status = 'error';
654
+ c.error = data.error;
655
+ renderCard(idx);
656
+ }
657
+ } catch {
658
+ clearInterval(iv);
659
+ c.status = 'error';
660
+ c.error = 'Lost connection to server';
661
+ renderCard(idx);
662
+ }
663
+ }, 1000);
664
+ }
665
+
666
+ function saveCard(idx) {
667
+ const c = cardData[idx];
668
+ if (!c.jobId) return;
669
+ const a = document.createElement('a');
670
+ a.href = `/api/file/${c.jobId}`;
671
+ a.download = c.filename || '';
672
+ a.click();
673
+ }
674
+
675
+ async function dlAll() {
676
+ const btn = document.querySelector('.dl-all-btn');
677
+ if (btn) { btn.disabled = true; btn.textContent = 'Downloading...'; }
678
+
679
+ for (let i = 0; i < cardData.length; i++) {
680
+ if (cardData[i].status === 'ready') {
681
+ await dlCard(i);
682
+ }
683
+ }
684
+
685
+ if (btn) { btn.disabled = false; btn.textContent = 'Download All'; }
686
+ }
687
+ </script>
688
+ </body>
689
+ </html>