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/membership gift #78

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
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
31 changes: 31 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LiveChatMembershipItemRenderer,
LiveChatPaidMessageRenderer,
LiveChatPaidStickerRenderer,
LiveChatSponsorshipsHeaderRenderer,
LiveChatTextMessageRenderer,
MessageRun,
Thumbnail,
Expand Down Expand Up @@ -117,6 +118,13 @@ function parseMessages(runs: MessageRun[]): MessageItem[] {
})
}

/** メンバーシップギフトを他のRendererに揃えるためのinterface */
interface LiveChatMembershipGiftRenderer extends LiveChatSponsorshipsHeaderRenderer {
id: string
timestampUsec: string
authorExternalChannelId: string
}

/** actionの種類を判別してRendererを返す */
function rendererFromAction(
action: Action
Expand All @@ -125,6 +133,7 @@ function rendererFromAction(
| LiveChatPaidMessageRenderer
| LiveChatPaidStickerRenderer
| LiveChatMembershipItemRenderer
| LiveChatMembershipGiftRenderer
| null {
if (!action.addChatItemAction) {
return null
Expand All @@ -138,6 +147,14 @@ function rendererFromAction(
return item.liveChatPaidStickerRenderer
} else if (item.liveChatMembershipItemRenderer) {
return item.liveChatMembershipItemRenderer
} else if (item.liveChatSponsorshipsGiftPurchaseAnnouncementRenderer) {
const parentRenderer = item.liveChatSponsorshipsGiftPurchaseAnnouncementRenderer
return {
id: parentRenderer.id,
timestampUsec: parentRenderer.timestampUsec,
authorExternalChannelId: parentRenderer.authorExternalChannelId,
...parentRenderer.header.liveChatSponsorshipsHeaderRenderer,
}
}
return null
}
Expand Down Expand Up @@ -210,6 +227,20 @@ function parseActionToChatItem(data: Action): ChatItem | null {
amount: messageRenderer.purchaseAmountText.simpleText,
color: convertColorToHex6(messageRenderer.bodyBackgroundColor),
}
} else if (
data.addChatItemAction?.item.liveChatSponsorshipsGiftPurchaseAnnouncementRenderer &&
"primaryText" in messageRenderer &&
messageRenderer.primaryText.runs
) {
ret.membershipGift = {
message: parseMessages(messageRenderer.primaryText.runs),
}
if (messageRenderer.image?.thumbnails?.[0]) {
ret.membershipGift.image = {
...messageRenderer.image.thumbnails[0],
alt: "",
}
}
}

return ret
Expand Down
4 changes: 4 additions & 0 deletions src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface ChatItem {
color: string
sticker?: ImageItem
}
membershipGift?: {
message: MessageItem[]
image?: ImageItem
}
isMembership: boolean
isVerified: boolean
isOwner: boolean
Expand Down
42 changes: 42 additions & 0 deletions src/types/yt-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,54 @@ export interface LiveChatMembershipItemRenderer extends MessageRendererBase {
authorBadges: AuthorBadge[]
}

export interface LiveChatSponsorshipsGiftPurchaseAnnouncementRenderer {
id: string
timestampUsec: string
authorExternalChannelId: string
header: {
liveChatSponsorshipsHeaderRenderer: LiveChatSponsorshipsHeaderRenderer
}
}

export interface LiveChatSponsorshipsHeaderRenderer {
primaryText: {
runs: MessageRun[]
}
image?: {
thumbnails?: { url: string }[]
}
authorName?: {
simpleText: string
}
authorPhoto: {
thumbnails: Thumbnail[]
}
authorBadges?: AuthorBadge[]
contextMenuEndpoint: {
clickTrackingParams: string
commandMetadata: {
webCommandMetadata: {
ignoreNavigation: true
}
}
liveChatItemContextMenuEndpoint: {
params: string
}
}
contextMenuAccessibility: {
accessibilityData: {
label: string
}
}
}

export interface AddChatItemAction {
item: {
liveChatTextMessageRenderer?: LiveChatTextMessageRenderer
liveChatPaidMessageRenderer?: LiveChatPaidMessageRenderer
liveChatMembershipItemRenderer?: LiveChatMembershipItemRenderer
liveChatPaidStickerRenderer?: LiveChatPaidStickerRenderer
liveChatSponsorshipsGiftPurchaseAnnouncementRenderer?: LiveChatSponsorshipsGiftPurchaseAnnouncementRenderer
liveChatViewerEngagementMessageRenderer?: object
}
clientId: string
Expand Down
36 changes: 36 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,42 @@ describe("Parser", () => {
])
})

test("Membership Gift", () => {
const res = JSON.parse(readFileSync(__dirname + "/testdata/get_live_chat.membership-gift.json").toString())
const [chatItems, continuation] = parseChatData(res)
expect(continuation).toBe("test-continuation:01")
expect(chatItems).toMatchObject([
{
author: {
name: "authorName",
thumbnail: {
url: "https://author.thumbnail.url",
alt: "authorName",
},
channelId: "channelId",
},
message: [],
membershipGift: {
message: [
{ text: "10" },
{ text: " 件の " },
{ text: "上級エンジニア" },
{ text: " のメンバーシップ ギフトを贈りました" },
],
image: {
url: "https://www.gstatic.com/youtube/img/sponsorships/sponsorships_gift_purchase_announcement_artwork.png",
alt: "",
},
},
isMembership: false,
isVerified: false,
isOwner: false,
isModerator: false,
timestamp: new Date("2021-01-01"),
},
])
})

test("From Verified User", () => {
const res = JSON.parse(readFileSync(__dirname + "/testdata/get_live_chat.from-verified.json").toString())
const [chatItems, continuation] = parseChatData(res)
Expand Down
168 changes: 168 additions & 0 deletions test/testdata/get_live_chat.membership-gift.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"responseContext": {
"serviceTrackingParams": [
{
"service": "CSI",
"params": [
{
"key": "c",
"value": "WEB"
},
{
"key": "cver",
"value": "2.20211119.09.00"
},
{
"key": "yt_li",
"value": "0"
},
{
"key": "GetLiveChat_rid",
"value": "0x05d2923065b2295c"
}
]
},
{
"service": "GFEEDBACK",
"params": [
{
"key": "logged_in",
"value": "0"
},
{
"key": "e",
"value": "24115586,24034168,24137390,24106921,24132435,24129452,24113096,24002025,39321281,24027701,24135287,23983296,24131029,23857950,24118516,24007790,23934970,23744176,24113224,24016904,24080738,24134829,23918597,24049820,24002022,24113538,24028143,24115508,24132376,24084440,24095695,23968386,24131277,24036948,24064555,23986025,24109689,24001373,24077241,24004644,24116916,39321426,24116772,24116735,24007246,24129402,24129776,24136255,23944779,24058380,23998056,24128612,24082661,23882502,23804281,24113699,24130238,23885487,24085811,24077266,23946420,24106407,24110902,24106839,1714247,24116717,24111165,24106628,24114970,24126458,23884386,23966208"
}
]
},
{
"service": "GUIDED_HELP",
"params": [
{
"key": "logged_in",
"value": "0"
}
]
},
{
"service": "ECATCHER",
"params": [
{
"key": "client.version",
"value": "2.20211119"
},
{
"key": "client.name",
"value": "WEB"
},
{
"key": "client.fexp",
"value": "24115586,24034168,24137390,24106921,24132435,24129452,24113096,24002025,39321281,24027701,24135287,23983296,24131029,23857950,24118516,24007790,23934970,23744176,24113224,24016904,24080738,24134829,23918597,24049820,24002022,24113538,24028143,24115508,24132376,24084440,24095695,23968386,24131277,24036948,24064555,23986025,24109689,24001373,24077241,24004644,24116916,39321426,24116772,24116735,24007246,24129402,24129776,24136255,23944779,24058380,23998056,24128612,24082661,23882502,23804281,24113699,24130238,23885487,24085811,24077266,23946420,24106407,24110902,24106839,1714247,24116717,24111165,24106628,24114970,24126458,23884386,23966208"
}
]
}
],
"mainAppWebResponseContext": {
"loggedOut": true
},
"webResponseContextExtensionData": {
"hasDecorated": true
}
},
"continuationContents": {
"liveChatContinuation": {
"continuations": [
{
"invalidationContinuationData": {
"invalidationId": {
"objectSource": 1056,
"objectId": "objectId",
"topic": "topic",
"subscribeToGcmTopics": true,
"protoCreationTimestampMs": "1637648016661"
},
"timeoutMs": 10000,
"continuation": "test-continuation:01"
}
}
],
"actions": [
{
"addChatItemAction": {
"item": {
"liveChatSponsorshipsGiftPurchaseAnnouncementRenderer": {
"id": "id",
"timestampUsec": "1609459200000000",
"authorExternalChannelId": "channelId",
"header": {
"liveChatSponsorshipsHeaderRenderer": {
"authorName": {
"simpleText": "authorName"
},
"authorPhoto": {
"thumbnails": [
{
"url": "https://author.thumbnail.url",
"width": 32,
"height": 32
},
{
"url": "https://author.thumbnail.url",
"width": 64,
"height": 64
}
]
},
"primaryText": {
"runs": [
{
"text": "10",
"bold": true
},
{
"text": " 件の ",
"bold": true
},
{
"text": "上級エンジニア",
"bold": true
},
{
"text": " のメンバーシップ ギフトを贈りました",
"bold": true
}
]
},
"contextMenuEndpoint": {
"clickTrackingParams": "CAEQl98BIhMI07io_reR-AIV1LRWAR1kDQ1K",
"commandMetadata": {
"webCommandMetadata": {
"ignoreNavigation": true
}
},
"liveChatItemContextMenuEndpoint": {
"params": "Q2g0S0hBb2FRMDV4Y1ROMllUTnJabWREUmxKTlQyWlJiMlJMU1hkSmQzY2FLU29uQ2hoVlEyUjVjVUZoV2tSTFNGaG5ORUZvYVRkV1JVNVVhRkVTQzI0NGNWSndhVlpYWDJGRklBRW9CRElhQ2hoVlF6TnJYMjFHZURkRGVsOWhhRXM0Y0RSVWNsbzVTMUU0QWtnQVVDUSUzRA=="
}
},
"contextMenuAccessibility": {
"accessibilityData": {
"label": "チャットの操作"
}
},
"image": {
"thumbnails": [
{
"url": "https://www.gstatic.com/youtube/img/sponsorships/sponsorships_gift_purchase_announcement_artwork.png"
}
]
}
}
}
}
}
}
}
]
}
}
}