AtlatestRepositorysigil-youtube
sigil-youtube / treeREADME.md
1
# sigil-youtube3
YouTube Data API v3 client library for [Sigil](https://codeberg.org/sigil/sigil).5
Provides a complete interface to YouTube's video management, upload,6
analytics, livestreaming, and playlist APIs.8
## Features10
- **Video management** — list, update, delete videos; set custom thumbnails11
- **Resumable uploads** — chunked upload protocol with resume-after-failure support12
- **Analytics** — query views, watch time, subscribers, traffic sources, and top videos13
- **Livestreaming** — full broadcast lifecycle: create, bind streams, transition states14
- **Playlists** — CRUD for playlists and playlist items with reordering15
- **Search** — full-text search across YouTube (quota-expensive; prefer known IDs)16
- **Channel info** — retrieve channel metadata, subscriber counts, uploads playlist17
- **Quota-aware** — all functions document their quota cost in comments19
## Modules21
| Module | Description |22
|--------|-------------|23
| `(youtube)` | Core client, records, HTTP helpers, video/channel/search APIs |24
| `(youtube upload)` | Resumable upload protocol with chunked transfer |25
| `(youtube analytics)` | YouTube Analytics API v2 queries |26
| `(youtube live)` | Live broadcast and stream management |27
| `(youtube playlist)` | Playlist and playlist item CRUD |28
| `(youtube oauth)` | OAuth 2.0 integration via sigil-oauth (token management, auto-refresh) |30
## Dependencies32
- `sigil-stdlib` — core, dict, string, struct33
- `sigil-json` — JSON encode/decode34
- `sigil-http` — HTTP client35
- `sigil-oauth` — shared OAuth 2.0 client library37
Core dependencies are from the [sigil](https://codeberg.org/sigil/sigil) mono-repo.38
OAuth support is from [sigil-oauth](https://codeberg.org/sigil/sigil-oauth).40
## Building42
```bash43
sigil deps install44
sigil build45
sigil test46
```48
## Usage50
### Create a client52
```scheme53
(import (youtube))55
;; With OAuth2 access token (required for mutations)56
(define client (youtube-client access-token: "ya29.your-token"))58
;; With API key only (read-only public data)59
(define client (youtube-client api-key: "AIza..."))60
```62
### OAuth 2.0 token management64
```scheme65
(import (youtube oauth))67
;; Create an OAuth config for Google/YouTube68
(define config69
(youtube-oauth-config "your-client-id" "your-client-secret"))71
;; Load stored tokens (from ~/.config/sigil/youtube/tokens.json)72
(define tokens (youtube-load-tokens))74
;; Build a client with auto-refresh75
(let-values (((client fresh-tokens)76
(youtube-ensure-client config tokens (current-second))))77
;; client is ready to use, tokens are refreshed if needed78
(youtube-channel-mine client))80
;; Run the full authorization flow (opens browser, starts callback server)81
(define tokens82
(oauth-run-authorization-flow config (current-second)))83
(youtube-save-tokens tokens)84
```86
### Get channel info88
```scheme89
;; Your own channel (requires OAuth)90
(define ch (youtube-channel-mine client))91
(youtube-channel-title ch) ; => "System Crafters"92
(youtube-channel-subscriber-count ch) ; => 5000093
(youtube-channel-uploads-playlist-id ch); => "UUxxxxxxxxxxxxxx"95
;; Any channel by ID96
(define ch (youtube-channel-info client "UCxxxxxxxxxxxxxx"))97
```99
### List and update videos101
```scheme102
;; Get videos by ID (1 quota unit, batches multiple IDs)103
(define videos (youtube-videos client "id1,id2,id3"))105
;; Update video metadata (50 quota units)106
(youtube-video-update client "video-id"107
#{ title: "New Title"108
description: "Updated description"109
privacyStatus: "public" })111
;; Delete a video (50 quota units)112
(youtube-video-delete client "video-id")113
```115
### Upload a video117
```scheme118
(import (youtube upload))120
;; High-level upload (handles chunking automatically)121
;; Quota cost: 1,600 units122
(define video123
(youtube-upload-video client124
#{ snippet: #{ title: "My Video"125
description: "A great video"126
categoryId: "28" }127
status: #{ privacyStatus: "private" } }128
file-data129
"video/mp4"))130
```132
### Query analytics134
```scheme135
(import (youtube analytics))137
;; Daily views for a date range138
(define report (youtube-views-by-day client "2026-01-01" "2026-03-25"))140
;; Top 10 videos by views141
(define top (youtube-top-videos client "2026-01-01" "2026-03-25"))143
;; Custom query144
(define custom145
(youtube-analytics-query client "2026-01-01" "2026-03-25"146
"views,estimatedMinutesWatched,subscribersGained"147
#{ dimensions: "day" sort: "-views" }))148
```150
### Manage playlists152
```scheme153
(import (youtube playlist))155
;; List your playlists156
(define plists (youtube-playlists client))158
;; Create a playlist (50 quota units)159
(define pl (youtube-create-playlist client "New Series"160
#{ description: "Episodes of my new series"161
privacy-status: "public" }))163
;; Add a video to a playlist (50 quota units)164
(youtube-add-to-playlist client (youtube-playlist-id pl) "video-id")166
;; List items in a playlist (1 quota unit per page)167
(define items (youtube-playlist-items client "PLxxxxxx"))168
```170
### Livestreaming172
```scheme173
(import (youtube live))175
;; Create broadcast + stream, bind them176
(define bc (youtube-create-broadcast client177
"Weekly Stream" "2026-03-28T18:00:00Z"))178
(define st (youtube-create-stream client "Main Feed"))179
(youtube-bind-broadcast client180
(youtube-broadcast-id bc) (youtube-stream-id st))182
;; Get RTMP credentials183
(youtube-stream-rtmp-url st) ; => "rtmp://a.rtmp.youtube.com/live2"184
(youtube-stream-stream-key st) ; => "xxxx-xxxx-xxxx-xxxx"186
;; Go live, then end187
(youtube-transition-broadcast client (youtube-broadcast-id bc) "live")188
(youtube-transition-broadcast client (youtube-broadcast-id bc) "complete")189
```191
## Quota Budget193
YouTube's default quota is 10,000 units/day. Key costs:195
| Operation | Cost |196
|-----------|------|197
| Read (list/get) | 1 unit |198
| Write (insert/update/delete) | 50 units |199
| Video upload | 1,600 units |200
| Search | 100 units |201
| Thumbnail upload | 50 units |203
Tip: batch video IDs in `youtube-videos` calls — multiple IDs still cost only 1 unit.205
## License207
BSD-3-Clause