Skip to content

Commit af487ae

Browse files
committed
fix!: rename live chat fns
1 parent 025d12c commit af487ae

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

packages/core/src/context.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { convertRunsToString, log, normalizeVideoId } from "./util";
88
export interface Context {
99
apiKey: string;
1010
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;
1517
}
1618

17-
export interface LiveChatParams {
19+
export interface LiveChatData {
1820
sendMessageParams: string | undefined;
1921
}
2022

@@ -25,7 +27,7 @@ export interface ClientInfo {
2527

2628
export interface Metadata {
2729
id: string;
28-
title: string | undefined;
30+
title: string;
2931
channelId: string;
3032
channelName: string;
3133
isLive: boolean;
@@ -121,17 +123,22 @@ function findMetadata(initialData: YTInitialData): Metadata | undefined {
121123
};
122124
}
123125

124-
async function fetchLiveChatParams(
126+
export async function fetchLiveChatData(
125127
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;
129136
const headers = withAuthHeader(credentials);
130137
const res = await fetch(url, { headers }).then((res) => res.text());
131138
const initialData = findInitialData(res);
132139
if (!initialData) {
133140
// happens when accessing to replay chat
134-
log("!liveChatInitialData: replay");
141+
log("!liveChatInitialData", initialData, url);
135142
return { sendMessageParams: undefined };
136143
}
137144

@@ -187,14 +194,14 @@ export async function fetchContext(
187194
}
188195

189196
if (continuations) {
190-
const params = await fetchLiveChatParams(continuations.top.token, {
197+
const liveChatContext = await fetchLiveChatData(continuations.top.token, {
191198
credentials,
192199
});
193200

194-
if (params) {
201+
if (liveChatContext) {
195202
context.chat = {
196203
continuations,
197-
params,
204+
data: liveChatContext,
198205
};
199206
}
200207
}

packages/core/src/message.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import fetch from "cross-fetch";
22
import { Credentials, DEFAULT_CLIENT, withAuthHeader } from "./auth";
3-
import { LiveChatParams } from "./context";
43

54
export interface SendMessageOptions {
65
apiKey: string;
76
credentials: Credentials;
8-
params: LiveChatParams;
7+
params: string;
98
}
109
export async function sendMessage(
1110
message: string,
@@ -22,7 +21,7 @@ export async function sendMessage(
2221
context: {
2322
client: DEFAULT_CLIENT,
2423
},
25-
params: params.sendMessageParams,
24+
params,
2625
};
2726

2827
const headers = withAuthHeader(credentials, {

packages/core/tests/message.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ itif("send message", async () => {
2323
const res = await sendMessage(msg, {
2424
apiKey,
2525
credentials,
26-
params: chat?.params!,
26+
params: chat?.data.sendMessageParams!,
2727
});
2828
completeRecording();
2929

0 commit comments

Comments
 (0)