# librespark/libretv architecture

> Architecture diagram of the GitHub repository librespark/libretv, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/librespark/libretv
- Repository: https://github.com/librespark/libretv

## Overview

LibreTV is a Next.js video aggregation and playback platform. Users configure on-demand and live sources, search across selected providers, inspect and compare results, then watch or download episodes; separate workflows cover IPTV, recommendations, source subscriptions, and playback history. The graph follows documented features and relationships visible in the README or supplied source excerpts/index. Several route and UI implementations were not sampled, so some documented subsystem links are shown as unverified dashed edges.

Main entry points: [`src/lib/client-api.ts`](https://github.com/librespark/libretv/blob/main/src/lib/client-api.ts) (Client API), [`src/app/watch/page.tsx`](https://github.com/librespark/libretv/blob/main/src/app/watch/page.tsx) (Watch Page), [`src/app/api/search/route.ts`](https://github.com/librespark/libretv/blob/main/src/app/api/search/route.ts) (Aggregated Search), [`src/app/live/page.tsx`](https://github.com/librespark/libretv/blob/main/src/app/live/page.tsx) (Live Browser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_discovery["Discovery"]
  node_home["Home &amp; Search<br/>[page.tsx]"]
  node_search_api["Aggregated Search<br/>[route.ts]"]
  node_cms["CMS Parsing<br/>[cms-parser.ts]"]
  node_detail_api["Video Details<br/>[route.ts]"]
  node_recommendations["Recommendations<br/>[douban-section.tsx]"]
  node_recommend_api["Recommendation APIs<br/>[route.ts]"]
end

subgraph group_watching["Playback"]
  node_watch_page["Watch Page<br/>[page.tsx]"]
  node_player["Video Player<br/>[player-shell.tsx]"]
  node_switch_source["Source Switching<br/>[switch-source.tsx]"]
  node_download["Episode Downloads"]
  node_history[("Playback History<br/>[db.ts]")]
end

subgraph group_live["Live TV"]
  node_live_page["Live Browser<br/>[page.tsx]"]
  node_live_channels["Channel List"]
  node_live_player["Live Player<br/>[live-player.tsx]"]
  node_live_playlist["Playlist API<br/>[route.ts]"]
  node_epg["EPG API<br/>[route.ts]"]
  node_stream_proxy["Live Stream Proxy<br/>[route.ts]"]
end

subgraph group_sources["Sources &amp; Settings"]
  node_settings["Source Management<br/>[source-manager.tsx]"]
  node_live_settings["Live Sources"]
  node_subscriptions["Subscription Sync"]
  node_source_list["Source List Parsing<br/>[source-list.ts]"]
  node_tvbox["TVBOX Import<br/>[tvbox-parser.ts]"]
  node_app_state["App State<br/>[store.ts]"]
  node_local_settings[("Settings Storage<br/>[persist-storage.ts]")]
end

subgraph group_platform["Platform Services"]
  node_client_api["Client API<br/>[client-api.ts]"]
  node_auth["Authentication<br/>[auth.ts]"]
  node_api_guard["API Guard<br/>[api-guard.ts]"]
  node_proxy["Media Proxy<br/>[proxy-handlers.ts]"]
end

node_viewer(("Viewer"))
node_external_sources{{"Third-Party Sources"}}
node_external_recs{{"Recommendation Services"}}
node_browser[("Browser Storage")]

node_viewer -->|"searches"| node_home
node_home -->|"requests"| node_client_api
node_client_api -->|"posts search"| node_search_api
node_search_api -->|"parses results"| node_cms
node_search_api -->|"queries sources"| node_external_sources
node_home -.->|"loads details"| node_detail_api
node_client_api -->|"requests detail"| node_detail_api
node_viewer -->|"browses picks"| node_recommendations
node_recommendations -.->|"requests picks"| node_client_api
node_client_api -->|"requests recommendations"| node_recommend_api
node_recommend_api -->|"fetches data"| node_external_recs
node_viewer -->|"watches episodes"| node_watch_page
node_watch_page -->|"renders player"| node_player
node_watch_page -->|"offers switching"| node_switch_source
node_watch_page -->|"offers downloads"| node_download
node_watch_page -->|"saves progress"| node_history
node_history -->|"persists locally"| node_browser
node_viewer -->|"opens live TV"| node_live_page
node_live_page -.->|"shows channels"| node_live_channels
node_live_channels -.->|"loads playlists"| node_client_api
node_client_api -->|"requests playlist"| node_live_playlist
node_live_playlist -.->|"fetches playlist"| node_external_sources
node_live_page -.->|"plays channel"| node_live_player
node_live_player -->|"retries via proxy"| node_stream_proxy
node_live_page -.->|"loads schedules"| node_epg
node_viewer -->|"manages sources"| node_settings
node_settings -->|"renders live settings"| node_live_settings
node_settings -->|"updates settings"| node_app_state
node_settings -.->|"syncs subscriptions"| node_subscriptions
node_subscriptions -->|"fetches lists"| node_client_api
node_subscriptions -->|"parses lists"| node_source_list
node_tvbox -->|"normalizes imports"| node_source_list
node_app_state -->|"persists preferences"| node_local_settings
node_client_api -.->|"calls protected APIs"| node_api_guard
node_api_guard -->|"checks session"| node_auth
node_search_api -->|"guards requests"| node_api_guard
node_proxy -.->|"proxies media"| node_external_sources

click node_home "https://github.com/librespark/libretv/blob/main/src/app/page.tsx"
click node_search_api "https://github.com/librespark/libretv/blob/main/src/app/api/search/route.ts"
click node_cms "https://github.com/librespark/libretv/blob/main/src/lib/cms-parser.ts"
click node_detail_api "https://github.com/librespark/libretv/blob/main/src/app/api/detail/route.ts"
click node_recommendations "https://github.com/librespark/libretv/blob/main/src/components/douban-section.tsx"
click node_recommend_api "https://github.com/librespark/libretv/blob/main/src/app/api/douban/route.ts"
click node_watch_page "https://github.com/librespark/libretv/blob/main/src/app/watch/page.tsx"
click node_player "https://github.com/librespark/libretv/blob/main/src/components/player-shell.tsx"
click node_switch_source "https://github.com/librespark/libretv/blob/main/src/components/switch-source.tsx"
click node_download "https://github.com/librespark/libretv/blob/main/src/components/download-manager.tsx"
click node_history "https://github.com/librespark/libretv/blob/main/src/lib/db.ts"
click node_live_page "https://github.com/librespark/libretv/blob/main/src/app/live/page.tsx"
click node_live_channels "https://github.com/librespark/libretv/blob/main/src/components/live-channel-list.tsx"
click node_live_player "https://github.com/librespark/libretv/blob/main/src/components/live-player.tsx"
click node_live_playlist "https://github.com/librespark/libretv/blob/main/src/app/api/live/playlist/route.ts"
click node_epg "https://github.com/librespark/libretv/blob/main/src/app/api/live/epg/route.ts"
click node_stream_proxy "https://github.com/librespark/libretv/blob/main/src/app/api/live/stream/route.ts"
click node_settings "https://github.com/librespark/libretv/blob/main/src/components/source-manager.tsx"
click node_live_settings "https://github.com/librespark/libretv/blob/main/src/components/live-source-manager.tsx"
click node_subscriptions "https://github.com/librespark/libretv/blob/main/src/lib/subscription-sync.ts"
click node_source_list "https://github.com/librespark/libretv/blob/main/src/lib/source-list.ts"
click node_tvbox "https://github.com/librespark/libretv/blob/main/src/lib/tvbox-parser.ts"
click node_app_state "https://github.com/librespark/libretv/blob/main/src/lib/store.ts"
click node_local_settings "https://github.com/librespark/libretv/blob/main/src/lib/persist-storage.ts"
click node_client_api "https://github.com/librespark/libretv/blob/main/src/lib/client-api.ts"
click node_auth "https://github.com/librespark/libretv/blob/main/src/lib/auth.ts"
click node_api_guard "https://github.com/librespark/libretv/blob/main/src/lib/api-guard.ts"
click node_proxy "https://github.com/librespark/libretv/blob/main/src/lib/proxy-handlers.ts"

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_home,node_search_api,node_cms,node_detail_api,node_recommendations,node_recommend_api,node_viewer toneBlue
class node_watch_page,node_player,node_switch_source,node_download,node_history,node_browser toneAmber
class node_live_page,node_live_channels,node_live_player,node_live_playlist,node_epg,node_stream_proxy toneMint
class node_settings,node_live_settings,node_subscriptions,node_source_list,node_tvbox,node_app_state,node_local_settings toneRose
class node_client_api,node_auth,node_api_guard,node_proxy,node_external_sources,node_external_recs toneIndigo
```

## Components

### Discovery

- [Home & Search](https://github.com/librespark/libretv/blob/main/src/app/page.tsx): `src/app/page.tsx`
- [Aggregated Search](https://github.com/librespark/libretv/blob/main/src/app/api/search/route.ts): `src/app/api/search/route.ts`
- [CMS Parsing](https://github.com/librespark/libretv/blob/main/src/lib/cms-parser.ts): `src/lib/cms-parser.ts`
- [Video Details](https://github.com/librespark/libretv/blob/main/src/app/api/detail/route.ts): `src/app/api/detail/route.ts`
- [Recommendations](https://github.com/librespark/libretv/blob/main/src/components/douban-section.tsx): `src/components/douban-section.tsx`
- [Recommendation APIs](https://github.com/librespark/libretv/blob/main/src/app/api/douban/route.ts): `src/app/api/douban/route.ts`

### Playback

- [Watch Page](https://github.com/librespark/libretv/blob/main/src/app/watch/page.tsx): `src/app/watch/page.tsx`
- [Video Player](https://github.com/librespark/libretv/blob/main/src/components/player-shell.tsx): `src/components/player-shell.tsx`
- [Source Switching](https://github.com/librespark/libretv/blob/main/src/components/switch-source.tsx): `src/components/switch-source.tsx`
- [Episode Downloads](https://github.com/librespark/libretv/blob/main/src/components/download-manager.tsx): `src/components/download-manager.tsx`
- [Playback History](https://github.com/librespark/libretv/blob/main/src/lib/db.ts): `src/lib/db.ts`

### Live TV

- [Live Browser](https://github.com/librespark/libretv/blob/main/src/app/live/page.tsx): `src/app/live/page.tsx`
- [Channel List](https://github.com/librespark/libretv/blob/main/src/components/live-channel-list.tsx): `src/components/live-channel-list.tsx`
- [Live Player](https://github.com/librespark/libretv/blob/main/src/components/live-player.tsx): `src/components/live-player.tsx`
- [Playlist API](https://github.com/librespark/libretv/blob/main/src/app/api/live/playlist/route.ts): `src/app/api/live/playlist/route.ts`
- [EPG API](https://github.com/librespark/libretv/blob/main/src/app/api/live/epg/route.ts): `src/app/api/live/epg/route.ts`
- [Live Stream Proxy](https://github.com/librespark/libretv/blob/main/src/app/api/live/stream/route.ts): `src/app/api/live/stream/route.ts`

### Sources & Settings

- [Source Management](https://github.com/librespark/libretv/blob/main/src/components/source-manager.tsx): `src/components/source-manager.tsx`
- [Live Sources](https://github.com/librespark/libretv/blob/main/src/components/live-source-manager.tsx): `src/components/live-source-manager.tsx`
- [Subscription Sync](https://github.com/librespark/libretv/blob/main/src/lib/subscription-sync.ts): `src/lib/subscription-sync.ts`
- [Source List Parsing](https://github.com/librespark/libretv/blob/main/src/lib/source-list.ts): `src/lib/source-list.ts`
- [TVBOX Import](https://github.com/librespark/libretv/blob/main/src/lib/tvbox-parser.ts): `src/lib/tvbox-parser.ts`
- [App State](https://github.com/librespark/libretv/blob/main/src/lib/store.ts): `src/lib/store.ts`
- [Settings Storage](https://github.com/librespark/libretv/blob/main/src/lib/persist-storage.ts): `src/lib/persist-storage.ts`

### Platform Services

- [Client API](https://github.com/librespark/libretv/blob/main/src/lib/client-api.ts): `src/lib/client-api.ts`
- [Authentication](https://github.com/librespark/libretv/blob/main/src/lib/auth.ts): `src/lib/auth.ts`
- [API Guard](https://github.com/librespark/libretv/blob/main/src/lib/api-guard.ts): `src/lib/api-guard.ts`
- [Media Proxy](https://github.com/librespark/libretv/blob/main/src/lib/proxy-handlers.ts): `src/lib/proxy-handlers.ts`

### Other components

- **Viewer**
- **Third-Party Sources**
- **Recommendation Services**
- **Browser Storage**

## Connections

- Viewer → Home & Search: searches (evidence: `README.md`)
- Home & Search → Client API: requests (evidence: `src/app/page.tsx`)
- Client API → Aggregated Search: posts search (evidence: `src/lib/client-api.ts`)
- Aggregated Search → CMS Parsing: parses results (evidence: `src/app/api/search/route.ts`)
- Aggregated Search → Third-Party Sources: queries sources (evidence: `README.md`)
- Home & Search → Video Details: loads details
- Client API → Video Details: requests detail (evidence: `src/lib/client-api.ts`)
- Viewer → Recommendations: browses picks (evidence: `README.md`)
- Recommendations → Client API: requests picks
- Client API → Recommendation APIs: requests recommendations (evidence: `src/lib/client-api.ts`)
- Recommendation APIs → Recommendation Services: fetches data (evidence: `README.md`)
- Viewer → Watch Page: watches episodes (evidence: `README.md`)
- Watch Page → Video Player: renders player (evidence: `src/app/watch/page.tsx`)
- Watch Page → Source Switching: offers switching (evidence: `src/app/watch/page.tsx`)
- Watch Page → Episode Downloads: offers downloads (evidence: `src/app/watch/page.tsx`)
- Watch Page → Playback History: saves progress (evidence: `README.md`)
- Playback History → Browser Storage: persists locally (evidence: `README.md`)
- Viewer → Live Browser: opens live TV (evidence: `README.md`)
- Live Browser → Channel List: shows channels
- Channel List → Client API: loads playlists
- Client API → Playlist API: requests playlist (evidence: `src/lib/client-api.ts`)
- Playlist API → Third-Party Sources: fetches playlist
- Live Browser → Live Player: plays channel
- Live Player → Live Stream Proxy: retries via proxy (evidence: `README.md`)
- Live Browser → EPG API: loads schedules
- Viewer → Source Management: manages sources (evidence: `README.md`)
- Source Management → Live Sources: renders live settings (evidence: `src/components/source-manager.tsx`)
- Source Management → App State: updates settings (evidence: `src/components/source-manager.tsx`)
- Source Management → Subscription Sync: syncs subscriptions (evidence: `src/components/source-manager.tsx`)
- Subscription Sync → Client API: fetches lists (evidence: `src/lib/subscription-sync.ts`)
- Subscription Sync → Source List Parsing: parses lists (evidence: `src/lib/subscription-sync.ts`)
- TVBOX Import → Source List Parsing: normalizes imports (evidence: `src/lib/tvbox-parser.ts`)
- App State → Settings Storage: persists preferences (evidence: `src/lib/store.ts`)
- Client API → API Guard: calls protected APIs
- API Guard → Authentication: checks session (evidence: `src/lib/api-guard.ts`)
- Aggregated Search → API Guard: guards requests (evidence: `src/app/api/search/route.ts`)
- Media Proxy → Third-Party Sources: proxies media
