-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyt_chat.go
322 lines (289 loc) · 9.37 KB
/
yt_chat.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
package YtChat
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type SubMenuItems struct {
Title string
Continuation struct {
ReloadContinuationData struct {
Continuation string
}
}
}
type InnerTubeContext struct {
Client struct {
Hl string `json:"hl"`
Gl string `json:"gl"`
RemoteHost string `json:"remoteHost"`
DeviceMake string `json:"deviceMake"`
DeviceModel string `json:"deviceModel"`
VisitorData string `json:"visitorData"`
UserAgent string `json:"userAgent"`
ClientName string `json:"clientName"`
ClientVersion string `json:"clientVersion"`
OsName string `json:"osName"`
OsVersion string `json:"osVersion"`
OriginalUrl string `json:"originalUrl"`
Platform string `json:"platform"`
ClientFormFactor string `json:"clientFormFactor"`
ConfigInfo struct {
AppInstallData string `json:"appInstallData"`
} `json:"configInfo"`
} `json:"client"`
}
type YtCfg struct {
INNERTUBE_API_KEY string
INNERTUBE_CONTEXT InnerTubeContext
INNERTUBE_CONTEXT_CLIENT_NAME string
INNERTUBE_CLIENT_VERSION string
ID_TOKEN string
}
type Context struct {
Context InnerTubeContext `json:"context"`
Continuation string `json:"continuation"`
}
type ContinuationChat struct {
TimedContinuationData struct {
Continuation string `json:"continuation"`
TimeoutMs int `json:"timeoutMs"`
} `json:"timedContinuationData"`
InvalidationContinuationData struct {
Continuation string `json:"continuation"`
TimeoutMs int `json:"timeoutMs"`
} `json:"invalidationContinuationData"`
}
type Actions struct {
AddChatItemAction struct {
Item struct {
LiveChatTextMessageRenderer struct {
Message struct {
Runs []Runs `json:"runs"`
} `json:"message"`
AuthorName struct {
SimpleText string `json:"simpleText"`
}
TimestampUsec string `json:"timestampUsec"`
} `json:"liveChatTextMessageRenderer"`
} `json:"item"`
} `json:"addChatItemAction"`
}
type Runs struct {
Text string `json:"text,omitempty"`
Emoji struct {
EmojiId string `json:"emojiId"`
IsCustomEmoji bool `json:"isCustomEmoji,omitempty"`
Image struct {
Thumbnails []struct {
Url string `json:"url,omitempty"`
}
}
} `json:"emoji,omitempty"`
}
type ChatMessagesResponse struct {
ContinuationContents struct {
LiveChatContinuation struct {
Actions []Actions `json:"actions"`
Continuations []ContinuationChat `json:"continuations"`
} `json:"liveChatContinuation"`
} `json:"continuationContents"`
}
type ChatMessage struct {
AuthorName string
Message string
Timestamp time.Time
}
type InitialData struct {
Contents struct {
TwoColumnWatchNextResults struct {
ConversationBar struct {
LiveChatRenderer struct {
Header struct {
LiveChatHeaderRenderer struct {
ViewSelector struct {
SortFilterSubMenuRenderer struct {
SubMenuItems []SubMenuItems `json:"subMenuItems"`
}
}
}
}
}
}
}
}
}
var (
LIVE_CHAT_URL = `https://www.youtube.com/youtubei/v1/live_chat/get_%s?key=%s`
// Google would sometimes ask you to solve a CAPTCHA before accessing it's websites
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
customCookies []*http.Cookie
ErrLiveStreamOver error = errors.New("live stream over")
ErrStreamNotLive error = errors.New("stream not live")
)
const (
API_TYPE = "live_chat"
YT_CFG_REGEX = `ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;`
INITIAL_DATA_REGEX = `(?:window\s*\[\s*["\']ytInitialData["\']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;\s*(?:var\s+meta|</script|\n)`
)
func regexSearch(regex string, str string) []string {
r, _ := regexp.Compile(regex)
matches := r.FindAllString(str, -1)
return matches
}
func parseMicroSeconds(timeStampStr string) time.Time {
tm, _ := strconv.ParseInt(timeStampStr, 10, 64)
tm = tm / 1000
sec := tm / 1000
msec := tm % 1000
return time.Unix(sec, msec*int64(time.Millisecond))
}
func fetchChatMessages(initialContinuationInfo string, ytCfg YtCfg) ([]ChatMessage, string, int, error) {
apiKey := ytCfg.INNERTUBE_API_KEY
continuationUrl := fmt.Sprintf(LIVE_CHAT_URL, API_TYPE, apiKey)
innertubeContext := ytCfg.INNERTUBE_CONTEXT
context := Context{innertubeContext, initialContinuationInfo}
b, _ := json.Marshal(context)
var jsonData = []byte(b)
request, error := http.NewRequest("POST", continuationUrl, bytes.NewBuffer(jsonData))
if error != nil {
return nil, "", 0, error
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
response, error := client.Do(request)
if error != nil {
return nil, "", 0, error
}
if response.StatusCode != 200 {
return nil, "", 0, fmt.Errorf("some error fetching chat messages status code: %d", response.StatusCode)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, "", 0, err
}
response.Body.Close()
var chatMsgResp ChatMessagesResponse
json.Unmarshal([]byte(string(body)), &chatMsgResp)
actions := chatMsgResp.ContinuationContents.LiveChatContinuation.Actions
chatMessages := []ChatMessage{}
for _, action := range actions {
liveChatTextMessageRenderer := action.AddChatItemAction.Item.LiveChatTextMessageRenderer
// Each chat message is seperated into multiple runs.
// Iterate through all runs and generate the chat message.
runs := liveChatTextMessageRenderer.Message.Runs
if len(runs) > 0 {
chatMessage := ChatMessage{}
authorName := liveChatTextMessageRenderer.AuthorName.SimpleText
chatMessage.Timestamp = parseMicroSeconds(liveChatTextMessageRenderer.TimestampUsec)
chatMessage.AuthorName = authorName
text := ""
for _, run := range runs {
if run.Text != "" {
text += run.Text
} else {
if run.Emoji.IsCustomEmoji {
numberOfThumbnails := len(run.Emoji.Image.Thumbnails)
// Youtube chat has custom emojis which
// are small PNG images and cannot be displayed as text.
//
// These custom emojis are available with their image url.
//
// Adding some whitespace around custom image URLs
// without the whitespace it would be difficult to parse these URLs
if numberOfThumbnails > 0 && numberOfThumbnails == 2 {
text += " " + run.Emoji.Image.Thumbnails[1].Url + " "
} else if numberOfThumbnails == 1 {
text += " " + run.Emoji.Image.Thumbnails[0].Url + " "
}
} else {
text += run.Emoji.EmojiId
}
}
}
chatMessage.Message = text
chatMessages = append(chatMessages, chatMessage)
}
}
// No continuation returned from youtube, Stream has ended.
if len(chatMsgResp.ContinuationContents.LiveChatContinuation.Continuations) == 0 {
return nil, "", 0, ErrLiveStreamOver
}
// extract continuation and timeout received from response
timeoutMs := 5
continuations := chatMsgResp.ContinuationContents.LiveChatContinuation.Continuations[0]
if continuations.TimedContinuationData.Continuation == "" {
initialContinuationInfo = continuations.InvalidationContinuationData.Continuation
timeoutMs = continuations.InvalidationContinuationData.TimeoutMs
} else {
initialContinuationInfo = continuations.TimedContinuationData.Continuation
timeoutMs = continuations.TimedContinuationData.TimeoutMs
}
return chatMessages, initialContinuationInfo, timeoutMs, nil
}
func ParseInitialData(videoUrl string) (string, YtCfg, error) {
req, err := http.NewRequest("GET", videoUrl, nil)
if err != nil {
return "", YtCfg{}, err
}
for _, cookie := range customCookies {
req.AddCookie(cookie)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", YtCfg{}, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Print(videoUrl +
"\nresp.StatusCode: " + strconv.Itoa(resp.StatusCode))
return "", YtCfg{}, err
}
intArr, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", YtCfg{}, err
}
html := string(intArr)
// TODO :: work on regex
initialDataArr := regexSearch(INITIAL_DATA_REGEX, html)
initialData := strings.Trim(string(initialDataArr[0]), "ytInitialData = ")
initialData = strings.Trim(initialData, ";</script")
ytCfg := regexSearch(YT_CFG_REGEX, html)[0]
ytCfg = strings.Trim(ytCfg, "ytcfg.set(")
ytCfg = strings.Trim(ytCfg, ");")
var _ytCfg YtCfg
json.Unmarshal([]byte(ytCfg), &_ytCfg)
var _initialData InitialData
json.Unmarshal([]byte(initialData), &_initialData)
subMenuItems := _initialData.Contents.TwoColumnWatchNextResults.ConversationBar.LiveChatRenderer.Header.LiveChatHeaderRenderer.ViewSelector.SortFilterSubMenuRenderer.SubMenuItems
if len(subMenuItems) == 0 {
return "", YtCfg{}, ErrStreamNotLive
}
initialContinuationInfo := subMenuItems[1].Continuation.ReloadContinuationData.Continuation
return initialContinuationInfo, _ytCfg, nil
}
func FetchContinuationChat(continuation string, ytCfg YtCfg) ([]ChatMessage, string, error) {
chatMessages, continuation, timeoutMs, error := fetchChatMessages(continuation, ytCfg)
if error != nil {
return nil, "", error
}
// Sleep for timeoutMs milliseconds sent by the server
if timeoutMs > 0 {
time.Sleep(time.Duration(timeoutMs) * time.Millisecond)
} else {
time.Sleep(time.Second * 5)
}
return chatMessages, continuation, nil
}
func AddCookies(cookies []*http.Cookie) {
customCookies = cookies
}