@@ -8,13 +8,15 @@ import { convertRunsToString, log, normalizeVideoId } from "./util";
8
8
export interface Context {
9
9
apiKey : string ;
10
10
metadata : Metadata ;
11
- chat ?: {
12
- continuations : ReloadContinuationItems ;
13
- params : LiveChatParams ;
14
- } ;
11
+ chat ?: LiveChatContext ;
12
+ }
13
+
14
+ export interface LiveChatContext {
15
+ continuations : ReloadContinuationItems ;
16
+ data : LiveChatData ;
15
17
}
16
18
17
- export interface LiveChatParams {
19
+ export interface LiveChatData {
18
20
sendMessageParams : string | undefined ;
19
21
}
20
22
@@ -25,7 +27,7 @@ export interface ClientInfo {
25
27
26
28
export interface Metadata {
27
29
id : string ;
28
- title : string | undefined ;
30
+ title : string ;
29
31
channelId : string ;
30
32
channelName : string ;
31
33
isLive : boolean ;
@@ -121,17 +123,22 @@ function findMetadata(initialData: YTInitialData): Metadata | undefined {
121
123
} ;
122
124
}
123
125
124
- async function fetchLiveChatParams (
126
+ export async function fetchLiveChatData (
125
127
continuation : string ,
126
- { credentials } : { credentials ?: Credentials } = { }
127
- ) : Promise < LiveChatParams | undefined > {
128
- const url = "https://www.youtube.com/live_chat?continuation=" + continuation ;
128
+ {
129
+ credentials,
130
+ isReplay = false ,
131
+ } : { credentials ?: Credentials ; isReplay ?: boolean } = { }
132
+ ) : Promise < LiveChatData | undefined > {
133
+ const endpoint = isReplay ? "live_chat_replay" : "live_chat" ;
134
+ const url =
135
+ `https://www.youtube.com/${ endpoint } ?continuation=` + continuation ;
129
136
const headers = withAuthHeader ( credentials ) ;
130
137
const res = await fetch ( url , { headers } ) . then ( ( res ) => res . text ( ) ) ;
131
138
const initialData = findInitialData ( res ) ;
132
139
if ( ! initialData ) {
133
140
// happens when accessing to replay chat
134
- log ( "!liveChatInitialData: replay" ) ;
141
+ log ( "!liveChatInitialData" , initialData , url ) ;
135
142
return { sendMessageParams : undefined } ;
136
143
}
137
144
@@ -187,14 +194,14 @@ export async function fetchContext(
187
194
}
188
195
189
196
if ( continuations ) {
190
- const params = await fetchLiveChatParams ( continuations . top . token , {
197
+ const liveChatContext = await fetchLiveChatData ( continuations . top . token , {
191
198
credentials,
192
199
} ) ;
193
200
194
- if ( params ) {
201
+ if ( liveChatContext ) {
195
202
context . chat = {
196
203
continuations,
197
- params ,
204
+ data : liveChatContext ,
198
205
} ;
199
206
}
200
207
}
0 commit comments