# subhamsarangi/yt-collector-backend architecture

> Architecture diagram of the GitHub repository subhamsarangi/yt-collector-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/subhamsarangi/yt-collector-backend
- Repository: https://github.com/subhamsarangi/yt-collector-backend

## Overview

1. **Overall shape**
   - This repository is a small FastAPI backend that acts as a media-processing gateway for YouTube content.
   - The app receives authenticated API requests, runs YouTube scraping/download workflows, stores artifacts in Cloudflare R2, and generates PDFs on demand.

2. **Request entrypoint and API surface**
   - `main.py` is the FastAPI application entrypoint and route registrar.
   - The HTTP API is split by concern into `routers/video.py`, `routers/jobs.py`, and `routers/pdf.py`.
   - Publicly exposed endpoints are centered on video ingestion, channel scanning, search, job polling, metadata retrieval, and PDF generation.

3. **Authentication boundary**
   - `auth.py` provides a shared bearer-token check using `OCI_API_KEY`.
   - Every API endpoint is protected by this dependency, so the backend is effectively a private service meant to be called from a trusted frontend or orchestrator.

4. **Video ingestion pipeline**
   - `routers/video.py` is the main workflow surface for `/video`, `/channel/scan`, and `/search`.
   - These routes orchestrate YouTube discovery and download work, then hand off to service code for extraction and storage.
   - The route layer stays thin; the actual scraping/downloading logic lives in `services/ytdlp.py`.

5. **YouTube integration**
   - `services/ytdlp.py` wraps `yt-dlp` operations.
   - It is responsible for pulling video metadata, thumbnails, and audio, and likely for channel/topic discovery used by the scan and search endpoints.
   - The README also indicates `ffmpeg` and the `yt-dlp` binary are runtime dependencies, so media extraction is done outside Python as part of the system environment.

6. **Object storage boundary**
   - `services/r2.py` is the persistence layer for Cloudflare R2.
   - This service uploads and deletes generated assets, acting as the backend’s durable storage for downloaded media and derived artifacts.
   - R2 is the main external storage system; the API itself does not appear to persist application state locally beyond transient job handling.

7. **PDF generation pipeline**
   - `routers/pdf.py` exposes `/pdf/video/{id}` and `/pdf/topic/{id}` for on-demand document generation.
   - `services/pdf.py` contains the rendering logic, and the README shows WeasyPrint is used at runtime.
   - PDF generation appears to depend on data already fetched or stored elsewhere, rather than re-scraping everything from scratch.

8. **Job status and metadata access**
   - `routers/jobs.py` provides `/job/{id}` and `/job/{id}/metadata`.
   - This is the async boundary for longer-running work such as downloads, uploads, or PDF generation.
   - The architecture implies jobs are tracked separately from the immediate request/response cycle so the frontend can poll for completion.

9. **Supporting scripts and operational utilities**
   - `poller.py` suggests a background or CLI polling utility for job processing or status checks.
   - `check_models_groq.py` appears to be a validation or model-checking script, likely used for experimentation or integration checks rather than serving API traffic.
   - `colab/whisper_transcriber.ipynb` is clearly an exploratory notebook, not part of the production request path.

10. **Deployment and runtime environment**
   - The backend is designed to run on a small OCI Ubuntu VM with `uvicorn`, managed by systemd in production.
   - System dependencies matter to the architecture: `ffmpeg` for media extraction, WeasyPrint libraries for PDFs, and the `yt-dlp` binary for scraping.
   - Network access is intentionally narrow: the service listens on port 8000 and is expected to be protected by bearer auth and possibly source-IP restrictions.

11. **External systems and data dependencies**
   - Cloudflare R2 is the primary file storage backend.
   - Supabase is referenced in the README as a data source used for PDF generation, so PDFs likely combine YouTube-derived data with application data from Supabase.
   - The service is not standalone; it is a processing layer between a frontend/orchestrator and these external systems.

12. **How to draw the architecture**
   - Model it as four layers: authenticated HTTP API, orchestration/routing, media/PDF/storage services, and external systems.
   - The dominant flows are: request in → auth → router → yt-dlp/PDF/R2 service → external storage/data source → job result/metadata out.
   - The job endpoints form the main asynchronous feedback loop, while `/health` is the only simple liveness endpoint.

