-
Notifications
You must be signed in to change notification settings - Fork 97
/
player_response.go
681 lines (583 loc) · 18.3 KB
/
player_response.go
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
)
const (
PlayableOk = "OK"
PlayableOffline = "LIVE_STREAM_OFFLINE"
PlayableUnplayable = "UNPLAYABLE"
PlayableError = "ERROR"
WebAPIPostData = `{
'context': {
'client': {
'clientName': '%s',
'clientVersion': '%s',
'hl': 'en'
}
},
'videoId': '%s',
'playbackContext': {
'contentPlaybackContext': {
'html5Preference': 'HTML5_PREF_WANTS'
}
},
'serviceIntegrityDimensions': {
'poToken': '%s'
}
}
`
)
const (
PlayerResponseFound = iota
PlayerResponseNotFound
PlayerResponseNotUsable
)
var (
playerRespDecl = []byte("var ytInitialPlayerResponse =")
ytInitialDataDecl = []byte("var ytInitialData =")
)
/*
Auto-generated using https://mholt.github.io/json-to-go/
Trimmed after to relevent fields
*/
type PlayerResponse struct {
ResponseContext struct {
MainAppWebResponseContext struct {
LoggedOut bool `json:"loggedOut"`
} `json:"mainAppWebResponseContext"`
} `json:"responseContext"`
PlayabilityStatus struct {
Status string `json:"status"`
Reason string `json:"reason"`
LiveStreamability struct {
LiveStreamabilityRenderer struct {
VideoID string `json:"videoId"`
OfflineSlate struct {
LiveStreamOfflineSlateRenderer struct {
ScheduledStartTime string `json:"scheduledStartTime"`
} `json:"liveStreamOfflineSlateRenderer"`
} `json:"offlineSlate"`
PollDelayMs string `json:"pollDelayMs"`
} `json:"liveStreamabilityRenderer"`
} `json:"liveStreamability"`
} `json:"playabilityStatus"`
StreamingData struct {
ExpiresInSeconds string `json:"expiresInSeconds"`
AdaptiveFormats []struct {
Itag int `json:"itag"`
URL string `json:"url"`
MimeType string `json:"mimeType"`
QualityLabel string `json:"qualityLabel,omitempty"`
TargetDurationSec float64 `json:"targetDurationSec"`
} `json:"adaptiveFormats"`
DashManifestURL string `json:"dashManifestUrl"`
} `json:"streamingData"`
VideoDetails struct {
VideoID string `json:"videoId"`
Title string `json:"title"`
LengthSeconds string `json:"lengthSeconds"`
IsLive bool `json:"isLive"`
ChannelID string `json:"channelId"`
IsOwnerViewing bool `json:"isOwnerViewing"`
ShortDescription string `json:"shortDescription"`
AverageRating float64 `json:"averageRating"`
AllowRatings bool `json:"allowRatings"`
ViewCount string `json:"viewCount"`
Author string `json:"author"`
IsLiveContent bool `json:"isLiveContent"`
} `json:"videoDetails"`
Microformat struct {
PlayerMicroformatRenderer struct {
Thumbnail struct {
Thumbnails []struct {
URL string `json:"url"`
} `json:"thumbnails"`
} `json:"thumbnail"`
LiveBroadcastDetails struct {
IsLiveNow bool `json:"isLiveNow"`
StartTimestamp string `json:"startTimestamp"`
EndTimestamp string `json:"endTimestamp"`
} `json:"liveBroadcastDetails"`
PublishDate string `json:"publishDate"`
UploadDate string `json:"uploadDate"`
} `json:"playerMicroformatRenderer"`
} `json:"microformat"`
}
type YtInitialData struct {
Contents struct {
Twocolumnbrowseresultsrenderer struct {
Tabs []struct {
Tabrenderer struct {
Endpoint struct {
Commandmetadata struct {
Webcommandmetadata struct {
URL string `json:"url"`
} `json:"webCommandMetadata"`
} `json:"commandMetadata"`
} `json:"endpoint"`
Content struct {
Richgridrenderer struct {
Contents []RichGridContent `json:"contents"`
} `json:"richGridRenderer"`
} `json:"content"`
} `json:"tabRenderer"`
} `json:"tabs"`
} `json:"twoColumnBrowseResultsRenderer"`
} `json:"contents"`
}
type RichGridContent struct {
Richitemrenderer struct {
Content struct {
Videorenderer struct {
Videoid string `json:"videoId"`
Thumbnailoverlays []struct {
Thumbnailoverlaytimestatusrenderer struct {
Style string `json:"style"`
} `json:"thumbnailOverlayTimeStatusRenderer"`
} `json:"thumbnailOverlays"`
Badges []struct {
Metadatabadgerenderer struct {
Style string `json:"style"`
} `json:"metadataBadgeRenderer"`
} `json:"badges"`
} `json:"videoRenderer"`
} `json:"content"`
} `json:"richItemRenderer"`
}
// Search the given HTML for the player response object
func GetJsonFromHtml(htmlData []byte, jsonDecl []byte) []byte {
var objData []byte
reader := bytes.NewReader(htmlData)
tokenizer := html.NewTokenizer(reader)
isScript := false
for {
tt := tokenizer.Next()
switch tt {
case html.ErrorToken:
return objData
case html.TextToken:
if isScript {
data := tokenizer.Text()
declStart := bytes.Index(data, jsonDecl)
if declStart < 0 {
continue
}
// Maybe add a LogTrace in the future for stuff like this
//LogDebug("Found script element with player response in watch page.")
objStart := bytes.Index(data[declStart:], []byte("{")) + declStart
objEnd := bytes.LastIndex(data[objStart:], []byte("};")) + 1 + objStart
if objEnd > objStart {
objData = data[objStart:objEnd]
}
return objData
}
case html.StartTagToken:
tn, _ := tokenizer.TagName()
if string(tn) == "script" {
isScript = true
} else {
isScript = false
}
}
}
}
func (di *DownloadInfo) GetNewestStreamFromStreams() string {
// Surely there won't be more than 5 simultaneous streams when looking for membership streams, right?
const MAX_STREAM_ITEM_CHECK = 5
streamUrl := ""
if !di.LiveURL {
return streamUrl
}
initialData := &YtInitialData{}
var contents []RichGridContent
streamsUrl := strings.Replace(di.URL, "/live", "/streams", 1)
streamsHtml := DownloadData(streamsUrl)
ytInitialData := GetJsonFromHtml(streamsHtml, ytInitialDataDecl)
err := json.Unmarshal(ytInitialData, initialData)
if err != nil {
return streamUrl
}
for _, tab := range initialData.Contents.Twocolumnbrowseresultsrenderer.Tabs {
if strings.HasSuffix(tab.Tabrenderer.Endpoint.Commandmetadata.Webcommandmetadata.URL, "/streams") {
contents = tab.Tabrenderer.Content.Richgridrenderer.Contents
}
}
for i, content := range contents {
if i >= MAX_STREAM_ITEM_CHECK {
break
}
videoRenderer := content.Richitemrenderer.Content.Videorenderer
if di.MembersOnly {
mengen := false
for _, badge := range videoRenderer.Badges {
if badge.Metadatabadgerenderer.Style == "BADGE_STYLE_TYPE_MEMBERS_ONLY" {
mengen = true
break
}
}
if !mengen {
continue
}
}
for _, thumbnailRenderer := range videoRenderer.Thumbnailoverlays {
if thumbnailRenderer.Thumbnailoverlaytimestatusrenderer.Style == "LIVE" {
streamUrl = fmt.Sprintf("https://www.youtube.com/watch?v=%s", videoRenderer.Videoid)
return streamUrl
}
}
}
return streamUrl
}
// New PO Token stuff requires calling the API instead of
// using the URLs from scraping the watch page.
// Credit to yt-dlp devs for POST data and headers.
func (di *DownloadInfo) DownloadWebPlayerResponse() (*PlayerResponse, error) {
if len(di.PoToken) == 0 {
return nil, fmt.Errorf("Cannot retrieve web api player response without a PO Token set")
}
pr := &PlayerResponse{}
auth := GenerateSAPISIDHash(di.CookiesURL)
queryParams := ""
ytcfg := di.Ytcfg
if ytcfg == nil {
ytcfg = GetDefaultYTCFG()
}
if len(ytcfg.InnertubeApiKey) > 0 {
queryParams = fmt.Sprintf("?innertube_key=%s", ytcfg.InnertubeApiKey)
}
data := []byte(fmt.Sprintf(WebAPIPostData, ytcfg.InnertubeClientName, ytcfg.InnertubeClientVersion, di.VideoID, di.PoToken))
req, err := http.NewRequest("POST", fmt.Sprintf("https://www.youtube.com/youtubei/v1/player%s", queryParams), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
req.Header.Add("X-YouTube-Client-Name", strconv.Itoa(ytcfg.InnertubeCtxClientName))
req.Header.Add("X-YouTube-Client-Version", ytcfg.InnertubeCtxClientVersion)
req.Header.Add("Origin", "https://www.youtube.com")
req.Header.Add("content-type", "application/json")
if len(auth) > 0 {
req.Header.Add("X-Origin", "https://www.youtube.com")
req.Header.Add("Authorization", auth)
}
if len(ytcfg.IdToken) > 0 {
req.Header.Add("X-Youtube-Identity-Token", ytcfg.IdToken)
}
if len(ytcfg.DelegatedSessionId) > 0 {
req.Header.Add("X-Goog-PageId", ytcfg.DelegatedSessionId)
}
if len(ytcfg.VisitorData) > 0 {
req.Header.Add("X-Goog-Visitor-Id", ytcfg.VisitorData)
}
if len(ytcfg.SessionIndex) > 0 {
req.Header.Add("X-Goog-AuthUser", ytcfg.SessionIndex)
}
LogTrace("%v", req)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("returned non-200 status code %d", resp.StatusCode)
}
respData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(respData, pr)
if err != nil {
return nil, err
}
return pr, nil
}
func (di *DownloadInfo) GetVideoHtml() []byte {
var videoHtml []byte
if di.LiveURL {
streamUrl := di.GetNewestStreamFromStreams()
if len(streamUrl) > 0 {
videoHtml = DownloadData(streamUrl)
}
}
if len(videoHtml) == 0 && !di.MembersOnly {
videoHtml = DownloadData(di.URL)
}
return videoHtml
}
// Get the player response object from youtube
func (di *DownloadInfo) GetPlayerResponse(videoHtml []byte) (*PlayerResponse, error) {
pr := &PlayerResponse{}
if len(videoHtml) == 0 {
return nil, fmt.Errorf("unable to retrieve data from video page")
}
prData := GetJsonFromHtml(videoHtml, playerRespDecl)
if len(prData) == 0 {
if debug && di.InProgress {
LogDebug("Could not find player response from video watch page. Writing html file to %s.html", di.VideoID)
os.WriteFile(fmt.Sprintf("%s.html", di.VideoID), videoHtml, di.FileMode)
}
return nil, fmt.Errorf("unable to retrieve player response object from watch page")
}
err := json.Unmarshal(prData, pr)
if err != nil {
return nil, err
}
if di.LiveURL {
videoId := GetVideoIdFromWatchPage(videoHtml)
if len(videoId) > 0 {
di.VideoID = videoId
}
}
return pr, nil
}
func (di *DownloadInfo) GetPlayablePlayerResponse() (retrieved int, pr *PlayerResponse, selectedQualities []string) {
firstWait := true
isLiveURL := di.LiveURL
waitOnLiveURL := isLiveURL && di.RetrySecs > 0 && !di.InProgress
liveWaited := 0
retryCount := 0
var secsLate int
var err error
if len(di.SelectedQuality) > 0 {
selectedQualities = ParseQualitySelection(VideoQualities, di.SelectedQuality)
}
for {
videoHtml := di.GetVideoHtml()
pr, err = di.GetPlayerResponse(videoHtml)
if err != nil {
if waitOnLiveURL {
if len(selectedQualities) < 1 {
fmt.Fprintln(os.Stderr)
selectedQualities = GetQualityFromUser(VideoQualities, true)
}
if liveWaited == 0 {
LogGeneral("You have opted to wait for a livestream to be scheduled. Retrying every %d seconds.\n", di.RetrySecs)
}
time.Sleep(time.Duration(di.RetrySecs) * time.Second)
liveWaited += di.RetrySecs
retryCount += 1
if loglevel > LoglevelQuiet {
msg := "Retries: %d (Last retry: %s), Total time waited: %d seconds"
if !statusNewlines {
msg = "\r" + msg
} else {
msg = msg + "\n"
}
fmt.Fprintf(os.Stderr, msg,
retryCount,
time.Now().Format("2006/01/02 15:04:05"),
liveWaited,
)
}
continue
}
fmt.Fprintln(os.Stderr)
LogError("Error retrieving player response: %s", err.Error())
return PlayerResponseNotFound, nil, nil
}
if len(pr.VideoDetails.VideoID) == 0 {
if di.InProgress {
LogWarn("Video details no longer available mid download.")
LogWarn("Stream was likely privated after finishing.")
LogWarn("We will continue to download, but if it starts to fail, nothing can be done.")
di.printStatusWithoutLock()
}
LogError("Video Details not found, video is likely private or does not exist.")
di.Live = false
di.Unavailable = true
return PlayerResponseNotUsable, nil, nil
}
if len(pr.PlayabilityStatus.LiveStreamability.LiveStreamabilityRenderer.VideoID) == 0 && !pr.VideoDetails.IsLiveContent {
if di.Live {
di.Live = false
} else {
LogError("%s is not a livestream. It would be better to use yt-dlp to download it.", di.URL)
}
return PlayerResponseNotUsable, nil, nil
}
switch pr.PlayabilityStatus.Status {
case PlayableError:
if di.InProgress {
LogInfo("Finishing download")
}
LogError("Playability status: ERROR. Reason: %s", pr.PlayabilityStatus.Reason)
di.Live = false
return PlayerResponseNotUsable, nil, nil
case PlayableUnplayable:
loggedIn := !pr.ResponseContext.MainAppWebResponseContext.LoggedOut
LogError("Playability status: UNPLAYABLE.")
LogError("Reason: %s", pr.PlayabilityStatus.Reason)
LogError("Logged in status: %t", loggedIn)
LogError("If this is a members only stream, you provided a cookies.txt file, and the above 'logged in' status is not True, please try updating your cookies file.")
di.Unavailable = true
if di.InProgress {
di.printStatusWithoutLock()
}
return PlayerResponseNotUsable, nil, nil
case PlayableOffline:
if di.InProgress {
LogDebug("Livestream status is %s mid-download", PlayableOffline)
return PlayerResponseNotUsable, nil, nil
}
if di.LiveFromVal != "" && strings.HasPrefix(di.LiveFromVal, "-") {
LogError("Option --live-from with a negative duration is not valid for a scheduled stream.")
return PlayerResponseNotUsable, nil, nil
}
if di.Wait == ActionDoNot {
LogError("Stream has not started, and you have opted not to wait.")
return PlayerResponseNotUsable, nil, nil
}
if firstWait && di.Wait == ActionAsk && di.RetrySecs == 0 {
if !di.AskWaitForStream() {
return PlayerResponseNotUsable, nil, nil
}
}
if firstWait {
if !(isLiveURL && di.RetrySecs > 0) {
di.printChannelAndTitle(pr)
}
fmt.Fprintln(os.Stderr)
if len(selectedQualities) < 1 {
selectedQualities = GetQualityFromUser(VideoQualities, true)
}
}
if di.RetrySecs > 0 {
if firstWait {
firstWait = false
LogGeneral("Waiting for stream, retrying every %d seconds...\n", di.RetrySecs)
}
time.Sleep(time.Duration(di.RetrySecs) * time.Second)
liveWaited += di.RetrySecs
retryCount += 1
if loglevel > LoglevelQuiet {
msg := "Retries: %d (Last retry: %s), Total time waited: %d seconds"
if !statusNewlines {
msg = "\r" + msg
} else {
msg = msg + "\n"
}
fmt.Fprintf(os.Stderr, msg,
retryCount,
time.Now().Format("2006/01/02 15:04:05"),
liveWaited,
)
}
continue
}
schedTime, err := strconv.ParseInt(pr.PlayabilityStatus.LiveStreamability.LiveStreamabilityRenderer.OfflineSlate.LiveStreamOfflineSlateRenderer.ScheduledStartTime,
10, 64)
if err != nil {
LogWarn("Failed to get stream start time: %s.", err)
LogWarn("Falling back to polling.")
di.RetrySecs = DefaultPollTime
time.Sleep(time.Duration(di.RetrySecs) * time.Second)
continue
}
curTime := time.Now().Unix()
slepTime := schedTime - curTime
if slepTime > 0 {
if !firstWait {
LogGeneral("Stream rescheduled.")
}
firstWait = false
secsLate = 0
LogGeneral("Stream starts at %s in %d seconds. ",
pr.Microformat.PlayerMicroformatRenderer.LiveBroadcastDetails.StartTimestamp,
slepTime)
LogGeneral("Waiting for this time to elapse...")
// Loop it just in case a rogue sleep interrupt happens
for slepTime > 0 {
time.Sleep(time.Duration(slepTime) * time.Second)
curTime = time.Now().Unix()
slepTime = schedTime - curTime
if slepTime > 0 {
LogDebug("Woke up %d seconds early. Continuing sleep...", slepTime)
}
}
// We've waited until the scheduled time
continue
}
if firstWait {
LogGeneral("Stream should have started. Checking back every %d seconds\n", DefaultPollTime)
firstWait = false
}
/*
If we get this far, the stream's scheduled time has passed but it's still not started
Check every 15 seconds
*/
time.Sleep(time.Duration(DefaultPollTime) * time.Second)
secsLate += DefaultPollTime
LogGeneral("Stream is %d seconds late...", secsLate)
continue
case PlayableOk:
// player response returned from /live does not include full information
if isLiveURL {
di.URL = fmt.Sprintf("https://www.youtube.com/watch?v=%s", di.VideoID)
di.MembersOnly = false
isLiveURL = false
continue
}
di.printChannelAndTitle(pr)
streamData := pr.StreamingData
liveDetails := pr.Microformat.PlayerMicroformatRenderer.LiveBroadcastDetails
isLive := liveDetails.IsLiveNow
if !isLive && !di.InProgress {
/*
The livestream has likely ended already.
Check if the stream has been processed.
If not, then download it.
*/
if len(liveDetails.EndTimestamp) > 0 {
if len(streamData.AdaptiveFormats) > 0 {
// Assume that all formats will be fully processed if one is, and vice versa
if len(streamData.AdaptiveFormats[0].URL) == 0 {
LogGeneral("Livestream has ended and is being processed. Download URLs not available.")
return PlayerResponseNotUsable, nil, nil
}
if !IsFragmented(streamData.AdaptiveFormats[0].URL) {
LogGeneral("Livestream has been processed. Use yt-dlp instead.")
return PlayerResponseNotUsable, nil, nil
}
} else {
LogGeneral("Livestream has ended and is being processed. Download URLs not available.")
return PlayerResponseNotUsable, nil, nil
}
} else {
/*
I actually ran into this case once so far.
Stream is set as playable, but has not started.
*/
LogGeneral("Livestream is offline, should have started, and does not have an end timestamp.")
LogGeneral("Waiting %d seconds and trying again.\n", DefaultPollTime)
time.Sleep(time.Duration(DefaultPollTime) * time.Second)
continue
}
}
err = di.GetYTCFG(videoHtml)
if err != nil {
LogDebug("Error getting ytcfg: %s", err.Error())
}
default:
if secsLate > 0 {
fmt.Fprintln(os.Stderr)
}
LogError("Unknown playability status: %s", pr.PlayabilityStatus.Status)
if di.InProgress {
di.Live = false
}
return PlayerResponseNotUsable, nil, nil
}
if secsLate > 0 {
fmt.Fprintln(os.Stderr)
}
break
}
return PlayerResponseFound, pr, selectedQualities
}