Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Add chatType option for fetching "LiveChat" #94

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

// Or specify LiveID in Stream manually.
const liveChat = new LiveChat({liveId: "LIVE_ID_HERE"})

// If the second argument is `true`, fetch "LiveChat" instead of "TopChat".
const liveChat = new LiveChat({handle: "HANDLE_NAME_HERE"}, true)
```
4. Add events
```typescript
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "youtube-chat",
"version": "2.2.0",
"version": "2.2.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "https://github.com/LinaTsukusu/youtube-chat.git",
"author": "LinaTsukusu <[email protected]>",
"repository": "https://github.com/advancedbear/youtube-chat.git",
"author": "advancedbear",
"license": "MIT",
"private": false,
"scripts": {
Expand All @@ -27,21 +27,21 @@
"stream"
],
"devDependencies": {
"@types/jest": "^29.2.3",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.1.6",
"jest": "^29.3.1",
"prettier": "^2.8.0",
"ts-jest": "^29.0.3",
"@types/jest": "^29.5.1",
"@types/node": "^18.16.3",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"eslint": "^8.39.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-jest": "^27.2.1",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
"typescript": "^4.9.5"
},
"dependencies": {
"axios": "^1.2.0",
"axios": "^1.4.0",
"typed-emitter": "^2.1.0"
}
}
14 changes: 9 additions & 5 deletions src/live-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export class LiveChat extends (EventEmitter as new () => TypedEmitter<LiveChatEv
#options?: FetchOptions
readonly #interval: number = 1000
readonly #id: YoutubeId
readonly #chatType: boolean = false

constructor(id: YoutubeId, interval = 1000) {
constructor(id: YoutubeId, chatType = false, interval = 1000) {
super()
if (!id || (!("channelId" in id) && !("liveId" in id) && !("handle" in id))) {
throw TypeError("Required channelId or liveId or handle.")
Expand All @@ -31,14 +32,17 @@ export class LiveChat extends (EventEmitter as new () => TypedEmitter<LiveChatEv

this.#id = id
this.#interval = interval
this.#chatType = chatType
}

async start(): Promise<boolean> {
if (this.#observer) {
return false
}
try {
const options = await fetchLivePage(this.#id)
const options = await fetchLivePage(this.#id, this.#chatType)
if (this.#observer && this.liveId == options.liveId) {
return false
} else if (this.#observer && this.liveId != options.liveId) {
this.stop("liveID is changed")
}
this.liveId = options.liveId
this.#options = options

Expand Down
20 changes: 16 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
FetchOptions,
GetLiveChatResponse,
LiveChatMembershipItemRenderer,
LiveChatMembershipMilestoneRenderer,
LiveChatPaidMessageRenderer,
LiveChatPaidStickerRenderer,
LiveChatTextMessageRenderer,
Expand All @@ -11,7 +12,7 @@ import {
} from "./types/yt-response"
import { ChatItem, ImageItem, MessageItem } from "./types/data"

export function getOptionsFromLivePage(data: string): FetchOptions & { liveId: string } {
export function getOptionsFromLivePage(data: string, chatType?: boolean): FetchOptions & { liveId: string } {
let liveId: string
const idResult = data.match(/<link rel="canonical" href="https:\/\/www.youtube.com\/watch\?v=(.+?)">/)
if (idResult) {
Expand Down Expand Up @@ -42,9 +43,16 @@ export function getOptionsFromLivePage(data: string): FetchOptions & { liveId: s
}

let continuation: string
const continuationResult = data.match(/['"]continuation['"]:\s*['"](.+?)['"]/)
const continuationResult = data.matchAll(/['"]continuation['"]:\s*['"](.+?)['"]/g)
if (continuationResult) {
continuation = continuationResult[1]
const list = Array.from(continuationResult)
if (chatType) {
/** すべてのチャットの取得時に利用するcontinuation */
continuation = list[2][1]
} else {
/** トップチャットの取得時に利用するcontinuation */
continuation = list[1][1]
}
} else {
throw new Error("Continuation was not found")
}
Expand Down Expand Up @@ -125,6 +133,7 @@ function rendererFromAction(
| LiveChatPaidMessageRenderer
| LiveChatPaidStickerRenderer
| LiveChatMembershipItemRenderer
| LiveChatMembershipMilestoneRenderer
| null {
if (!action.addChatItemAction) {
return null
Expand All @@ -138,6 +147,8 @@ function rendererFromAction(
return item.liveChatPaidStickerRenderer
} else if (item.liveChatMembershipItemRenderer) {
return item.liveChatMembershipItemRenderer
} else if (item.LiveChatMembershipMilestoneRenderer) {
return item.LiveChatMembershipMilestoneRenderer
}
return null
}
Expand All @@ -151,10 +162,11 @@ function parseActionToChatItem(data: Action): ChatItem | null {
let message: MessageRun[] = []
if ("message" in messageRenderer) {
message = messageRenderer.message.runs
} else if ("empty" in messageRenderer) {
message = messageRenderer.headerPrimaryText.runs
} else if ("headerSubtext" in messageRenderer) {
message = messageRenderer.headerSubtext.runs
}

const authorNameText = messageRenderer.authorName?.simpleText ?? ""
const ret: ChatItem = {
id: messageRenderer.id,
Expand Down
7 changes: 5 additions & 2 deletions src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ export async function fetchChat(options: FetchOptions): Promise<[ChatItem[], str
return parseChatData(res.data)
}

export async function fetchLivePage(id: { channelId: string } | { liveId: string } | { handle: string }) {
export async function fetchLivePage(
id: { channelId: string } | { liveId: string } | { handle: string },
chatType?: boolean
) {
const url = generateLiveUrl(id)
if (!url) {
throw TypeError("not found id")
}
const res = await axios.get(url)
return getOptionsFromLivePage(res.data.toString())
return getOptionsFromLivePage(res.data.toString(), chatType)
}

function generateLiveUrl(id: YoutubeId) {
Expand Down
12 changes: 12 additions & 0 deletions src/types/yt-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export interface LiveChatPaidStickerRenderer extends MessageRendererBase {
stickerDisplayHeight: number
backgroundColor: number
authorNameTextColor: number
headerPrimaryText: {
runs: MessageRun[]
}
}

export interface LiveChatMembershipItemRenderer extends MessageRendererBase {
Expand All @@ -157,12 +160,21 @@ export interface LiveChatMembershipItemRenderer extends MessageRendererBase {
authorBadges: AuthorBadge[]
}

export interface LiveChatMembershipMilestoneRenderer extends MessageRendererBase {
headerPrimaryText: {
runs: MessageRun[]
}
empty: boolean
authorBadges: AuthorBadge[]
}

export interface AddChatItemAction {
item: {
liveChatTextMessageRenderer?: LiveChatTextMessageRenderer
liveChatPaidMessageRenderer?: LiveChatPaidMessageRenderer
liveChatMembershipItemRenderer?: LiveChatMembershipItemRenderer
liveChatPaidStickerRenderer?: LiveChatPaidStickerRenderer
LiveChatMembershipMilestoneRenderer?: LiveChatMembershipMilestoneRenderer
liveChatViewerEngagementMessageRenderer?: object
}
clientId: string
Expand Down