Compare commits
8 Commits
refactor/r
...
043c8b4df3
| Author | SHA1 | Date | |
|---|---|---|---|
| 043c8b4df3 | |||
| 5210d2c032 | |||
| 8b21034d78 | |||
| 2a5648506e | |||
| 7926534e54 | |||
| 0ed16eca62 | |||
| 35a2592dce | |||
| 5f78ac6a1a |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -23,6 +23,7 @@ env/
|
|||||||
/private/
|
/private/
|
||||||
archive/
|
archive/
|
||||||
downloads/
|
downloads/
|
||||||
|
test/
|
||||||
data.json
|
data.json
|
||||||
|
|
||||||
# --- django ---
|
# --- django ---
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
v2 architecture draft: see `docs/architecture-v2.org`
|
||||||
|
|
||||||
build and run the docker container
|
build and run the docker container
|
||||||
```
|
```
|
||||||
api_token = [discord bot token]
|
api_token = [discord bot token]
|
||||||
|
|||||||
24
agents.md
Normal file
24
agents.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# agent rules
|
||||||
|
|
||||||
|
## priorities
|
||||||
|
- optimize for simplicity, boringness, and long-term maintainability
|
||||||
|
- prefer minimal diffs; avoid refactors unless required for the active task
|
||||||
|
|
||||||
|
## tech stack
|
||||||
|
- python
|
||||||
|
- file storage: json and csv, no sqlite or databases
|
||||||
|
- assume local virtual env is available and accessible
|
||||||
|
- do not add new dependencies unless explicitly approved; if unavoidable, document justification in the active task notes
|
||||||
|
|
||||||
|
## workflow
|
||||||
|
- work on ONE task at a time unless explicitly instructed otherwise
|
||||||
|
- at the start of work, state the task id you are executing
|
||||||
|
- do not start work unless a task id is specified; if missing, choose the earliest unchecked task and say so
|
||||||
|
- propose incremental steps
|
||||||
|
- always include basic tests for core logic
|
||||||
|
- add assumptions and questions along-the-way to the ** notes section under the active task
|
||||||
|
- when you complete a task:
|
||||||
|
- mark it [X] in pm/tasks.org
|
||||||
|
- fill in evidence with commit hash + commands run
|
||||||
|
- never mark complete unless acceptance criteria are met
|
||||||
|
- include date and time (HH:MM)
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
# yt-dlp config file (yt-dlp.conf or .config/yt-dlp/config)
|
# yt-dlp config file
|
||||||
--simulate
|
|
||||||
-f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"
|
-f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"
|
||||||
--embed-chapters
|
--embed-chapters
|
||||||
--embed-info-json
|
--embed-info-json
|
||||||
--write-playlist-metafiles
|
--write-playlist-metafiles
|
||||||
#--paths "{"home":"./downloads"}"
|
|
||||||
--download-archive "/config/archive.txt"
|
--download-archive "/config/archive.txt"
|
||||||
--restrict-filenames
|
--restrict-filenames
|
||||||
--output "/downloads/%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s"
|
--output "/downloads/%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s"
|
||||||
# --split-chapters
|
|
||||||
248
docs/architecture-v2.org
Normal file
248
docs/architecture-v2.org
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
#+TITLE: youdis v2 architecture
|
||||||
|
#+DATE: [2026-03-31 Tue]
|
||||||
|
#+VERSION: 3
|
||||||
|
|
||||||
|
* Purpose
|
||||||
|
Youdis v2 splits the current monolithic Discord bot into:
|
||||||
|
- a private backend worker service that owns yt-dlp execution
|
||||||
|
- thin chat adapters that translate user actions into backend requests
|
||||||
|
|
||||||
|
The goal is to make downloader behavior inspectable, reusable, and easier to debug across multiple frontends without introducing queueing infrastructure or complex deployment.
|
||||||
|
|
||||||
|
** Goals
|
||||||
|
- Keep the backend transport-agnostic.
|
||||||
|
- Preserve archive-first duplicate prevention via `archive.txt`.
|
||||||
|
- Keep single-job semantics explicit.
|
||||||
|
- Treat requester and origin metadata as informational, not authorization.
|
||||||
|
- Make yt-dlp configuration understandable and debuggable.
|
||||||
|
|
||||||
|
** Non-Goals
|
||||||
|
- No backend auth or user membership logic.
|
||||||
|
- No database, Redis, or external queue.
|
||||||
|
- No multi-worker scheduling in v2.
|
||||||
|
- No frontend-owned downloader logic.
|
||||||
|
|
||||||
|
* Proposed Components
|
||||||
|
** Backend worker service
|
||||||
|
Owns:
|
||||||
|
- request validation
|
||||||
|
- single active job state
|
||||||
|
- yt-dlp executable invocation
|
||||||
|
- yt-dlp configuration selection and runtime argument construction
|
||||||
|
- archive behavior
|
||||||
|
- output path behavior
|
||||||
|
- progress and final result events
|
||||||
|
|
||||||
|
Does not own:
|
||||||
|
- Discord slash-command parsing
|
||||||
|
- chat formatting decisions
|
||||||
|
- access control policy
|
||||||
|
|
||||||
|
** Frontend adapters
|
||||||
|
1. Discord
|
||||||
|
2. Zulip
|
||||||
|
3. XMPP
|
||||||
|
|
||||||
|
Own:
|
||||||
|
- command parsing
|
||||||
|
- user-facing message formatting
|
||||||
|
- transport-specific reply behavior
|
||||||
|
- optional frontend-side gating if the deployment wants it
|
||||||
|
|
||||||
|
Do not own:
|
||||||
|
- yt-dlp option construction
|
||||||
|
- archive checks
|
||||||
|
- output path logic
|
||||||
|
- job lifecycle rules
|
||||||
|
|
||||||
|
* Initial Deployment Shape
|
||||||
|
|
||||||
|
V2 should start as a single private process boundary with two layers:
|
||||||
|
1. backend service process
|
||||||
|
2. one Discord adapter process
|
||||||
|
|
||||||
|
The backend is intended for private/local deployment. Trust comes from deployment topology and network placement, not backend auth.
|
||||||
|
|
||||||
|
** Execution model
|
||||||
|
The backend is a thin FastAPI wrapper around the `yt-dlp` executable, not a deep Python integration, unless proven necessary.
|
||||||
|
|
||||||
|
Why this is the default:
|
||||||
|
- keeps behavior closer to native `yt-dlp`
|
||||||
|
- makes container debugging easier because commands can be reproduced manually
|
||||||
|
- avoids reimplementing `yt-dlp` option parsing unless we truly need tighter integration
|
||||||
|
|
||||||
|
This means the backend is primarily responsible for:
|
||||||
|
- deciding when `yt-dlp` may run
|
||||||
|
- constructing a safe effective command
|
||||||
|
- supervising the subprocess while it is active
|
||||||
|
- translating process outcomes into a small API contract
|
||||||
|
|
||||||
|
** Backend Contract
|
||||||
|
*** Request model
|
||||||
|
Minimal fields:
|
||||||
|
- `url`
|
||||||
|
- `requester_id` optional
|
||||||
|
- `requester_name` optional
|
||||||
|
- `origin` optional
|
||||||
|
- `requested_at` optional
|
||||||
|
Only `url` affects downloader behavior in v2. The rest is passthrough metadata for logs and frontend context.
|
||||||
|
|
||||||
|
*** Result states
|
||||||
|
The backend should keep its external state model deliberately small:
|
||||||
|
- `accepted`
|
||||||
|
- `busy`
|
||||||
|
- `running`
|
||||||
|
- `completed`
|
||||||
|
- `failed`
|
||||||
|
- `cancelled`
|
||||||
|
|
||||||
|
These states should be transport-agnostic, human-debuggable, and only distinct when a frontend would behave differently.
|
||||||
|
|
||||||
|
Internal phases and details may be richer, but they should usually be carried as metadata rather than promoted to top-level states.
|
||||||
|
|
||||||
|
Examples of detail fields that can travel alongside the small state model:
|
||||||
|
- `phase=validating`
|
||||||
|
- `phase=downloading`
|
||||||
|
- `disposition=archive_hit`
|
||||||
|
- `message=already in archive`
|
||||||
|
- `result_path=/downloads/...`
|
||||||
|
|
||||||
|
This keeps the API simple while still leaving room for useful operator-facing detail.
|
||||||
|
|
||||||
|
*** Minimal endpoints
|
||||||
|
The first backend seam should stay small:
|
||||||
|
- `POST /jobs`
|
||||||
|
- `GET /jobs/current`
|
||||||
|
- `POST /jobs/current/cancel`
|
||||||
|
- `GET /health`
|
||||||
|
- `GET /version`
|
||||||
|
|
||||||
|
Polling is the default integration model for v2. Streaming or push delivery is deferred unless a real frontend need appears.
|
||||||
|
|
||||||
|
** Single-Job Semantics
|
||||||
|
V2 explicitly supports one active job at a time.
|
||||||
|
- If idle, the backend accepts a new job and marks it active.
|
||||||
|
- If busy, the backend rejects the new request with a `busy` result.
|
||||||
|
- Cancel is coarse and best-effort.
|
||||||
|
- The backend clears active state when the job reaches `completed`, `failed`, or `cancelled`.
|
||||||
|
|
||||||
|
This is a feature, not a limitation, for the initial service.
|
||||||
|
|
||||||
|
* yt-dlp Configuration Ownership
|
||||||
|
|
||||||
|
This is the most important v2 cleanup.
|
||||||
|
|
||||||
|
The backend owns all yt-dlp invocation behavior. Frontends must not build `yt-dlp` commands or option dictionaries.
|
||||||
|
|
||||||
|
** Config split
|
||||||
|
|
||||||
|
Static settings belong in `default-yt-dlp.conf`:
|
||||||
|
- archive path
|
||||||
|
- output template
|
||||||
|
- embedding and metadata preferences
|
||||||
|
- stable format defaults
|
||||||
|
- retry defaults
|
||||||
|
|
||||||
|
Runtime settings belong in backend code:
|
||||||
|
- target URL
|
||||||
|
- config-file path, if explicitly set at launch
|
||||||
|
- request-scoped flags or overrides
|
||||||
|
- subprocess lifecycle and cancellation behavior
|
||||||
|
- any values that must vary per request
|
||||||
|
|
||||||
|
*** Merge rule
|
||||||
|
The backend should invoke `yt-dlp` with the default config first, then apply a small set of explicit runtime overrides.
|
||||||
|
|
||||||
|
If a runtime override conflicts with file-backed config, the override wins and the backend should log the effective value used.
|
||||||
|
|
||||||
|
*** Debuggability requirement
|
||||||
|
For each job, the backend should make it easy to inspect:
|
||||||
|
|
||||||
|
- config file path used
|
||||||
|
- effective `yt-dlp` command or key override arguments
|
||||||
|
- final normalized job result
|
||||||
|
|
||||||
|
This exists specifically because the current config behavior has been difficult to reason about.
|
||||||
|
|
||||||
|
*** Config hygiene expectations
|
||||||
|
|
||||||
|
The default config should be safe for real downloads in production-like use.
|
||||||
|
|
||||||
|
That means test-only settings such as `--simulate` should not remain enabled in the default runtime path unless the backend intentionally supports an explicit dry-run mode.
|
||||||
|
|
||||||
|
** Recommended Repo Shape
|
||||||
|
One reasonable first cut:
|
||||||
|
|
||||||
|
#+begin_quote
|
||||||
|
youdis/
|
||||||
|
README.md
|
||||||
|
default-yt-dlp.conf
|
||||||
|
youdis/
|
||||||
|
__init__.py
|
||||||
|
main.py
|
||||||
|
models.py
|
||||||
|
adapters/
|
||||||
|
__init__.py
|
||||||
|
discord.py
|
||||||
|
docs/
|
||||||
|
architecture-v2.org
|
||||||
|
#+end_quote
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- `main.py` owns the FastAPI app, active-job state, and `yt-dlp` subprocess lifecycle.
|
||||||
|
- `models.py` owns request and response models.
|
||||||
|
- `adapters/discord.py` becomes a thin client of the backend.
|
||||||
|
|
||||||
|
** Example response shape
|
||||||
|
|
||||||
|
One likely response shape for v1:
|
||||||
|
|
||||||
|
#+begin_src json
|
||||||
|
{
|
||||||
|
"state": "completed",
|
||||||
|
"disposition": "archive_hit",
|
||||||
|
"message": "already in archive",
|
||||||
|
"job_id": "abc123"
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
And for an active job:
|
||||||
|
|
||||||
|
#+begin_src json
|
||||||
|
{
|
||||||
|
"state": "running",
|
||||||
|
"phase": "downloading",
|
||||||
|
"message": "downloading 3 of 9",
|
||||||
|
"job_id": "abc123"
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
** Framework Recommendation
|
||||||
|
|
||||||
|
FastAPI is the current recommendation for the backend seam.
|
||||||
|
|
||||||
|
Why it fits:
|
||||||
|
|
||||||
|
- typed request and response models help define the contract cleanly
|
||||||
|
- built-in docs make local inspection easier during early iterations
|
||||||
|
- it stays small enough for this service if we keep the surface area disciplined
|
||||||
|
|
||||||
|
Why this is not a strong ideological choice:
|
||||||
|
|
||||||
|
- Flask would also work if we decide the type/model ergonomics are not worth it
|
||||||
|
- the important decision is keeping the seam small, not choosing a fashionable framework
|
||||||
|
|
||||||
|
** Immediate Next Task
|
||||||
|
|
||||||
|
`2.0.1` should implement the backend skeleton with just enough real behavior to prove the seam:
|
||||||
|
|
||||||
|
- package/module layout
|
||||||
|
- health and version endpoint
|
||||||
|
- single active-job state holder
|
||||||
|
- one submit-job path
|
||||||
|
- one current-job status path
|
||||||
|
- `yt-dlp` subprocess invocation using `default-yt-dlp.conf`
|
||||||
|
- explicit runtime overrides for request URL and cancel behavior
|
||||||
|
|
||||||
|
The Discord adapter should remain unchanged until that backend skeleton can accept a job and report status coherently.
|
||||||
@@ -2,8 +2,9 @@
|
|||||||
#+updated: [2026-03-31 Tue 16:03]
|
#+updated: [2026-03-31 Tue 16:03]
|
||||||
|
|
||||||
Use the template below, which should be a top-level org-mode header.
|
Use the template below, which should be a top-level org-mode header.
|
||||||
|
A sample task is below the template
|
||||||
|
|
||||||
* [ ] M.m.m: Task Title (# commits)
|
* [ ] M.m.m: Task Title (<estimated # of commits>)
|
||||||
description of the task
|
description of the task
|
||||||
** pm notes: amplifying information
|
** pm notes: amplifying information
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ description of the task
|
|||||||
1. Criterion
|
1. Criterion
|
||||||
- expanded data
|
- expanded data
|
||||||
2. Criterion
|
2. Criterion
|
||||||
|
3f. Criterion added after initial task completion
|
||||||
|
|
||||||
** evidence
|
** evidence
|
||||||
- commit: abc123, bcd234
|
- commit: abc123, bcd234
|
||||||
@@ -19,3 +21,61 @@ description of the task
|
|||||||
|
|
||||||
** notes
|
** notes
|
||||||
- explanation of work done, decisions made, reasoning
|
- explanation of work done, decisions made, reasoning
|
||||||
|
|
||||||
|
|
||||||
|
* [ ] 2.0.1: build backend yt-dlp worker (3)
|
||||||
|
create the minimal backend/service skeleton and establish a working yt-dlp baseline with clean hooks for future frontends
|
||||||
|
** pm notes
|
||||||
|
- foundation; don't need the full finished service here, just the basic shape plus enough real yt-dlp execution to validate the seam and build on it.
|
||||||
|
- keep single-job semantics
|
||||||
|
- prioritize inspectable behavior over polish
|
||||||
|
|
||||||
|
** Acceptance Criteria
|
||||||
|
1. create the backend/service skeleton
|
||||||
|
- app/module layout exists
|
||||||
|
- core request path is stubbed or minimally working
|
||||||
|
2. establish a working yt-dlp baseline
|
||||||
|
- archive behavior is preserved
|
||||||
|
- output path behavior is preserved or intentionally updated
|
||||||
|
- use yt-dlp .conf and set reasonable default
|
||||||
|
3. expose basic hooks/interfaces for future frontends
|
||||||
|
- submit/request path exists
|
||||||
|
- status/progress hook exists
|
||||||
|
- basic health/version visibility exists
|
||||||
|
4f. print env vars to stdout on fastapi launch
|
||||||
|
- repo_root
|
||||||
|
- default_config
|
||||||
|
- ytdlp_executable
|
||||||
|
5f. create ytdlp conf to facilitate tests
|
||||||
|
** evidence
|
||||||
|
- commit:
|
||||||
|
- tests:
|
||||||
|
1. `python3 -m py_compile ./youdis/main.py ./youdis/models.py ./youdis/adapters/__init__.py ./youdis/adapters/discord.py`
|
||||||
|
2. `python3 -m uvicorn youdis.main:app --host 127.0.0.1 --port 8000`
|
||||||
|
3. `sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/download/2026.03.17/yt-dlp -o /usr/local/bin/yt-dlp`
|
||||||
|
4. `sudo chmod +x /usr/local/bin/yt-dlp`
|
||||||
|
5. `yt-dlp --version`
|
||||||
|
6. `curl http://127.0.0.1:8000/health`
|
||||||
|
7. `curl http://127.0.0.1:8000/version`
|
||||||
|
8. `curl -X POST http://127.0.0.1:8000/jobs -H 'content-type: application/json' -d '{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"}'`
|
||||||
|
9. `curl http://127.0.0.1:8000/jobs/current`
|
||||||
|
10. if testing in container: `docker build -t youdis:v2 .`
|
||||||
|
11. if testing in container: `docker run --rm -p 8000:8000 -v [config]:/config -v [downloads]:/downloads youdis:v2`
|
||||||
|
:OUTPUT_STEP1-9:
|
||||||
|
user@paladin:~/proj/youdis$ curl http://127.0.0.1:8000/health
|
||||||
|
{"status":"ok"}user@paladin:~/proj/youdis$
|
||||||
|
user@paladin:~/proj/youdis$ curl http://127.0.0.1:8000/version
|
||||||
|
{"version":"20250829-ec72c56","active_job":false}user@paladin:~/proj/youdis$
|
||||||
|
user@paladin:~/proj/youdis$ curl -X POST http://127.0.0.1:8000/jobs -H 'content-type: application/json' -d '{"url":"https://www.youtube.com/watch?v=3i72yY_LaW4"}'
|
||||||
|
{"job_id":"cc85165e-d906-4eee-864f-1a398b6de2e0","state":"accepted","url":"https://www.youtube.com/watch?v=3i72yY_LaW4","message":"accepted","phase":"queued","disposition":null,"requester_id":null,"requester_name":null,"origin":null,"result_path":null,"command":[],"returncode":null,"created_at":"2026-04-01T00:44:35.657196","updated_at":"2026-04-01T00:44:35.657198"}user@paladin:~/proj/youdis$ curl http://127.0.0.1:8000/jobs/current
|
||||||
|
{"active":false,"job":{"job_id":"cc85165e-d906-4eee-864f-1a398b6de2e0","state":"failed","url":"https://www.youtube.com/watch?v=3i72yY_LaW4","message":"ERROR: unable to create directory [Errno 13] Permission denied: '/downloads'","phase":null,"disposition":null,"requester_id":null,"requester_name":null,"origin":null,"result_path":null,"command":["yt-dlp","--config-locations","/home/user/proj/youdis/default-yt-dlp.conf","https://www.youtube.com/watch?v=3i72yY_LaW4"],"returncode":1,"created_at":"2026-04-01T00:44:35.657196","updated_at":"2026-03-31T20:44:36.653353"}}user@paladin:~/proj/youdis$
|
||||||
|
:END:
|
||||||
|
- datetime: [2026-03-31 Tue 20:45]
|
||||||
|
|
||||||
|
** notes
|
||||||
|
- validate `yt-dlp` subprocess invocation in-container; not verifiable in the current shell because `yt-dlp` is not installed here
|
||||||
|
- confirm `--config-locations` behavior against the installed `yt-dlp` version during integration testing
|
||||||
|
- current backend scaffold is not yet wired into `dockerfile` or `run-youdis.sh`
|
||||||
|
- archive-hit and result-path parsing currently depend on `yt-dlp` stdout text patterns, so treat them as provisional until integration-tested
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
259
pm/tasks-v2.org
Normal file
259
pm/tasks-v2.org
Normal file
@@ -0,0 +1,259 @@
|
|||||||
|
#+title: Task Log
|
||||||
|
#+updated: [2026-03-31 Tue 16:03]
|
||||||
|
#+startup: overview
|
||||||
|
# Local Variables:
|
||||||
|
# org-babel-python-command: "/home/user/proj/youdis/.venv/bin/python"
|
||||||
|
# project-dir: "~/proj/youdis"
|
||||||
|
# End:
|
||||||
|
|
||||||
|
* youdis v2 goals:
|
||||||
|
1. Separate backend from frontend
|
||||||
|
2. Offload auth
|
||||||
|
3. Ensure auto nightly builds
|
||||||
|
4. Default output format supports plex browsing youtube channels as "tv shows"
|
||||||
|
5. Facilitate multiple GUI inbounds: Discord, Zulip, XMPP
|
||||||
|
|
||||||
|
* [X] 2.0.0: define architecture (2-4)
|
||||||
|
define the target architecture for a private backend yt-dlp worker with thin chat frontends
|
||||||
|
** pm notes:
|
||||||
|
- keep this iterative. the point is to choose the shape and seam, not prematurely implement infra. likely decisions include backend framework, request/status model, and how thin the discord shim should be.
|
||||||
|
- goal is simple, private, maintainable deployment
|
||||||
|
- avoid auth, queues, or persistence beyond clear & immediate needs
|
||||||
|
|
||||||
|
** Acceptance Criteria
|
||||||
|
1. document the target architecture at a high level
|
||||||
|
- backend owns yt-dlp execution and job state
|
||||||
|
- frontends own chat-specific UX
|
||||||
|
2. identify key decisions still open
|
||||||
|
- backend choice
|
||||||
|
- service seam/endpoints
|
||||||
|
- status/progress model
|
||||||
|
3. capture enough structure to begin implementation
|
||||||
|
- repo/component layout is sketched
|
||||||
|
- next implementation task is unblocked
|
||||||
|
|
||||||
|
** evidence
|
||||||
|
- commit: 0ed16ec
|
||||||
|
- tests: n/a
|
||||||
|
- datetime: [2026-03-31 Tue 18:48]
|
||||||
|
|
||||||
|
** notes
|
||||||
|
- first architecture draft captured in `docs/architecture-v2.org`
|
||||||
|
|
||||||
|
* [X] 2.0.1: build backend yt-dlp worker (3)
|
||||||
|
create the minimal backend/service skeleton and establish a working yt-dlp baseline with clean hooks for future frontends
|
||||||
|
** pm notes
|
||||||
|
- foundation; don't need the full finished service here, just the basic shape plus enough real yt-dlp execution to validate the seam and build on it.
|
||||||
|
- keep single-job semantics
|
||||||
|
- prioritize inspectable behavior over polish
|
||||||
|
|
||||||
|
** Acceptance Criteria
|
||||||
|
1. create the backend/service skeleton
|
||||||
|
- app/module layout exists
|
||||||
|
- core request path is stubbed or minimally working
|
||||||
|
2. establish a working yt-dlp baseline
|
||||||
|
- archive behavior is preserved
|
||||||
|
- output path behavior is preserved or intentionally updated
|
||||||
|
- use yt-dlp .conf and set reasonable default
|
||||||
|
3. expose basic hooks/interfaces for future frontends
|
||||||
|
- submit/request path exists
|
||||||
|
- status/progress hook exists
|
||||||
|
- basic health/version visibility exists
|
||||||
|
4. make local testing practical without breaking container defaults
|
||||||
|
- backend can run when `/config` or `/downloads` are not writable in local dev
|
||||||
|
- env vars may override executable/config/download paths
|
||||||
|
- status makes the effective runtime command/paths inspectable
|
||||||
|
|
||||||
|
** evidence
|
||||||
|
- commit: 7926534,2a56485
|
||||||
|
- tests:
|
||||||
|
1. `python3 -m py_compile ./youdis/main.py ./youdis/models.py ./youdis/adapters/__init__.py ./youdis/adapters/discord.py`
|
||||||
|
2. `python3 -m uvicorn youdis.main:app --host 127.0.0.1 --port 8000`
|
||||||
|
3. `sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/download/2026.03.17/yt-dlp -o /usr/local/bin/yt-dlp`
|
||||||
|
4. `sudo chmod +x /usr/local/bin/yt-dlp`
|
||||||
|
5. `yt-dlp --version`
|
||||||
|
6. `curl http://127.0.0.1:8000/health`
|
||||||
|
7. `curl http://127.0.0.1:8000/version`
|
||||||
|
8. `curl -X POST http://127.0.0.1:8000/jobs -H 'content-type: application/json' -d '{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"}'`
|
||||||
|
9. `curl http://127.0.0.1:8000/jobs/current`
|
||||||
|
10. if testing in container: `docker build -t youdis:v2 .`
|
||||||
|
11. if testing in container: `docker run --rm -p 8000:8000 -v [config]:/config -v [downloads]:/downloads youdis:v2`
|
||||||
|
:OUTPUT_STEP1-9:
|
||||||
|
user@paladin:~/proj/youdis$ curl http://127.0.0.1:8000/health
|
||||||
|
{"status":"ok"}
|
||||||
|
user@paladin:~/proj/youdis$ curl http://127.0.0.1:8000/version
|
||||||
|
{"version":"20250829-ec72c56","active_job":false}
|
||||||
|
user@paladin:~/proj/youdis$ curl -X POST http://127.0.0.1:8000/jobs -H 'content-type: application/json' -d '{"url":"https://www.youtube.com/watch?v=3i72yY_LaW4"}'
|
||||||
|
{"job_id":"cc85165e-d906-4eee-864f-1a398b6de2e0","state":"accepted","url":"https://www.youtube.com/watch?v=3i72yY_LaW4","message":"accepted","phase":"queued","disposition":null,"requester_id":null,"requester_name":null,"origin":null,"result_path":null,"command":[],"returncode":null,"created_at":"2026-04-01T00:44:35.657196","updated_at":"2026-04-01T00:44:35.657198"}
|
||||||
|
user@paladin:~/proj/youdis$ curl http://127.0.0.1:8000/jobs/current
|
||||||
|
{"active":false,"job":{"job_id":"cc85165e-d906-4eee-864f-1a398b6de2e0","state":"failed","url":"https://www.youtube.com/watch?v=3i72yY_LaW4","message":"ERROR: unable to create directory [Errno 13] Permission denied: '/downloads'","phase":null,"disposition":null,"requester_id":null,"requester_name":null,"origin":null,"result_path":null,"command":["yt-dlp","--config-locations","/home/user/proj/youdis/default-yt-dlp.conf","https://www.youtube.com/watch?v=3i72yY_LaW4"],"returncode":1,"created_at":"2026-04-01T00:44:35.657196","updated_at":"2026-03-31T20:44:36.653353"}}user@paladin:~/proj/youdis$
|
||||||
|
:END:
|
||||||
|
- datetime: [2026-03-31 Tue 20:45]
|
||||||
|
|
||||||
|
** notes
|
||||||
|
- validate `yt-dlp` subprocess invocation in-container; not verifiable in the current shell because `yt-dlp` is not installed here
|
||||||
|
- confirm `--config-locations` behavior against the installed `yt-dlp` version during integration testing
|
||||||
|
- current backend scaffold is not yet wired into `dockerfile` or `run-youdis.sh`
|
||||||
|
- archive-hit and result-path parsing currently depend on `yt-dlp` stdout text patterns, so treat them as provisional until integration-tested
|
||||||
|
- local dev now falls back to repo-local `.runtime/{config,downloads}` when `/config` or `/downloads` are not writable
|
||||||
|
- had to uninstall yt-dlp python pkg from the venv, which resulted in a '403 Forbidden'
|
||||||
|
|
||||||
|
* [ ] 2.0.2: update discord bot to use new backend (3)
|
||||||
|
update the discord bot into a thin frontend that talks to the backend and verify the flow end to end
|
||||||
|
** pm notes
|
||||||
|
- this is the first real frontend proof. once this works cleanly, zulip/xmpp should mostly be adapter work rather than downloader rewrites.
|
||||||
|
- keep discord logic thin; no auth
|
||||||
|
- do not duplicate yt-dlp behavior in the bot
|
||||||
|
|
||||||
|
** Acceptance Criteria
|
||||||
|
1. discord bot submits requests to backend
|
||||||
|
- command/input handling works
|
||||||
|
- acceptance/busy/failure responses are clear
|
||||||
|
2. discord bot relays useful backend status
|
||||||
|
- progress reporting works at a basic level
|
||||||
|
- completion/failure/skipped outcomes are surfaced
|
||||||
|
3. backend-discord flow is tested end to end
|
||||||
|
- valid request path tested
|
||||||
|
- busy or conflict behavior tested
|
||||||
|
- failure path tested
|
||||||
|
4. add dotenv support to ease dev
|
||||||
|
- os.getenv methods remain standard for prod/docker build
|
||||||
|
- populate .env with dev env defaults
|
||||||
|
|
||||||
|
** evidence
|
||||||
|
- commit:
|
||||||
|
- tests: https://youtu.be/20HxMMSqRyg?si=3v7mN2L88c_FxpQR 18m
|
||||||
|
1. start backend: `python3 -m uvicorn youdis.main:app --host 127.0.0.1 --port 8000`
|
||||||
|
2. create local env file: `cp .env.example .env`
|
||||||
|
3. add `api_token` to `.env`
|
||||||
|
4. start adapter: `python3 ./youdis.py`
|
||||||
|
5. in discord, run `/youtube url:https://www.youtube.com/watch?v=dQw4w9WgXcQ`
|
||||||
|
:out:
|
||||||
|
#+begin_src shell
|
||||||
|
(venv) user@paladin:~/proj/youdis$ python ./youdis.py
|
||||||
|
Task exception was never retrieved
|
||||||
|
future: <Task finished name='Task-43' coro=<Client._dispatch_interaction() done, defined at /home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/client/client.py:1773> exception=KeyError('youtube')>
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/client/client.py", line 1798, in _dispatch_interaction
|
||||||
|
if ctx.command:
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/models/internal/context.py", line 329, in command
|
||||||
|
return self.client._interaction_lookup[self._command_name]
|
||||||
|
KeyError: 'youtube'
|
||||||
|
#+end_src
|
||||||
|
:end:
|
||||||
|
|
||||||
|
6. confirm channel response says the job was submitted to backend
|
||||||
|
7. confirm requester receives DM updates for accepted/running/completed or failed
|
||||||
|
:out:
|
||||||
|
accepted job 4ef6fde5-57cc-478c-b0e2-18458c693fa4 for https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
||||||
|
state=running | phase=running | [youtube] dQw4w9WgXcQ: Downloading webpage | path=/home/user/proj/youdis/downloads
|
||||||
|
state=running | phase=downloading | [download] Destination: /home/user/proj/youdis/downloads/Rick_Astley/NA/NANARickAstley-_Never_Gonna_Give_You_Up_Official_Video_4K_Remaster.f401.mp4 | path=/home/user/proj/youdis/downloads/Rick_Astley/NA/NANARickAstley-_Never_Gonna_Give_You_Up_Official_Video_4K_Remaster.f401.mp4
|
||||||
|
state=completed | [Metadata] There isn't any metadata to add | path=/home/user/proj/youdis/downloads/Rick_Astley/NA/NANARickAstley-_Never_Gonna_Give_You_Up_Official_Video_4K_Remaster.mp4
|
||||||
|
:end:
|
||||||
|
8. while first job is active, submit another `/youtube` and confirm busy behavior
|
||||||
|
:out:
|
||||||
|
#+begin_src shell
|
||||||
|
Error: AttributeError
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/client/client.py", line 1900, in __dispatch_interaction
|
||||||
|
response = await callback
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/client/client.py", line 1771, in _run_slash_command
|
||||||
|
return await command(ctx, **ctx.kwargs)
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/models/internal/command.py", line 132, in __call__
|
||||||
|
await self.call_callback(self.callback, context)
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/models/internal/application_commands.py", line 833, in call_callback
|
||||||
|
return await self.call_with_binding(callback, ctx, *new_args, **new_kwargs)
|
||||||
|
File "/home/user/proj/youdis/venv/lib/python3.10/site-packages/interactions/models/internal/callback.py", line 43, in call_with_binding
|
||||||
|
return await callback(*args, **kwargs)
|
||||||
|
File "/home/user/proj/youdis/youdis/adapters/discord.py", line 167, in youtube
|
||||||
|
await ctx.channel.send(f"Submitted <{url}> to the backend. Status updates via DM.")
|
||||||
|
AttributeError: 'NoneType' object has no attribute 'send'
|
||||||
|
#+end_src
|
||||||
|
busy: busy with https://youtu.be/20HxMMSqRyg?si=3v7mN2L88c_FxpQR
|
||||||
|
state=running | phase=running | cancel requested | path=/home/user/proj/youdis/downloads/James_Hoffmann/NA/NANAThe_Beginner_s_Guide_To_Latte_Art.f401.mp4
|
||||||
|
:end:
|
||||||
|
9. run `/status` and confirm it reflects current or last backend job
|
||||||
|
:out:
|
||||||
|
last job: state=completed | [Metadata] There isn't any metadata to add | path=/home/user/proj/youdis/downloads/Rick_Astley/NA/NANARickAstley-_Never_Gonna_Give_You_Up_Official_Video_4K_Remaster.mp4
|
||||||
|
:end:
|
||||||
|
10. run `/interrupt` as owner and confirm cancellation is surfaced via DM
|
||||||
|
:out:
|
||||||
|
last job: state=cancelled | cancelled | path=/home/user/proj/youdis/downloads/James_Hoffmann/NA/NANAThe_Beginner_s_Guide_To_Latte_Art.f401.mp4
|
||||||
|
:end:
|
||||||
|
- datetime:[2026-04-02 Thu 11:52]
|
||||||
|
|
||||||
|
*** org-block tests
|
||||||
|
#+begin_src shell :dir ~/proj/youdis :results output verbatim
|
||||||
|
source ./venv/bin/activate
|
||||||
|
python3 -m uvicorn youdis.main:app --host 127.0.0.1 --port 8000
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+begin_src shell :results output
|
||||||
|
echo ok
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS:
|
||||||
|
: ok
|
||||||
|
|
||||||
|
#+begin_src python :dir ~/proj/youdis :results output verbatim
|
||||||
|
python ./youdis.py
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS:
|
||||||
|
|
||||||
|
** notes
|
||||||
|
- discord adapter is now a thin HTTP client of the backend; it no longer imports or configures yt-dlp
|
||||||
|
- `YOUDIS_BACKEND_URL` controls which backend the adapter targets
|
||||||
|
- progress updates are currently implemented by polling `/jobs/current` and DMing only when the summary changes
|
||||||
|
- legacy auth/user-management commands were removed from the active adapter path and should be cleaned up formally in `2.0.3`
|
||||||
|
- `.env` is now supported for local/dev convenience, while real environment variables still override it in prod/docker
|
||||||
|
- submitting via DM doesn't work
|
||||||
|
* [ ] 2.0.3: remove deprecated discord-bot functionality (2)
|
||||||
|
delete or retire legacy bot behaviors that no longer fit once the backend split is in place
|
||||||
|
** pm notes
|
||||||
|
- only remove this after the new path works. this is cleanup, not pioneering work.
|
||||||
|
- favor deletion over compatibility shims
|
||||||
|
- keep operator controls only if still useful
|
||||||
|
|
||||||
|
** Acceptance Criteria
|
||||||
|
1. remove obsolete auth/user-management behavior
|
||||||
|
- old user persistence and commands are removed
|
||||||
|
- backend-facing flow no longer depends on them
|
||||||
|
2. remove obsolete downloader/runtime logic from bot
|
||||||
|
- bot no longer owns yt-dlp execution
|
||||||
|
- dead code paths are deleted
|
||||||
|
3. leave the bot in a coherent state
|
||||||
|
- remaining commands reflect actual supported behavior
|
||||||
|
- deprecated artifacts are clearly removed or marked
|
||||||
|
|
||||||
|
** evidence
|
||||||
|
- commit:
|
||||||
|
- tests:
|
||||||
|
- datetime:
|
||||||
|
|
||||||
|
** notes
|
||||||
|
|
||||||
|
* [ ] 2.0.4: fix automation and build pipeline (3)
|
||||||
|
repair and simplify the build/update/deploy path so it matches the new backend-plus-frontend structure
|
||||||
|
** pm notes
|
||||||
|
- this should come after architecture and discord integration stabilize. no point polishing the pipeline for the wrong shape.
|
||||||
|
- optimize for simple manual ops first
|
||||||
|
- stop here after pipeline is sane
|
||||||
|
|
||||||
|
** Acceptance Criteria
|
||||||
|
1. align build artifacts with the new structure
|
||||||
|
- docker/build scripts reflect current components
|
||||||
|
- runtime assumptions are consistent
|
||||||
|
2. review old automation artifacts
|
||||||
|
- stale runner/update/restart logic is removed or updated
|
||||||
|
- manual update/rebuild flow is clear
|
||||||
|
3. confirm deployment path works
|
||||||
|
- local or unraid deployment is validated
|
||||||
|
- pipeline is understandable enough to maintain
|
||||||
|
|
||||||
|
** evidence
|
||||||
|
- commit:
|
||||||
|
- tests:
|
||||||
|
- datetime:
|
||||||
|
|
||||||
|
** notes
|
||||||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
234
youdis.py
234
youdis.py
@@ -1,235 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
'''
|
|
||||||
youdis v1.1
|
|
||||||
bot for downloading youtube videos using yt-dlp
|
|
||||||
discord-py-interactions 5.11.0 has new option
|
|
||||||
requires python>=3.9
|
|
||||||
'''
|
|
||||||
# match_filter: info_dict -> Raise utils.DownloadCancelled(msg) ? interrupt
|
|
||||||
|
|
||||||
import interactions
|
from youdis.adapters.discord import main
|
||||||
from os import getenv
|
|
||||||
from pathlib import Path
|
|
||||||
import yt_dlp
|
|
||||||
import json
|
|
||||||
import asyncio
|
|
||||||
import threading
|
|
||||||
|
|
||||||
userFile = Path('/config/users.json')
|
|
||||||
userFile.parent.mkdir(exist_ok=True, parents=True)
|
|
||||||
|
|
||||||
bot = interactions.Client(intents=interactions.Intents.DEFAULT,default_scope=2147491904)
|
|
||||||
|
|
||||||
def save_authorized_users(authorized_users):
|
|
||||||
with open(userFile, 'w') as f:
|
|
||||||
json.dump({'authorized_users': authorized_users}, f)
|
|
||||||
|
|
||||||
def load_authorized_users():
|
|
||||||
if not userFile.exists():
|
|
||||||
save_authorized_users([])
|
|
||||||
print(f'users.json not found; saving to {userFile}')
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(userFile, 'r') as f:
|
|
||||||
data = json.load(f)
|
|
||||||
except (json.JSONDecodeError, OSError):
|
|
||||||
save_authorized_users([])
|
|
||||||
print(f'users.json invalid; resetting {userFile}')
|
|
||||||
return []
|
|
||||||
|
|
||||||
authorized_users = data.get('authorized_users', [])
|
|
||||||
if not isinstance(authorized_users, list):
|
|
||||||
authorized_users = []
|
|
||||||
|
|
||||||
authorized_users = [str(user_id) for user_id in authorized_users]
|
|
||||||
save_authorized_users(authorized_users)
|
|
||||||
print(f'authorized_users:{authorized_users}')
|
|
||||||
return authorized_users
|
|
||||||
|
|
||||||
authorized_users = load_authorized_users()
|
|
||||||
|
|
||||||
active_job_lock = threading.Lock()
|
|
||||||
active_job = None
|
|
||||||
|
|
||||||
async def send_message(ctx, message):
|
|
||||||
await ctx.author.send(message)
|
|
||||||
|
|
||||||
def claim_active_job(job):
|
|
||||||
global active_job
|
|
||||||
with active_job_lock:
|
|
||||||
if active_job is not None:
|
|
||||||
return active_job
|
|
||||||
active_job = job
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_active_job():
|
|
||||||
with active_job_lock:
|
|
||||||
return active_job
|
|
||||||
|
|
||||||
def clear_active_job(job):
|
|
||||||
global active_job
|
|
||||||
with active_job_lock:
|
|
||||||
if active_job is job:
|
|
||||||
active_job = None
|
|
||||||
|
|
||||||
def download_video(url, options):
|
|
||||||
with yt_dlp.YoutubeDL(options) as ydl:
|
|
||||||
ydl.download(url)
|
|
||||||
|
|
||||||
def create_hook(ctx, loop, cancel_event):
|
|
||||||
seen_updates = set()
|
|
||||||
|
|
||||||
def hook(d):
|
|
||||||
if cancel_event.is_set():
|
|
||||||
raise yt_dlp.utils.DownloadCancelled('download canceled by /interrupt')
|
|
||||||
|
|
||||||
status = d.get('status')
|
|
||||||
info = d.get('info_dict') or {}
|
|
||||||
|
|
||||||
if status not in {'downloading', 'finished'}:
|
|
||||||
return
|
|
||||||
|
|
||||||
filename = d.get('filename') or info.get('_filename') or info.get('title')
|
|
||||||
update_key = (status, filename)
|
|
||||||
if update_key in seen_updates:
|
|
||||||
return
|
|
||||||
|
|
||||||
seen_updates.add(update_key)
|
|
||||||
playlist_index = info.get('playlist_index')
|
|
||||||
playlist_count = info.get('playlist_count')
|
|
||||||
url = info.get('webpage_url')
|
|
||||||
|
|
||||||
prefix = status
|
|
||||||
if playlist_index and playlist_count:
|
|
||||||
prefix = f'{status} {playlist_index} of {playlist_count}'
|
|
||||||
|
|
||||||
msg = f'{prefix}: {filename}'
|
|
||||||
if url:
|
|
||||||
msg = f'{msg} <{url}>'
|
|
||||||
|
|
||||||
asyncio.run_coroutine_threadsafe(send_message(ctx, msg), loop)
|
|
||||||
|
|
||||||
return hook
|
|
||||||
|
|
||||||
@interactions.slash_command(name="youtube",description="download video from youtube to server")
|
|
||||||
@interactions.slash_option(
|
|
||||||
name='url',
|
|
||||||
opt_type=interactions.OptionType.STRING,
|
|
||||||
required=True,
|
|
||||||
description='url target'
|
|
||||||
)
|
|
||||||
async def youtube(ctx: interactions.SlashContext, url:str):
|
|
||||||
print(f'{ctx.author.id} requested {url}')
|
|
||||||
# check that user is authorized
|
|
||||||
if str(ctx.author.id) not in authorized_users:
|
|
||||||
if ctx.author.id == 127831327012683776:
|
|
||||||
await ctx.author.send('potato stop')
|
|
||||||
await ctx.author.send('you are not authorized to use this command. message my owner to be added.')
|
|
||||||
return
|
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
|
||||||
cancel_event = threading.Event()
|
|
||||||
hook = create_hook(ctx, loop, cancel_event)
|
|
||||||
job = {
|
|
||||||
'requester_id': str(ctx.author.id),
|
|
||||||
'request_url': url,
|
|
||||||
'cancel_event': cancel_event,
|
|
||||||
}
|
|
||||||
existing_job = claim_active_job(job)
|
|
||||||
if existing_job:
|
|
||||||
await ctx.author.send(
|
|
||||||
f'already downloading for <@{existing_job["requester_id"]}>. '
|
|
||||||
'single-job mode is enabled right now; try again after it finishes.'
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
# use api_to_cli and paste cli options to get the output you need
|
|
||||||
yoptions = {
|
|
||||||
'format':'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
|
||||||
'fragment_tries': 10,
|
|
||||||
'restrictfilenames':True,
|
|
||||||
'paths': {'home':'/downloads'},
|
|
||||||
'retries':10,
|
|
||||||
'writeinfojson':False,
|
|
||||||
'allow_playlist_files':True,
|
|
||||||
'noplaylist':True,
|
|
||||||
'download_archive':'/config/archive.txt',
|
|
||||||
'progress_hooks':[hook],
|
|
||||||
'outtmpl': '%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s',
|
|
||||||
'outtmpl_na_placeholder':'',
|
|
||||||
}
|
|
||||||
await ctx.channel.send(f'Downloading from <{url}>. Status updates via DM. Single-job mode is enabled.')
|
|
||||||
|
|
||||||
try:
|
|
||||||
await asyncio.to_thread(download_video, url, yoptions)
|
|
||||||
except yt_dlp.utils.DownloadCancelled as exc:
|
|
||||||
print(f'download canceled: {exc}')
|
|
||||||
await ctx.author.send(f'download canceled: {exc}')
|
|
||||||
except yt_dlp.utils.DownloadError as exc:
|
|
||||||
print(f'download failed: {exc}')
|
|
||||||
await ctx.author.send(f'download failed: {exc}')
|
|
||||||
except Exception as exc:
|
|
||||||
print(f'unexpected download failure: {exc}')
|
|
||||||
await ctx.author.send(f'unexpected download failure: {exc}')
|
|
||||||
else:
|
|
||||||
await ctx.author.send(f'download complete for <{url}>')
|
|
||||||
finally:
|
|
||||||
clear_active_job(job)
|
|
||||||
|
|
||||||
|
|
||||||
@interactions.slash_command(name="interrupt",description="cancel current job")
|
if __name__ == "__main__":
|
||||||
@interactions.check(interactions.is_owner())
|
main()
|
||||||
async def _interrupt(ctx):
|
|
||||||
job = get_active_job()
|
|
||||||
if not job:
|
|
||||||
await ctx.author.send('no active download to interrupt')
|
|
||||||
return
|
|
||||||
|
|
||||||
job['cancel_event'].set()
|
|
||||||
print(f'interrupt requested for {job["request_url"]}')
|
|
||||||
await ctx.author.send(
|
|
||||||
f'interrupt requested for <{job["request_url"]}>; '
|
|
||||||
'cancellation is coarse and will stop on the next yt-dlp progress update'
|
|
||||||
)
|
|
||||||
|
|
||||||
@interactions.slash_command(name="adduser",description="authorize target user")
|
|
||||||
@interactions.slash_option(
|
|
||||||
name="user",
|
|
||||||
opt_type=interactions.OptionType.USER,
|
|
||||||
required=True,
|
|
||||||
description='enable this bot for target user',
|
|
||||||
)
|
|
||||||
@interactions.check(interactions.is_owner())
|
|
||||||
async def _adduser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
|
||||||
user_id = str(user.id)
|
|
||||||
if user_id not in authorized_users:
|
|
||||||
authorized_users.append(user_id)
|
|
||||||
save_authorized_users(authorized_users)
|
|
||||||
print(f'authorized {user_id}')
|
|
||||||
await ctx.author.send(f'authorized {user.mention}')
|
|
||||||
else:
|
|
||||||
await ctx.author.send(f'{user.mention} is already authorized')
|
|
||||||
|
|
||||||
@interactions.slash_command(name="removeuser",description="deauthorize target user")
|
|
||||||
@interactions.slash_option(
|
|
||||||
name="user",
|
|
||||||
opt_type=interactions.OptionType.USER,
|
|
||||||
required=True,
|
|
||||||
description='disable this bot for target user',
|
|
||||||
)
|
|
||||||
@interactions.check(interactions.is_owner())
|
|
||||||
async def _removeuser(ctx: interactions.SlashContext, user:interactions.OptionType.USER):
|
|
||||||
user_id = str(user.id)
|
|
||||||
if user_id in authorized_users:
|
|
||||||
authorized_users.remove(user_id)
|
|
||||||
save_authorized_users(authorized_users)
|
|
||||||
print(f'deauthorized {user_id}')
|
|
||||||
await ctx.author.send(f'deauthorized {user.mention}')
|
|
||||||
else:
|
|
||||||
await ctx.author.send(f'{user.mention} is not currently authorized')
|
|
||||||
|
|
||||||
api_token = getenv('api_token')
|
|
||||||
if not api_token:
|
|
||||||
raise ValueError('API token not set. Retrieve from your Discord bot.')
|
|
||||||
bot.start(api_token)
|
|
||||||
|
|||||||
1
youdis/__init__.py
Normal file
1
youdis/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""Youdis v2 backend package."""
|
||||||
1
youdis/adapters/__init__.py
Normal file
1
youdis/adapters/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""Frontend adapters for the youdis backend."""
|
||||||
228
youdis/adapters/discord.py
Normal file
228
youdis/adapters/discord.py
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
import asyncio
|
||||||
|
from os import getenv
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
import interactions
|
||||||
|
|
||||||
|
from ..env import load_project_dotenv
|
||||||
|
|
||||||
|
|
||||||
|
load_project_dotenv()
|
||||||
|
|
||||||
|
|
||||||
|
BACKEND_URL = getenv("YOUDIS_BACKEND_URL", "http://127.0.0.1:8000").rstrip("/")
|
||||||
|
POLL_INTERVAL_SECONDS = float(getenv("YOUDIS_POLL_INTERVAL_SECONDS", "2"))
|
||||||
|
DEFAULT_SCOPE = int(getenv("DISCORD_BOT_SCOPE", "2147491904"))
|
||||||
|
|
||||||
|
|
||||||
|
bot = interactions.Client(
|
||||||
|
intents=interactions.Intents.DEFAULT,
|
||||||
|
default_scope=DEFAULT_SCOPE,
|
||||||
|
)
|
||||||
|
http_session: aiohttp.ClientSession | None = None
|
||||||
|
poll_tasks: dict[str, asyncio.Task] = {}
|
||||||
|
|
||||||
|
|
||||||
|
def backend_url(path: str) -> str:
|
||||||
|
return f"{BACKEND_URL}{path}"
|
||||||
|
|
||||||
|
|
||||||
|
async def get_session() -> aiohttp.ClientSession:
|
||||||
|
global http_session
|
||||||
|
if http_session is None or http_session.closed:
|
||||||
|
http_session = aiohttp.ClientSession()
|
||||||
|
return http_session
|
||||||
|
|
||||||
|
|
||||||
|
async def request_json(method: str, path: str, **kwargs):
|
||||||
|
session = await get_session()
|
||||||
|
async with session.request(method, backend_url(path), **kwargs) as response:
|
||||||
|
data = await response.json()
|
||||||
|
return response.status, data
|
||||||
|
|
||||||
|
|
||||||
|
def format_status_message(job: dict) -> str:
|
||||||
|
state = job.get("state")
|
||||||
|
phase = job.get("phase")
|
||||||
|
disposition = job.get("disposition")
|
||||||
|
message = job.get("message")
|
||||||
|
result_path = job.get("result_path")
|
||||||
|
|
||||||
|
parts = [f"state={state}"]
|
||||||
|
if phase:
|
||||||
|
parts.append(f"phase={phase}")
|
||||||
|
if disposition:
|
||||||
|
parts.append(f"disposition={disposition}")
|
||||||
|
if message:
|
||||||
|
parts.append(message)
|
||||||
|
if result_path:
|
||||||
|
parts.append(f"path={result_path}")
|
||||||
|
return " | ".join(parts)
|
||||||
|
|
||||||
|
|
||||||
|
async def dm(ctx: interactions.SlashContext, message: str) -> None:
|
||||||
|
await ctx.author.send(message)
|
||||||
|
|
||||||
|
|
||||||
|
async def respond(ctx: interactions.SlashContext, message: str) -> None:
|
||||||
|
if ctx.channel is not None:
|
||||||
|
await ctx.channel.send(message)
|
||||||
|
return
|
||||||
|
await dm(ctx, message)
|
||||||
|
|
||||||
|
|
||||||
|
async def poll_job_updates(ctx: interactions.SlashContext, job_id: str) -> None:
|
||||||
|
last_sent = None
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
status_code, payload = await request_json("GET", "/jobs/current")
|
||||||
|
if status_code != 200:
|
||||||
|
await dm(ctx, f"backend status check failed: HTTP {status_code}")
|
||||||
|
return
|
||||||
|
|
||||||
|
job = payload.get("job")
|
||||||
|
if not job:
|
||||||
|
await dm(ctx, f"job {job_id} is no longer visible from the backend")
|
||||||
|
return
|
||||||
|
|
||||||
|
if job.get("job_id") != job_id:
|
||||||
|
await dm(ctx, f"job {job_id} is no longer the current backend job")
|
||||||
|
return
|
||||||
|
|
||||||
|
summary = format_status_message(job)
|
||||||
|
if summary != last_sent:
|
||||||
|
await dm(ctx, summary)
|
||||||
|
last_sent = summary
|
||||||
|
|
||||||
|
if job.get("state") in {"completed", "failed", "cancelled"}:
|
||||||
|
return
|
||||||
|
|
||||||
|
await asyncio.sleep(POLL_INTERVAL_SECONDS)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
raise
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
await dm(ctx, f"backend poll failed: {exc}")
|
||||||
|
finally:
|
||||||
|
poll_tasks.pop(job_id, None)
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_poll_task(ctx: interactions.SlashContext, job_id: str) -> None:
|
||||||
|
existing = poll_tasks.get(job_id)
|
||||||
|
if existing and not existing.done():
|
||||||
|
return
|
||||||
|
poll_tasks[job_id] = asyncio.create_task(poll_job_updates(ctx, job_id))
|
||||||
|
|
||||||
|
@bot.listen()
|
||||||
|
async def on_startup():
|
||||||
|
await get_session()
|
||||||
|
print(f"discord adapter configured for backend {BACKEND_URL}")
|
||||||
|
print(f"discord adapter default scope: {DEFAULT_SCOPE}")
|
||||||
|
print(f"discord adapter command cache keys: {sorted(bot._interaction_lookup.keys())}")
|
||||||
|
|
||||||
|
@bot.listen()
|
||||||
|
async def on_ready():
|
||||||
|
print(f"registered commands: {bot.application_commands}")
|
||||||
|
|
||||||
|
@bot.listen()
|
||||||
|
async def on_shutdown():
|
||||||
|
global http_session
|
||||||
|
for task in list(poll_tasks.values()):
|
||||||
|
task.cancel()
|
||||||
|
poll_tasks.clear()
|
||||||
|
if http_session is not None and not http_session.closed:
|
||||||
|
await http_session.close()
|
||||||
|
http_session = None
|
||||||
|
|
||||||
|
|
||||||
|
@interactions.slash_command(name="youtube", description="submit a youtube download to the backend")
|
||||||
|
@interactions.slash_option(
|
||||||
|
name="url",
|
||||||
|
opt_type=interactions.OptionType.STRING,
|
||||||
|
required=True,
|
||||||
|
description="url target",
|
||||||
|
)
|
||||||
|
async def youtube(ctx: interactions.SlashContext, url: str):
|
||||||
|
payload = {
|
||||||
|
"url": url,
|
||||||
|
"requester_id": str(ctx.author.id),
|
||||||
|
"requester_name": ctx.author.username,
|
||||||
|
"origin": f"discord:{ctx.guild_id or 'dm'}:{ctx.channel_id}",
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
status_code, job = await request_json("POST", "/jobs", json=payload)
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
await dm(ctx, f"backend request failed: {exc}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if status_code != 200:
|
||||||
|
await dm(ctx, f"backend request failed: HTTP {status_code}")
|
||||||
|
return
|
||||||
|
|
||||||
|
state = job.get("state")
|
||||||
|
job_id = job.get("job_id", "unknown")
|
||||||
|
if state == "busy":
|
||||||
|
await respond(ctx, "Backend is busy with another job. Details via DM.")
|
||||||
|
await dm(ctx, f"busy: {job.get('message')}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if state != "accepted":
|
||||||
|
await respond(ctx, "Backend rejected the request. Details via DM.")
|
||||||
|
await dm(ctx, format_status_message(job))
|
||||||
|
return
|
||||||
|
|
||||||
|
await respond(ctx, f"Submitted <{url}> to the backend. Status updates via DM.")
|
||||||
|
await dm(ctx, f"accepted job {job_id} for <{url}>")
|
||||||
|
ensure_poll_task(ctx, job_id)
|
||||||
|
|
||||||
|
|
||||||
|
@interactions.slash_command(name="interrupt", description="cancel the current backend job")
|
||||||
|
@interactions.check(interactions.is_owner())
|
||||||
|
async def interrupt(ctx: interactions.SlashContext):
|
||||||
|
try:
|
||||||
|
status_code, payload = await request_json("POST", "/jobs/current/cancel")
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
await dm(ctx, f"backend cancel failed: {exc}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if status_code == 404:
|
||||||
|
await dm(ctx, "no active backend job to interrupt")
|
||||||
|
return
|
||||||
|
|
||||||
|
if status_code != 200:
|
||||||
|
await dm(ctx, f"backend cancel failed: HTTP {status_code}")
|
||||||
|
return
|
||||||
|
|
||||||
|
await dm(ctx, format_status_message(payload))
|
||||||
|
|
||||||
|
|
||||||
|
@interactions.slash_command(name="status", description="show the current backend job status")
|
||||||
|
async def status(ctx: interactions.SlashContext):
|
||||||
|
try:
|
||||||
|
status_code, payload = await request_json("GET", "/jobs/current")
|
||||||
|
except aiohttp.ClientError as exc:
|
||||||
|
await dm(ctx, f"backend status failed: {exc}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if status_code != 200:
|
||||||
|
await dm(ctx, f"backend status failed: HTTP {status_code}")
|
||||||
|
return
|
||||||
|
|
||||||
|
job = payload.get("job")
|
||||||
|
if not job:
|
||||||
|
await dm(ctx, "backend has no active or recent job")
|
||||||
|
return
|
||||||
|
|
||||||
|
active = payload.get("active")
|
||||||
|
prefix = "active" if active else "last"
|
||||||
|
await dm(ctx, f"{prefix} job: {format_status_message(job)}")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
api_token = getenv("DISCORD_BOT_TOKEN")
|
||||||
|
if not api_token:
|
||||||
|
raise ValueError("API token not set. Retrieve from your Discord bot.")
|
||||||
|
bot.add_command(youtube)
|
||||||
|
bot.add_command(status)
|
||||||
|
bot.add_command(interrupt)
|
||||||
|
bot.start(api_token)
|
||||||
8
youdis/env.py
Normal file
8
youdis/env.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
|
||||||
|
def load_project_dotenv() -> None:
|
||||||
|
repo_root = Path(__file__).resolve().parent.parent
|
||||||
|
load_dotenv(repo_root / ".env", override=False)
|
||||||
309
youdis/main.py
Normal file
309
youdis/main.py
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
import asyncio
|
||||||
|
from asyncio.subprocess import PIPE, STDOUT
|
||||||
|
from collections import deque
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from datetime import datetime
|
||||||
|
from os import getenv
|
||||||
|
from pathlib import Path
|
||||||
|
import tempfile
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from fastapi import FastAPI, HTTPException
|
||||||
|
|
||||||
|
from .env import load_project_dotenv
|
||||||
|
from .models import CurrentJobResponse, HealthResponse, JobRequest, JobStatus, VersionResponse
|
||||||
|
|
||||||
|
|
||||||
|
load_project_dotenv()
|
||||||
|
|
||||||
|
|
||||||
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
DEFAULT_CONFIG = REPO_ROOT / "default-yt-dlp.conf"
|
||||||
|
VERSION_FILE = REPO_ROOT / "youdis-version.txt"
|
||||||
|
YTDLP_EXECUTABLE = getenv("YOUDIS_YTDLP_EXECUTABLE", "yt-dlp")
|
||||||
|
PREFERRED_CONFIG_ROOT = Path(getenv("YOUDIS_CONFIG_DIR", "/config"))
|
||||||
|
PREFERRED_DOWNLOAD_ROOT = Path(getenv("YOUDIS_DOWNLOAD_DIR", "/downloads"))
|
||||||
|
LOCAL_RUNTIME_ROOT = REPO_ROOT / ".runtime"
|
||||||
|
FALLBACK_CONFIG_ROOT = LOCAL_RUNTIME_ROOT / "config"
|
||||||
|
FALLBACK_DOWNLOAD_ROOT = LOCAL_RUNTIME_ROOT / "downloads"
|
||||||
|
OUTPUT_TEMPLATE = "%(uploader)s/%(playlist_title)s/%(playlist_index)s%(playlist_index& - )s%(title)s.%(ext)s"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ManagedJob:
|
||||||
|
status: JobStatus
|
||||||
|
process: asyncio.subprocess.Process | None = None
|
||||||
|
task: asyncio.Task | None = None
|
||||||
|
cancel_requested: bool = False
|
||||||
|
output_lines: deque[str] = field(default_factory=lambda: deque(maxlen=25))
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(title="youdis", version="2")
|
||||||
|
job_lock = asyncio.Lock()
|
||||||
|
active_job: ManagedJob | None = None
|
||||||
|
last_job: JobStatus | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def now_utc() -> datetime:
|
||||||
|
return datetime.now()
|
||||||
|
|
||||||
|
|
||||||
|
def read_version() -> str:
|
||||||
|
if VERSION_FILE.exists():
|
||||||
|
return VERSION_FILE.read_text().strip()
|
||||||
|
return "unknown"
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_writable_directory(preferred: Path, fallback: Path) -> Path:
|
||||||
|
for candidate in (preferred, fallback):
|
||||||
|
try:
|
||||||
|
candidate.mkdir(parents=True, exist_ok=True)
|
||||||
|
with tempfile.NamedTemporaryFile(dir=candidate, prefix=".youdis-write-", delete=True):
|
||||||
|
pass
|
||||||
|
return candidate
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
raise OSError(f"no writable runtime directory available for {preferred} or {fallback}")
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_runtime_paths() -> tuple[Path, Path]:
|
||||||
|
config_root = ensure_writable_directory(PREFERRED_CONFIG_ROOT, FALLBACK_CONFIG_ROOT)
|
||||||
|
download_root = ensure_writable_directory(PREFERRED_DOWNLOAD_ROOT, FALLBACK_DOWNLOAD_ROOT)
|
||||||
|
return config_root, download_root
|
||||||
|
|
||||||
|
|
||||||
|
def build_ytdlp_command(request: JobRequest, config_root: Path, download_root: Path) -> list[str]:
|
||||||
|
return [
|
||||||
|
YTDLP_EXECUTABLE,
|
||||||
|
"--config-locations",
|
||||||
|
str(DEFAULT_CONFIG),
|
||||||
|
"--download-archive",
|
||||||
|
str(config_root / "archive.txt"),
|
||||||
|
"--output",
|
||||||
|
str(download_root / OUTPUT_TEMPLATE),
|
||||||
|
request.url,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def clone_status(status: JobStatus) -> JobStatus:
|
||||||
|
return JobStatus(**status.model_dump())
|
||||||
|
|
||||||
|
|
||||||
|
def update_status(job: ManagedJob, **changes: object) -> None:
|
||||||
|
for key, value in changes.items():
|
||||||
|
setattr(job.status, key, value)
|
||||||
|
job.status.updated_at = now_utc()
|
||||||
|
|
||||||
|
|
||||||
|
def classify_output_line(job: ManagedJob, line: str) -> None:
|
||||||
|
if not line:
|
||||||
|
return
|
||||||
|
|
||||||
|
job.output_lines.append(line)
|
||||||
|
message = line.strip()
|
||||||
|
if not message:
|
||||||
|
return
|
||||||
|
|
||||||
|
lowered = message.lower()
|
||||||
|
if "has already been recorded in the archive" in lowered:
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
disposition="archive_hit",
|
||||||
|
phase="downloading",
|
||||||
|
message="already in archive",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if "[download]" in lowered:
|
||||||
|
update_status(job, phase="downloading", message=message)
|
||||||
|
if "destination:" in lowered:
|
||||||
|
result_path = message.split(":", 1)[1].strip()
|
||||||
|
update_status(job, result_path=result_path)
|
||||||
|
return
|
||||||
|
|
||||||
|
if "merging formats into" in lowered:
|
||||||
|
result_path = message.split("into", 1)[1].strip().strip('"')
|
||||||
|
update_status(job, phase="postprocessing", message=message, result_path=result_path)
|
||||||
|
return
|
||||||
|
|
||||||
|
update_status(job, message=message)
|
||||||
|
|
||||||
|
|
||||||
|
async def finalize_job(job: ManagedJob, returncode: int) -> None:
|
||||||
|
disposition = job.status.disposition
|
||||||
|
if job.cancel_requested:
|
||||||
|
state = "cancelled"
|
||||||
|
message = "cancelled"
|
||||||
|
elif returncode == 0 and disposition == "archive_hit":
|
||||||
|
state = "completed"
|
||||||
|
message = "already in archive"
|
||||||
|
elif returncode == 0:
|
||||||
|
state = "completed"
|
||||||
|
message = job.status.message or "completed"
|
||||||
|
else:
|
||||||
|
state = "failed"
|
||||||
|
message = job.status.message or f"yt-dlp exited with code {returncode}"
|
||||||
|
|
||||||
|
update_status(job, state=state, phase=None, returncode=returncode, message=message)
|
||||||
|
|
||||||
|
global active_job, last_job
|
||||||
|
async with job_lock:
|
||||||
|
if active_job is job:
|
||||||
|
active_job = None
|
||||||
|
last_job = clone_status(job.status)
|
||||||
|
|
||||||
|
|
||||||
|
async def run_job(job: ManagedJob, request: JobRequest) -> None:
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
state="running",
|
||||||
|
phase="starting",
|
||||||
|
message="starting yt-dlp",
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not DEFAULT_CONFIG.exists():
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
state="failed",
|
||||||
|
phase=None,
|
||||||
|
message=f"default config not found: {DEFAULT_CONFIG}",
|
||||||
|
returncode=78,
|
||||||
|
)
|
||||||
|
await finalize_job(job, 78)
|
||||||
|
return
|
||||||
|
|
||||||
|
config_root, download_root = resolve_runtime_paths()
|
||||||
|
command = build_ytdlp_command(request, config_root, download_root)
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
command=command,
|
||||||
|
archive_path=str(config_root / "archive.txt"),
|
||||||
|
result_path=str(download_root),
|
||||||
|
)
|
||||||
|
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
*command,
|
||||||
|
stdout=PIPE,
|
||||||
|
stderr=STDOUT,
|
||||||
|
)
|
||||||
|
except FileNotFoundError:
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
state="failed",
|
||||||
|
phase=None,
|
||||||
|
message="yt-dlp executable not found",
|
||||||
|
returncode=127,
|
||||||
|
)
|
||||||
|
await finalize_job(job, 127)
|
||||||
|
return
|
||||||
|
except OSError as exc:
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
state="failed",
|
||||||
|
phase=None,
|
||||||
|
message=f"runtime path setup failed: {exc}",
|
||||||
|
returncode=73,
|
||||||
|
)
|
||||||
|
await finalize_job(job, 73)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
job.process = process
|
||||||
|
update_status(job, phase="running", message="yt-dlp running")
|
||||||
|
|
||||||
|
assert process.stdout is not None
|
||||||
|
while True:
|
||||||
|
line = await process.stdout.readline()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
classify_output_line(job, line.decode(errors="replace").strip())
|
||||||
|
|
||||||
|
returncode = await process.wait()
|
||||||
|
await finalize_job(job, returncode)
|
||||||
|
except Exception as exc:
|
||||||
|
update_status(
|
||||||
|
job,
|
||||||
|
state="failed",
|
||||||
|
phase=None,
|
||||||
|
message=f"backend runner error: {exc}",
|
||||||
|
returncode=1,
|
||||||
|
)
|
||||||
|
await finalize_job(job, 1)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_job(request: JobRequest) -> JobStatus:
|
||||||
|
global active_job
|
||||||
|
async with job_lock:
|
||||||
|
if active_job is not None:
|
||||||
|
busy_job = active_job.status
|
||||||
|
return JobStatus(
|
||||||
|
job_id=busy_job.job_id,
|
||||||
|
state="busy",
|
||||||
|
url=request.url,
|
||||||
|
message=f"busy with {busy_job.url}",
|
||||||
|
requester_id=request.requester_id,
|
||||||
|
requester_name=request.requester_name,
|
||||||
|
origin=request.origin,
|
||||||
|
)
|
||||||
|
|
||||||
|
status = JobStatus(
|
||||||
|
job_id=str(uuid4()),
|
||||||
|
state="accepted",
|
||||||
|
url=request.url,
|
||||||
|
message="accepted",
|
||||||
|
phase="queued",
|
||||||
|
requester_id=request.requester_id,
|
||||||
|
requester_name=request.requester_name,
|
||||||
|
origin=request.origin,
|
||||||
|
)
|
||||||
|
managed_job = ManagedJob(status=status)
|
||||||
|
managed_job.task = asyncio.create_task(run_job(managed_job, request))
|
||||||
|
active_job = managed_job
|
||||||
|
return clone_status(status)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health", response_model=HealthResponse)
|
||||||
|
async def health() -> HealthResponse:
|
||||||
|
return HealthResponse(status="ok")
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/version", response_model=VersionResponse)
|
||||||
|
async def version() -> VersionResponse:
|
||||||
|
return VersionResponse(
|
||||||
|
version=read_version(),
|
||||||
|
active_job=active_job is not None,
|
||||||
|
ytdlp_executable=YTDLP_EXECUTABLE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/jobs/current", response_model=CurrentJobResponse)
|
||||||
|
async def current_job() -> CurrentJobResponse:
|
||||||
|
async with job_lock:
|
||||||
|
if active_job is not None:
|
||||||
|
return CurrentJobResponse(active=True, job=clone_status(active_job.status))
|
||||||
|
if last_job is not None:
|
||||||
|
return CurrentJobResponse(active=False, job=clone_status(last_job))
|
||||||
|
return CurrentJobResponse(active=False, job=None)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/jobs", response_model=JobStatus)
|
||||||
|
async def submit_job(request: JobRequest) -> JobStatus:
|
||||||
|
return await create_job(request)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/jobs/current/cancel", response_model=JobStatus)
|
||||||
|
async def cancel_current_job() -> JobStatus:
|
||||||
|
async with job_lock:
|
||||||
|
if active_job is None:
|
||||||
|
raise HTTPException(status_code=404, detail="no active job")
|
||||||
|
job = active_job
|
||||||
|
if job.process is None:
|
||||||
|
update_status(job, message="cancel requested before yt-dlp started")
|
||||||
|
job.cancel_requested = True
|
||||||
|
return clone_status(job.status)
|
||||||
|
|
||||||
|
job.cancel_requested = True
|
||||||
|
update_status(job, message="cancel requested", phase="running")
|
||||||
|
job.process.terminate()
|
||||||
|
return clone_status(job.status)
|
||||||
48
youdis/models.py
Normal file
48
youdis/models.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
|
JobState = Literal["accepted", "busy", "running", "completed", "failed", "cancelled"]
|
||||||
|
|
||||||
|
|
||||||
|
class JobRequest(BaseModel):
|
||||||
|
url: str
|
||||||
|
requester_id: str | None = None
|
||||||
|
requester_name: str | None = None
|
||||||
|
origin: str | None = None
|
||||||
|
requested_at: datetime | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class JobStatus(BaseModel):
|
||||||
|
job_id: str
|
||||||
|
state: JobState
|
||||||
|
url: str
|
||||||
|
message: str | None = None
|
||||||
|
phase: str | None = None
|
||||||
|
disposition: str | None = None
|
||||||
|
requester_id: str | None = None
|
||||||
|
requester_name: str | None = None
|
||||||
|
origin: str | None = None
|
||||||
|
result_path: str | None = None
|
||||||
|
archive_path: str | None = None
|
||||||
|
command: list[str] = Field(default_factory=list)
|
||||||
|
returncode: int | None = None
|
||||||
|
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||||
|
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||||
|
|
||||||
|
|
||||||
|
class CurrentJobResponse(BaseModel):
|
||||||
|
active: bool
|
||||||
|
job: JobStatus | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class HealthResponse(BaseModel):
|
||||||
|
status: Literal["ok"]
|
||||||
|
|
||||||
|
|
||||||
|
class VersionResponse(BaseModel):
|
||||||
|
version: str
|
||||||
|
active_job: bool
|
||||||
|
ytdlp_executable: str
|
||||||
Reference in New Issue
Block a user