This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
/
livestream.go
115 lines (108 loc) · 2.45 KB
/
livestream.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
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/terorie/ytpriv"
"github.com/terorie/ytpriv/types"
)
var livestreamCmd = cobra.Command{
Use: "livestream",
Short: "Scrape a livestream",
}
func init() {
rootCmd.AddCommand(&livestreamCmd)
}
var livestreamChat = cobra.Command{
Use: "chat",
Short: "Follow the live chat",
Args: cobra.ExactArgs(1),
Run: cmdFunc(doLivestreamChat),
}
func init() {
livestreamCmd.AddCommand(&livestreamChat)
flags := livestreamChat.Flags()
flags.Bool("top", false, "Top chat only")
}
func doLivestreamChat(c *cobra.Command, args []string) error {
flags := c.Flags()
top, err := flags.GetBool("top")
if err != nil {
panic(err.Error())
}
videoID := args[0]
videoID, err = yt.ExtractVideoID(videoID)
if err != nil {
return err
}
video, err := client.RequestVideo(videoID).Do()
if err != nil {
return fmt.Errorf("failed to get video info: %w", err)
}
var cont string
var live bool
if top {
live = video.TopChatContinuation != ""
if live {
cont = video.TopChatContinuation
} else {
cont = video.TopChatReplayContinuation
}
} else {
live = video.LiveChatContinuation != ""
if live {
cont = video.LiveChatContinuation
} else {
cont = video.LiveChatReplayContinuation
}
}
if cont == "" {
return fmt.Errorf("no live chat found")
}
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
if !live {
logrus.Info("Dumping live chat replay")
for i := 0; cont != ""; i++ {
logrus.WithField("page", i).Info("Dumping page")
var out []*types.LivechatMessage
out, cont, err = client.RequestLivechatReplay(cont).Do()
if err != nil {
return err
}
for _, msg := range out {
if err := enc.Encode(&msg); err != nil {
panic(err)
}
}
}
} else {
logrus.Info("Dumping live chat")
liveCont := yt.LivechatContinuation{
Timeout: 0,
Continuation: cont,
}
for i := 0; liveCont.Continuation != ""; i++ {
logrus.
WithField("page", i).
WithField("timeout_ms", liveCont.Timeout).
Info("Dumping page")
time.Sleep(time.Duration(liveCont.Timeout) * time.Millisecond)
var out []*types.LivechatMessage
out, liveCont, err = client.RequestLivechat(liveCont.Continuation).Do()
if err != nil {
return err
}
for _, msg := range out {
if err := enc.Encode(&msg); err != nil {
panic(err)
}
}
}
}
logrus.Info("Reached end of chat")
return nil
}