-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranscript.go
More file actions
248 lines (211 loc) · 6.06 KB
/
Copy pathtranscript.go
File metadata and controls
248 lines (211 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Package yt_transcript fetches YouTube video transcripts via the innertube API.
// Stdlib only, zero dependencies.
package yt_transcript
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"regexp"
"strings"
"time"
)
const (
watchURL = "https://www.youtube.com/watch?v=%s"
playerURL = "https://www.youtube.com/youtubei/v1/player?key=%s"
apiKeyRe = `"INNERTUBE_API_KEY":\s*"([a-zA-Z0-9_-]+)"`
androidUA = "com.google.android.youtube/20.10.38 (Linux; U; Android 13) gzip"
)
// Segment represents a single transcript line with timing.
type Segment struct {
Text string `json:"text"`
Start float64 `json:"start"`
Duration float64 `json:"duration"`
}
// Client fetches YouTube transcripts.
type Client struct {
http *http.Client
}
// NewClient returns a Client with a default 30s timeout and cookie jar.
func NewClient() *Client {
jar, _ := cookiejar.New(nil)
return &Client{
http: &http.Client{Timeout: 30 * time.Second, Jar: jar},
}
}
// WithHTTPClient sets a custom http.Client.
func (c *Client) WithHTTPClient(h *http.Client) *Client {
c.http = h
return c
}
// FetchTranscript retrieves the transcript for a YouTube video in the given language.
func (c *Client) FetchTranscript(ctx context.Context, videoID, lang string) ([]Segment, error) {
html, err := c.fetchWatchPage(ctx, videoID)
if err != nil {
return nil, fmt.Errorf("fetching watch page: %w", err)
}
apiKey, err := extractAPIKey(html)
if err != nil {
return nil, fmt.Errorf("YouTube did not return expected page content — the video may be unavailable or requests are being blocked: %w", err)
}
captions, err := c.fetchCaptions(ctx, videoID, apiKey)
if err != nil {
return nil, fmt.Errorf("fetching captions: %w", err)
}
transcript, err := findCaptionTrack(captions, lang)
if err != nil {
return nil, fmt.Errorf("finding transcript: %w", err)
}
return c.fetchAndParse(ctx, transcript.BaseURL)
}
func (c *Client) fetchWatchPage(ctx context.Context, videoID string) (string, error) {
u := fmt.Sprintf(watchURL, videoID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept-Language", "en-US")
resp, err := c.http.Do(req)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusTooManyRequests {
return "", fmt.Errorf("rate limited by YouTube (429)")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
html := string(body)
if strings.Contains(html, `class="g-recaptcha"`) {
return "", fmt.Errorf("bot detection triggered — YouTube is blocking requests from this IP")
}
return html, nil
}
var apiKeyRegex = regexp.MustCompile(apiKeyRe)
func extractAPIKey(html string) (string, error) {
matches := apiKeyRegex.FindStringSubmatch(html)
if len(matches) < 2 {
return "", fmt.Errorf("could not parse YouTube page")
}
return matches[1], nil
}
type captionTrack struct {
BaseURL string `json:"baseUrl"`
LanguageCode string `json:"languageCode"`
Kind string `json:"kind"`
Name struct {
Runs []struct {
Text string `json:"text"`
} `json:"runs"`
} `json:"name"`
}
func (c *Client) fetchCaptions(ctx context.Context, videoID, apiKey string) ([]captionTrack, error) {
body, err := json.Marshal(map[string]any{
"context": map[string]any{
"client": map[string]string{
"clientName": "ANDROID",
"clientVersion": "20.10.38",
},
},
"videoId": videoID,
})
if err != nil {
return nil, err
}
u := fmt.Sprintf(playerURL, apiKey)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", androidUA)
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusTooManyRequests {
return nil, fmt.Errorf("rate limited by YouTube (429)")
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var data struct {
Captions struct {
PlayerCaptionsTracklistRenderer struct {
CaptionTracks []captionTrack `json:"captionTracks"`
} `json:"playerCaptionsTracklistRenderer"`
} `json:"captions"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, fmt.Errorf("parsing player response: %w", err)
}
tracks := data.Captions.PlayerCaptionsTracklistRenderer.CaptionTracks
if len(tracks) == 0 {
return nil, fmt.Errorf("no caption tracks available")
}
return tracks, nil
}
func findCaptionTrack(tracks []captionTrack, lang string) (captionTrack, error) {
for _, t := range tracks {
if t.LanguageCode == lang {
t.BaseURL = strings.Replace(t.BaseURL, "&fmt=srv3", "", 1)
return t, nil
}
}
return captionTrack{}, fmt.Errorf("no transcript found for language %q", lang)
}
type transcriptXML struct {
XMLName xml.Name `xml:"transcript"`
Texts []transcriptText `xml:"text"`
}
type transcriptText struct {
Start float64 `xml:"start,attr"`
Dur float64 `xml:"dur,attr"`
Value string `xml:",chardata"`
}
func (c *Client) fetchAndParse(ctx context.Context, baseURL string) ([]Segment, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL, nil)
if err != nil {
return nil, err
}
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusTooManyRequests {
return nil, fmt.Errorf("rate limited by YouTube (429)")
}
var x transcriptXML
if err := xml.NewDecoder(resp.Body).Decode(&x); err != nil {
return nil, fmt.Errorf("parsing transcript XML: %w", err)
}
segments := make([]Segment, len(x.Texts))
for i, t := range x.Texts {
segments[i] = Segment{
Text: unescapeHTML(t.Value),
Start: t.Start,
Duration: t.Dur,
}
}
return segments, nil
}
var htmlReplacer = strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
""", `"`,
"'", "'",
"'", "'",
)
func unescapeHTML(s string) string {
return htmlReplacer.Replace(s)
}