-
Notifications
You must be signed in to change notification settings - Fork 0
/
archivelivechat.go
494 lines (480 loc) · 19.8 KB
/
archivelivechat.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
package youtubehelper
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
)
const (
youtubeBaseUrl string = "https://www.youtube.com/watch?v="
youtubeLiveChatReplayBaseUrl string = "https://www.youtube.com/live_chat_replay?continuation="
youtubeLiveChatApiBaseUrl string = "https://www.youtube.com/youtubei/v1/live_chat/get_live_chat_replay?key="
userAgent string = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.91 Safari/537.36"
)
type ArchiveLiveChatParams map[string]string
func (a ArchiveLiveChatParams) GetContinuation() string {
return a["continuation"]
}
type ArchiveLiveChatCollector struct {
verbose bool
res map[string]*regexp.Regexp
}
func (a *ArchiveLiveChatCollector) httpRequest(url string, method string, header map[string]string, reqBody io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, fmt.Errorf("can not create http request (url = %v): %w", url, err)
}
for k, v := range header {
req.Header.Set(k, v)
}
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("can not request of http (url = %v): %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("response have unexpected status (url = %v, status = %v)", url, resp.Status)
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("can not read body (url = %v): %w", url, err)
}
return respBody, nil
}
func (a *ArchiveLiveChatCollector) getParam(re *regexp.Regexp, body []byte) (string, error) {
v := re.FindAllSubmatch(body, -1)
if len(v) == 0 {
return "", fmt.Errorf("not found parameter '%v'", re.String())
}
if len(v[0]) <= 1 {
return "", fmt.Errorf("not found parameter '%v'", re.String())
}
return string(v[0][1]), nil
}
func (a *ArchiveLiveChatCollector) GetParams(videoId string) (ArchiveLiveChatParams, error) {
ytUrl := youtubeBaseUrl + videoId
header := make(map[string]string)
header["User-Agent"] = userAgent
body, err := a.httpRequest(ytUrl, "GET", header, nil)
if err != nil {
return nil, fmt.Errorf("can not get video page (url = %v, heade = %+v): %v", ytUrl, header, err)
}
params := make(map[string]string)
params["offsetMs"] = "0"
for name, re := range a.res {
v, err := a.getParam(re, body)
if err != nil {
return nil, fmt.Errorf("can not get param (name = %v): %v", name, err)
}
params[name] = v
}
return params, nil
}
func (a *ArchiveLiveChatCollector) buildRequestBody(params ArchiveLiveChatParams) (*bytes.Buffer, error) {
request := &GetLiveChatRequest{}
request.Context.Client.Hl = params["hl"]
request.Context.Client.Gl = params["gl"]
request.Context.Client.RemoteHost = params["remoteHost"]
request.Context.Client.DeviceMake = params["deviceMake"]
request.Context.Client.DeviceModel = params["deviceModel"]
request.Context.Client.VisitorData = params["visitorData"]
request.Context.Client.UserAgent = params["userAgent"]
request.Context.Client.ClientName = params["clientName"]
request.Context.Client.ClientVersion = params["clientVersion"]
request.Context.Client.OsName = params["osName"]
request.Context.Client.OsVersion = params["osVersion"]
request.Context.Client.OriginalURL = youtubeLiveChatReplayBaseUrl + url.QueryEscape(params["continuation"])
request.Context.Client.Platform = params["platform"]
request.Context.Client.ClientFormFactor = params["clientFormFactor"]
request.Context.Client.TimeZone = "Asia/Tokyo"
request.Context.Client.BrowserName = params["browserName"]
request.Context.Client.BrowserVersion = params["browserVersion"]
request.Context.Client.UtcOffsetMinutes = 540
request.Context.Client.MainAppWebInfo.GraftURL = youtubeLiveChatReplayBaseUrl + url.QueryEscape(params["continuation"])
request.Context.Client.MainAppWebInfo.WebDisplayMode = "WEB_DISPLAY_MODE_BROWSER"
request.Context.User.LockedSafetyMode = false
request.Context.Request.UseSsl = true
request.Continuation = params["continuation"]
request.CurrentPlayerState.PlayerOffsetMs = params["offsetMs"]
requestBytes, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("can not convert struct to json: %v", err)
}
fmt.Printf("%v\n", string(requestBytes))
return bytes.NewBuffer(requestBytes), nil
}
func (a *ArchiveLiveChatCollector) GetArchiveLiveChat(params ArchiveLiveChatParams) (*GetLiveChatRespose, error) {
reqBody, err := a.buildRequestBody(params)
if err != nil {
return nil, fmt.Errorf("can not build request body: %v", err)
}
ytUrl := youtubeLiveChatApiBaseUrl + params["innertubeApiKey"]
header := make(map[string]string)
header["User-Agent"] = userAgent
header["Content-Type"] = "application/json"
respBody, err := a.httpRequest(ytUrl, "POST", header, reqBody)
if err != nil {
return nil, fmt.Errorf("can not get archive live chat (url = %v, header = %+v, request =%v): %v", ytUrl, header, string(reqBody.Bytes()), err)
}
resp := &GetLiveChatRespose{}
if err = json.Unmarshal(respBody, resp); err != nil {
return nil, fmt.Errorf("can not convert json to struct: %v", err)
}
return resp, nil
}
func (a *ArchiveLiveChatCollector) Next(params ArchiveLiveChatParams, resp *GetLiveChatRespose) bool {
params["continuation"] = ""
for _, c := range resp.ContinuationContents.LiveChatContinuation.Continuations {
if c.LiveChatReplayContinuationData.Continuation != "" {
params["continuation"] = c.LiveChatReplayContinuationData.Continuation
}
}
for i := len(resp.ContinuationContents.LiveChatContinuation.Actions) - 1; i > 0; i-- {
if resp.ContinuationContents.LiveChatContinuation.Actions[i].ReplayChatItemAction.VideoOffsetTimeMsec != "" {
params["offsetMs"] = resp.ContinuationContents.LiveChatContinuation.Actions[i].ReplayChatItemAction.VideoOffsetTimeMsec
break
}
}
if params["continuation"] == "" {
return false
} else {
return true
}
}
func NewArchiveLiveChatCollector(opts ...Option) *ArchiveLiveChatCollector {
baseOpts := defaultOptions()
for _, opt := range opts {
if opt == nil {
continue
}
opt(baseOpts)
}
res := make(map[string]*regexp.Regexp)
res["continuation"] = regexp.MustCompile(`"liveChatRenderer".+?"continuations".+?"reloadContinuationData".+?"continuation"[ ]*:[ ]*"([^"]+)"`)
res["visitorData"] = regexp.MustCompile(`"visitorData"[ ]*:[ ]*"([^"]+)"`)
res["innertubeApiKey"] = regexp.MustCompile(`"innertubeApiKey"[ ]*:[ ]*"([^"]+)"`)
res["browserName"] = regexp.MustCompile(`"browserName"[ ]*:[ ]*"([^"]+)"`)
res["browserVersion"] = regexp.MustCompile(`"browserVersion"[ ]*:[ ]*"([^"]+)"`)
res["clientName"] = regexp.MustCompile(`"clientName"[ ]*:[ ]*"([^"]+)"`)
res["clientVersion"] = regexp.MustCompile(`"clientVersion"[ ]*:[ ]*"([^"]+)"`)
res["remoteHost"] = regexp.MustCompile(`"remoteHost"[ ]*:[ ]*"([^"]+)"`)
res["gl"] = regexp.MustCompile(`"GL"[ ]*:[ ]*"([^"]+)"`)
res["hl"] = regexp.MustCompile(`"HL"[ ]*:[ ]*"([^"]+)"`)
res["osName"] = regexp.MustCompile(`"osName"[ ]*:[ ]*"([^"]+)"`)
res["osVersion"] = regexp.MustCompile(`"osVersion"[ ]*:[ ]*"([^"]+)"`)
res["deviceMake"] = regexp.MustCompile(`"deviceMake"[ ]*:[ ]*"([^"]*)"`)
res["deviceModel"] = regexp.MustCompile(`"deviceModel"[ ]*:[ ]*"([^"]*)"`)
res["userAgent"] = regexp.MustCompile(`"userAgent"[ ]*:[ ]*"([^"]+)"`)
res["platform"] = regexp.MustCompile(`"platform"[ ]*:[ ]*"([^"]+)"`)
res["clientFormFactor"] = regexp.MustCompile(`"clientFormFactor"[ ]*:[ ]*"([^"]+)"`)
return &ArchiveLiveChatCollector{
verbose: baseOpts.verbose,
res: res,
}
}
type GetLiveChatRequest struct {
Context struct {
AdSignalsInfo struct {
Params []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"params,omitempty"`
} `json:"adSignalsInfo"`
Client struct {
BrowserName string `json:"browserName"`
BrowserVersion string `json:"browserVersion"`
ClientFormFactor string `json:"clientFormFactor"`
ClientName string `json:"clientName"`
ClientVersion string `json:"clientVersion"`
ConnectionType string `json:"connectionType,omitempty"`
DeviceMake string `json:"deviceMake"`
DeviceModel string `json:"deviceModel"`
Gl string `json:"gl"`
Hl string `json:"hl"`
MainAppWebInfo struct {
GraftURL string `json:"graftUrl"`
WebDisplayMode string `json:"webDisplayMode"`
} `json:"mainAppWebInfo"`
OriginalURL string `json:"originalUrl"`
OsName string `json:"osName"`
OsVersion string `json:"osVersion"`
Platform string `json:"platform"`
RemoteHost string `json:"remoteHost"`
ScreenDensityFloat int64 `json:"screenDensityFloat,omitempty"`
ScreenHeightPoints int64 `json:"screenHeightPoints,omitempty"`
ScreenPixelDensity int64 `json:"screenPixelDensity,omitempty"`
ScreenWidthPoints int64 `json:"screenWidthPoints,omitempty"`
TimeZone string `json:"timeZone"`
UserAgent string `json:"userAgent"`
UserInterfaceTheme string `json:"userInterfaceTheme,omitempty"`
UtcOffsetMinutes int64 `json:"utcOffsetMinutes"`
VisitorData string `json:"visitorData"`
} `json:"client"`
ClientScreenNonce string `json:"clientScreenNonce,omitempty"`
Request struct {
ConsistencyTokenJars []interface{} `json:"consistencyTokenJars,omitempty"`
InternalExperimentFlags []interface{} `json:"internalExperimentFlags,,omitempty"`
UseSsl bool `json:"useSsl"`
} `json:"request"`
User struct {
LockedSafetyMode bool `json:"lockedSafetyMode"`
} `json:"user"`
} `json:"context"`
Continuation string `json:"continuation"`
CurrentPlayerState struct {
PlayerOffsetMs string `json:"playerOffsetMs"`
} `json:"currentPlayerState"`
}
type GetLiveChatRespose struct {
ContinuationContents struct {
LiveChatContinuation struct {
Actions []struct {
ReplayChatItemAction struct {
Actions []struct {
AddChatItemAction struct {
ClientID string `json:"clientId"`
Item struct {
LiveChatPaidMessageRenderer struct {
AuthorExternalChannelID string `json:"authorExternalChannelId"`
AuthorName struct {
SimpleText string `json:"simpleText"`
} `json:"authorName"`
AuthorNameTextColor int64 `json:"authorNameTextColor"`
AuthorPhoto struct {
Thumbnails []struct {
Height int64 `json:"height"`
URL string `json:"url"`
Width int64 `json:"width"`
} `json:"thumbnails"`
} `json:"authorPhoto"`
BodyBackgroundColor int64 `json:"bodyBackgroundColor"`
BodyTextColor int64 `json:"bodyTextColor"`
ContextMenuAccessibility struct {
AccessibilityData struct {
Label string `json:"label"`
} `json:"accessibilityData"`
} `json:"contextMenuAccessibility"`
ContextMenuEndpoint struct {
CommandMetadata struct {
WebCommandMetadata struct {
IgnoreNavigation bool `json:"ignoreNavigation"`
} `json:"webCommandMetadata"`
} `json:"commandMetadata"`
LiveChatItemContextMenuEndpoint struct {
Params string `json:"params"`
} `json:"liveChatItemContextMenuEndpoint"`
} `json:"contextMenuEndpoint"`
HeaderBackgroundColor int64 `json:"headerBackgroundColor"`
HeaderTextColor int64 `json:"headerTextColor"`
ID string `json:"id"`
Message struct {
Runs []struct {
Text string `json:"text"`
} `json:"runs"`
} `json:"message"`
PurchaseAmountText struct {
SimpleText string `json:"simpleText"`
} `json:"purchaseAmountText"`
TimestampColor int64 `json:"timestampColor"`
TimestampText struct {
SimpleText string `json:"simpleText"`
} `json:"timestampText"`
TimestampUsec string `json:"timestampUsec"`
} `json:"liveChatPaidMessageRenderer"`
LiveChatTextMessageRenderer struct {
AuthorBadges []struct {
LiveChatAuthorBadgeRenderer struct {
Accessibility struct {
AccessibilityData struct {
Label string `json:"label"`
} `json:"accessibilityData"`
} `json:"accessibility"`
CustomThumbnail struct {
Thumbnails []struct {
URL string `json:"url"`
} `json:"thumbnails"`
} `json:"customThumbnail"`
Tooltip string `json:"tooltip"`
} `json:"liveChatAuthorBadgeRenderer"`
} `json:"authorBadges"`
AuthorExternalChannelID string `json:"authorExternalChannelId"`
AuthorName struct {
SimpleText string `json:"simpleText"`
} `json:"authorName"`
AuthorPhoto struct {
Thumbnails []struct {
Height int64 `json:"height"`
URL string `json:"url"`
Width int64 `json:"width"`
} `json:"thumbnails"`
} `json:"authorPhoto"`
ContextMenuAccessibility struct {
AccessibilityData struct {
Label string `json:"label"`
} `json:"accessibilityData"`
} `json:"contextMenuAccessibility"`
ContextMenuEndpoint struct {
CommandMetadata struct {
WebCommandMetadata struct {
IgnoreNavigation bool `json:"ignoreNavigation"`
} `json:"webCommandMetadata"`
} `json:"commandMetadata"`
LiveChatItemContextMenuEndpoint struct {
Params string `json:"params"`
} `json:"liveChatItemContextMenuEndpoint"`
} `json:"contextMenuEndpoint"`
ID string `json:"id"`
Message struct {
Runs []struct {
Emoji struct {
EmojiID string `json:"emojiId"`
Image struct {
Accessibility struct {
AccessibilityData struct {
Label string `json:"label"`
} `json:"accessibilityData"`
} `json:"accessibility"`
Thumbnails []struct {
Height int64 `json:"height"`
URL string `json:"url"`
Width int64 `json:"width"`
} `json:"thumbnails"`
} `json:"image"`
IsCustomEmoji bool `json:"isCustomEmoji"`
SearchTerms []string `json:"searchTerms"`
Shortcuts []string `json:"shortcuts"`
} `json:"emoji"`
Text string `json:"text"`
} `json:"runs"`
} `json:"message"`
TimestampText struct {
SimpleText string `json:"simpleText"`
} `json:"timestampText"`
TimestampUsec string `json:"timestampUsec"`
} `json:"liveChatTextMessageRenderer"`
} `json:"item"`
} `json:"addChatItemAction"`
AddLiveChatTickerItemAction struct {
DurationSec string `json:"durationSec"`
Item struct {
LiveChatTickerPaidMessageItemRenderer struct {
Amount struct {
SimpleText string `json:"simpleText"`
} `json:"amount"`
AmountTextColor int64 `json:"amountTextColor"`
AuthorExternalChannelID string `json:"authorExternalChannelId"`
AuthorPhoto struct {
Accessibility struct {
AccessibilityData struct {
Label string `json:"label"`
} `json:"accessibilityData"`
} `json:"accessibility"`
Thumbnails []struct {
Height int64 `json:"height"`
URL string `json:"url"`
Width int64 `json:"width"`
} `json:"thumbnails"`
} `json:"authorPhoto"`
DurationSec int64 `json:"durationSec"`
EndBackgroundColor int64 `json:"endBackgroundColor"`
FullDurationSec int64 `json:"fullDurationSec"`
ID string `json:"id"`
ShowItemEndpoint struct {
CommandMetadata struct {
WebCommandMetadata struct {
IgnoreNavigation bool `json:"ignoreNavigation"`
} `json:"webCommandMetadata"`
} `json:"commandMetadata"`
ShowLiveChatItemEndpoint struct {
Renderer struct {
LiveChatPaidMessageRenderer struct {
AuthorExternalChannelID string `json:"authorExternalChannelId"`
AuthorName struct {
SimpleText string `json:"simpleText"`
} `json:"authorName"`
AuthorNameTextColor int64 `json:"authorNameTextColor"`
AuthorPhoto struct {
Thumbnails []struct {
Height int64 `json:"height"`
URL string `json:"url"`
Width int64 `json:"width"`
} `json:"thumbnails"`
} `json:"authorPhoto"`
BodyBackgroundColor int64 `json:"bodyBackgroundColor"`
BodyTextColor int64 `json:"bodyTextColor"`
ContextMenuAccessibility struct {
AccessibilityData struct {
Label string `json:"label"`
} `json:"accessibilityData"`
} `json:"contextMenuAccessibility"`
ContextMenuEndpoint struct {
CommandMetadata struct {
WebCommandMetadata struct {
IgnoreNavigation bool `json:"ignoreNavigation"`
} `json:"webCommandMetadata"`
} `json:"commandMetadata"`
LiveChatItemContextMenuEndpoint struct {
Params string `json:"params"`
} `json:"liveChatItemContextMenuEndpoint"`
} `json:"contextMenuEndpoint"`
HeaderBackgroundColor int64 `json:"headerBackgroundColor"`
HeaderTextColor int64 `json:"headerTextColor"`
ID string `json:"id"`
Message struct {
Runs []struct {
Text string `json:"text"`
} `json:"runs"`
} `json:"message"`
PurchaseAmountText struct {
SimpleText string `json:"simpleText"`
} `json:"purchaseAmountText"`
TimestampColor int64 `json:"timestampColor"`
TimestampText struct {
SimpleText string `json:"simpleText"`
} `json:"timestampText"`
TimestampUsec string `json:"timestampUsec"`
} `json:"liveChatPaidMessageRenderer"`
} `json:"renderer"`
} `json:"showLiveChatItemEndpoint"`
} `json:"showItemEndpoint"`
StartBackgroundColor int64 `json:"startBackgroundColor"`
} `json:"liveChatTickerPaidMessageItemRenderer"`
} `json:"item"`
} `json:"addLiveChatTickerItemAction"`
} `json:"actions"`
VideoOffsetTimeMsec string `json:"videoOffsetTimeMsec"`
} `json:"replayChatItemAction"`
} `json:"actions"`
Continuations []struct {
LiveChatReplayContinuationData struct {
Continuation string `json:"continuation"`
TimeUntilLastMessageMsec int64 `json:"timeUntilLastMessageMsec"`
} `json:"liveChatReplayContinuationData"`
PlayerSeekContinuationData struct {
Continuation string `json:"continuation"`
} `json:"playerSeekContinuationData"`
} `json:"continuations"`
} `json:"liveChatContinuation"`
} `json:"continuationContents"`
ResponseContext struct {
MainAppWebResponseContext struct {
DatasyncID string `json:"datasyncId"`
LoggedOut bool `json:"loggedOut"`
} `json:"mainAppWebResponseContext"`
ServiceTrackingParams []struct {
Params []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"params"`
Service string `json:"service"`
} `json:"serviceTrackingParams"`
WebResponseContextExtensionData struct {
HasDecorated bool `json:"hasDecorated"`
} `json:"webResponseContextExtensionData"`
} `json:"responseContext"`
}