Main entry points: [`routers/video.py`](https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/video.py) (Video API), [`main.py`](https://github.com/subhamsarangi/yt-collector-backend/blob/main/main.py) (App entry), [`services/ytdlp.py`](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/ytdlp.py) (yt-dlp service), [`services/r2.py`](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/r2.py) (R2 storage)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Authenticated API"]
  node_main["App entry<br/>FastAPI app<br/>[main.py]"]
  node_auth["Auth guard<br/>bearer check<br/>[auth.py]"]
  node_video_router["Video API<br/>router<br/>[video.py]"]
  node_jobs_router["Job API<br/>router<br/>[jobs.py]"]
  node_pdf_router["PDF API<br/>router<br/>[pdf.py]"]
end

subgraph group_svc["Core services"]
  node_ytdlp["yt-dlp service<br/>media pipeline<br/>[ytdlp.py]"]
  node_r2[("R2 storage<br/>object storage<br/>[r2.py]")]
  node_pdf["PDF renderer<br/>document pipeline<br/>[pdf.py]"]
  node_search["Search service<br/>search helper<br/>[search.py]"]
  node_summarize["Summary service<br/>text pipeline<br/>[summarize.py]"]
end

subgraph group_ext["External systems"]
  node_youtube(("YouTube<br/>external source"))
  node_r2_ext[("Cloudflare R2<br/>external storage")]
  node_supabase[("Supabase<br/>external data")]
  node_runtime{{"Media tools<br/>runtime deps"}}
end

subgraph group_ops["Ops and tools"]
  node_poller["Poller<br/>job utility<br/>[poller.py]"]
  node_check_models["Model check<br/>validation script"]
  node_notebook["Notebook<br/>exploration"]
end

node_main -->|"secures"| node_auth
node_main -->|"registers"| node_video_router
node_main -->|"registers"| node_jobs_router
node_main -->|"registers"| node_pdf_router
node_video_router -->|"depends on"| node_auth
node_jobs_router -->|"depends on"| node_auth
node_pdf_router -->|"depends on"| node_auth
node_video_router -->|"orchestrates"| node_ytdlp
node_video_router -->|"uses"| node_search
node_video_router -->|"stores"| node_r2
node_pdf_router -->|"renders"| node_pdf
node_pdf -->|"reads"| node_supabase
node_pdf -->|"reads/writes"| node_r2
node_ytdlp -->|"fetches"| node_youtube
node_ytdlp -.->|"relies on"| node_runtime
node_r2 -->|"uploads"| node_r2_ext
node_jobs_router -.->|"surfaces"| node_poller
node_poller -.->|"monitors"| node_r2
node_check_models -.->|"validates"| node_runtime
node_notebook -.->|"experiments with"| node_ytdlp
node_notebook -.->|"requires"| node_runtime

click node_main "https://github.com/subhamsarangi/yt-collector-backend/blob/main/main.py"
click node_auth "https://github.com/subhamsarangi/yt-collector-backend/blob/main/auth.py"
click node_video_router "https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/video.py"
click node_jobs_router "https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/jobs.py"
click node_pdf_router "https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/pdf.py"
click node_ytdlp "https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/ytdlp.py"
click node_r2 "https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/r2.py"
click node_pdf "https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/pdf.py"
click node_search "https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/search.py"
click node_summarize "https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/summarize.py"
click node_poller "https://github.com/subhamsarangi/yt-collector-backend/blob/main/poller.py"
click node_check_models "https://github.com/subhamsarangi/yt-collector-backend/blob/main/check_models_groq.py"
click node_notebook "https://github.com/subhamsarangi/yt-collector-backend/blob/main/colab/whisper_transcriber.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_auth,node_video_router,node_jobs_router,node_pdf_router toneBlue
class node_ytdlp,node_r2,node_pdf,node_search,node_summarize toneAmber
class node_youtube,node_r2_ext,node_supabase,node_runtime toneMint
class node_poller,node_check_models,node_notebook toneRose
```

## Components

### Authenticated API

FastAPI entrypoint and route surfaces protected by shared bearer auth.

- [App entry](https://github.com/subhamsarangi/yt-collector-backend/blob/main/main.py): FastAPI app · Creates the API application and registers routers and middleware boundaries. · `main.py`
- [Auth guard](https://github.com/subhamsarangi/yt-collector-backend/blob/main/auth.py): bearer check · Shared API-key authentication dependency used by protected endpoints. · `auth.py`
- [Video API](https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/video.py): router · Endpoints for video ingestion, channel scan, and search workflows. · `routers/video.py`
- [Job API](https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/jobs.py): router · Polling and metadata endpoints for long-running tasks. · `routers/jobs.py`
- [PDF API](https://github.com/subhamsarangi/yt-collector-backend/blob/main/routers/pdf.py): router · On-demand PDF generation endpoints for video and topic documents. · `routers/pdf.py`

### Core services

Media, storage, PDF, search, and summarization logic.

- [yt-dlp service](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/ytdlp.py): media pipeline · Wraps YouTube scraping, metadata retrieval, thumbnail/audio extraction, and discovery flows. · `services/ytdlp.py`
- [R2 storage](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/r2.py): object storage · Uploads and deletes derived artifacts in Cloudflare R2. · `services/r2.py`
- [PDF renderer](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/pdf.py): document pipeline · Renders PDFs from fetched data and stored artifacts. · `services/pdf.py`
- [Search service](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/search.py): search helper · Search-oriented support logic used by the video workflows. · `services/search.py`
- [Summary service](https://github.com/subhamsarangi/yt-collector-backend/blob/main/services/summarize.py): text pipeline · Summarization logic used by downstream content workflows. · `services/summarize.py`

### External systems

Remote systems and runtime dependencies used by the backend.

- **YouTube**: external source · Primary upstream content source for discovery and downloads.
- **Cloudflare R2**: external storage · Durable object storage for downloaded media and generated assets.
- **Supabase**: external data · Referenced data source used during PDF generation.
- **Media tools**: runtime deps · System-level dependencies such as yt-dlp, ffmpeg, and WeasyPrint libraries.

### Ops and tools

Background utilities and exploratory scripts.

- [Poller](https://github.com/subhamsarangi/yt-collector-backend/blob/main/poller.py): job utility · Background or CLI utility for checking or advancing job state. · `poller.py`
- [Model check](https://github.com/subhamsarangi/yt-collector-backend/blob/main/check_models_groq.py): validation script · Ad hoc script for validating model connectivity or integration behavior. · `check_models_groq.py`
- [Notebook](https://github.com/subhamsarangi/yt-collector-backend/blob/main/colab/whisper_transcriber.ipynb): exploration · Experimental Whisper transcription notebook outside the production request path. · `colab/whisper_transcriber.ipynb`

## Connections

- App entry → Auth guard: secures · Applies shared bearer-token protection to the API surface.
- App entry → Video API: registers · Mounts the video workflow routes.
- App entry → Job API: registers · Mounts async job polling and metadata routes.
- App entry → PDF API: registers · Mounts PDF generation routes.
- Video API → Auth guard: depends on · Protects ingestion and discovery endpoints with the API key check.
- Job API → Auth guard: depends on · Protects job status and metadata endpoints.
- PDF API → Auth guard: depends on · Protects document generation endpoints.
- Video API → yt-dlp service: orchestrates · Delegates discovery and download work to the YouTube media pipeline.
- Video API → Search service: uses · Supports search-oriented video workflows.
- Video API → R2 storage: stores · Persists downloaded and derived media artifacts.
- PDF API → PDF renderer: renders · Delegates PDF document creation to the rendering service.
- PDF renderer → Supabase: reads · Combines PDF content with external application data.
- PDF renderer → R2 storage: reads/writes · Uses stored artifacts as PDF inputs and outputs.
- yt-dlp service → YouTube: fetches · Pulls metadata and media from YouTube.
- yt-dlp service → Media tools: relies on · Uses external media tooling during extraction and conversion.
- R2 storage → Cloudflare R2: uploads · Persists media and generated assets in Cloudflare R2.
- Job API → Poller: surfaces · Exposes the async job state tracked by background processing.
- Poller → R2 storage: monitors · Likely checks or advances jobs tied to stored artifacts.
- Model check → Media tools: validates · Script-level validation around external model/runtime access.
- Notebook → yt-dlp service: experiments with · Exploratory transcription workflow adjacent to the main media pipeline.
- Notebook → Media tools: requires · Notebook experimentation depends on the same media-processing environment.
