youtube-live-connector 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,224 @@
1
+ # youtube-live-connector
2
+
3
+ [![Node.js Version](https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen.svg)](https://nodejs.org/)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ **youtube-live-connector** เป็นไลบรารี Node.js สำหรับเชื่อมต่อกับ **YouTube Live** เพื่อดึงข้อมูลแชท/คอมเมนต์, ของขวัญ (Super Chat, Super Sticker, สมาชิก/Memberships), และจำนวนคนดูสดแบบ Real-time โดยออกแบบโครงสร้าง Event-driven ให้ใช้งานง่ายเหมือน [tiktok-live-connector](https://github.com/zerodytrash/TikTok-Live-Connector)
7
+
8
+ > 💡 **จุดเด่น**: ไม่ต้องใช้ Google Cloud Console API Key (Keyless) และไม่มีปัญหาโควต้า API Quota รายวันเต็ม!
9
+
10
+ ---
11
+
12
+ ## สารบัญ / Table of Contents
13
+ - [คุณสมบัติเด่น (Features)](#คุณสมบัติเด่น-features)
14
+ - [การติดตั้ง (Installation)](#การติดตั้ง-installation)
15
+ - [เริ่มต้นใช้งาน (Quick Start)](#เริ่มต้นใช้งาน-quick-start)
16
+ - [รายการ Events ทั้งหมด (Events List)](#รายการ-events-ทั้งหมด-events-list)
17
+ - [`connected`](#event-connected)
18
+ - [`chat`](#event-chat)
19
+ - [`gift` (Super Chat, Sticker, Membership)](#event-gift)
20
+ - [`viewers` / `roomUser`](#event-viewers--roomuser)
21
+ - [รองรับ YouTube Link ทุกรูปแบบ](#รองรับ-youtube-link-ทุกรูปแบบ)
22
+ - [ตัวเลือกการตั้งค่า (Options)](#ตัวเลือกการตั้งค่า-options)
23
+ - [ตัวอย่างการใช้งานแบบเต็ม (Full Example)](#ตัวอย่างการใช้งานแบบเต็ม-full-example)
24
+
25
+ ---
26
+
27
+ ## คุณสมบัติเด่น (Features)
28
+ - 🔗 **รองรับลิงก์ YouTube หลากหลายรูปแบบ**: ลิงก์ Live (`/live/...`), ลิงก์ Watch (`/watch?v=...`), ลิงก์สั้น (`youtu.be`), ลิงก์ช่อง (`@channel/live`), หรือ Video ID 11 ตัว
29
+ - 💬 **ดึงคอมเมนต์ (Live Chat)**: ข้อความ, อีโมจิ (รวม Custom Emojis), ชื่อผู้ส่ง, รูปโปรไฟล์, ป้ายสถานะ (Owner, Moderator, Member, Verified)
30
+ - 🎁 **ดึงข้อมูลของขวัญ / การสนับสนุน (Monetization & Gifts)**:
31
+ - **Super Chat**: ยอดเงิน, สกุลเงิน, ข้อความ, สีของแบนเนอร์
32
+ - **Super Sticker**: รูปสติกเกอร์, ยอดเงิน, สกุลเงิน
33
+ - **Memberships**: สมาชิกใหม่, การต่ออายุสมาชิก
34
+ - **Gift Memberships**: การแจกของขวัญสมาชิกให้ผู้ชมในช่อง
35
+ - 👥 **ดึงจำนวนคนดูสด (Live Viewers)**: อัปเดตยอดคนดูสดและยอดไลก์ต่อเนื่องแบบ Real-time
36
+ - ⚡ **Zero External Dependencies**: สร้างด้วย Node.js มาตรฐาน ไม่ต้องติดตั้งแพ็กเกจภายนอกเพิ่ม ทำงานได้รวดเร็วและเบามาก
37
+ - 📘 **TypeScript Ready**: มี `index.d.ts` รองรับ Auto-complete ใน VS Code และ TypeScript ทันที
38
+
39
+ ---
40
+
41
+ ## การติดตั้ง (Installation)
42
+
43
+ ```bash
44
+ npm install youtube-live-connector
45
+ ```
46
+
47
+ *(หรือหากนำโปรเจกต์นี้ไปใส่ในโฟลเดอร์งาน สามารถเรียกใช้ได้ทันทีโดยไม่ต้อง install dependency เพิ่มเติม)*
48
+
49
+ ---
50
+
51
+ ## เริ่มต้นใช้งาน (Quick Start)
52
+
53
+ ```javascript
54
+ const { YouTubeLiveConnector } = require('youtube-live-connector');
55
+
56
+ // สร้าง Connector โดยใส่ลิงก์ YouTube Live หรือ Video ID
57
+ const live = new YouTubeLiveConnector('https://www.youtube.com/watch?v=VIDEO_ID');
58
+
59
+ // 1. รับการแจ้งเตือนเมื่อเชื่อมต่อสำเร็จ
60
+ live.on('connected', (info) => {
61
+ console.log(`เชื่อมต่อสำเร็จ: ${info.title} (คนดูสดเริ่มต้น: ${info.viewerCount})`);
62
+ });
63
+
64
+ // 2. ดึงคอมเมนต์แชทสด
65
+ live.on('chat', (data) => {
66
+ console.log(`[แชท] ${data.author.name}: ${data.message}`);
67
+ });
68
+
69
+ // 3. ดึงของขวัญ (Super Chat, Super Sticker, Membership)
70
+ live.on('gift', (gift) => {
71
+ if (gift.type === 'superchat') {
72
+ console.log(`[Super Chat] ${gift.author.name} ส่ง ${gift.amountDisplay}: "${gift.message}"`);
73
+ } else if (gift.type === 'supersticker') {
74
+ console.log(`[Super Sticker] ${gift.author.name} ส่งสติกเกอร์มูลค่า ${gift.amountDisplay}`);
75
+ } else if (gift.type === 'membership') {
76
+ console.log(`[Membership] ${gift.author.name} สมัครสมาชิก: ${gift.headerText}`);
77
+ } else if (gift.type === 'membership_gift') {
78
+ console.log(`[Gift Memberships] ${gift.author.name} แจกสมาชิกจำนวน ${gift.giftCount} คน!`);
79
+ }
80
+ });
81
+
82
+ // 4. ดึงจำนวนคนดูสด (อัปเดตแบบเรียลไทม์)
83
+ live.on('viewers', (data) => {
84
+ console.log(`[คนดูสด] ${data.viewerCount.toLocaleString()} คน (${data.viewerCountDisplay})`);
85
+ });
86
+
87
+ // เริ่มเชื่อมต่อ
88
+ live.connect().catch(console.error);
89
+ ```
90
+
91
+ ---
92
+
93
+ ## รายการ Events ทั้งหมด (Events List)
94
+
95
+ ### Event: `connected`
96
+ ส่งออกเมื่อเชื่อมต่อกับ YouTube Live สำเร็จ
97
+ ```javascript
98
+ live.on('connected', (info) => {
99
+ console.log(info.videoId); // 'sgQT3zcN1u4'
100
+ console.log(info.title); // 'หัวข้อสตรีมสด'
101
+ console.log(info.channelName); // 'ชื่อช่อง'
102
+ console.log(info.channelUrl); // 'https://www.youtube.com/@...'
103
+ console.log(info.viewerCount); // จำนวนคนดูสด (ตัวเลข)
104
+ console.log(info.viewerCountDisplay);// e.g. "3,188 watching now"
105
+ console.log(info.url); // URL ของสตรีม
106
+ });
107
+ ```
108
+
109
+ ### Event: `chat`
110
+ ส่งออกเมื่อมีคอมเมนต์/ข้อความใหม่เข้ามา
111
+ ```javascript
112
+ live.on('chat', (chat) => {
113
+ console.log(chat.id); // Message ID
114
+ console.log(chat.message); // ข้อความธรรมดา (รวม emoji shortcut)
115
+ console.log(chat.author.name); // ชื่อผู้ส่ง
116
+ console.log(chat.author.channelId); // Channel ID ของผู้ส่ง
117
+ console.log(chat.author.profilePictureUrl); // รูปโปรไฟล์
118
+ console.log(chat.author.isOwner); // เจ้าของช่องหรือไม่ (true/false)
119
+ console.log(chat.author.isModerator);// แอดมิน/ผู้ดูแลหรือไม่ (true/false)
120
+ console.log(chat.author.isMember); // เป็นสมาชิกช่องหรือไม่ (true/false)
121
+ console.log(chat.emojis); // Array รายการอีโมจิในข้อความ
122
+ console.log(chat.timestamp); // Date object
123
+ });
124
+ ```
125
+
126
+ ### Event: `gift`
127
+ Event รวมสำหรับของขวัญ/การสนับสนุนทางการเงินทั้งหมด ประกอบด้วย 4 รูปแบบ:
128
+ 1. `type: 'superchat'`:
129
+ - `amount`: ตัวเลขจำนวนเงิน (เช่น `100`)
130
+ - `currency`: สกุลเงิน (เช่น `฿`, `$`)
131
+ - `amountDisplay`: ข้อความแสดงยอดเงิน (เช่น `"฿100.00"`)
132
+ - `message`: ข้อความที่แนบมากับ Super Chat
133
+ - `colors`: สีของแบนเนอร์ (headerBackgroundColor, bodyBackgroundColor)
134
+ 2. `type: 'supersticker'`:
135
+ - `amount`: จำนวนเงิน
136
+ - `sticker.url`: URL รูปสติกเกอร์
137
+ - `sticker.alt`: คำอธิบายสติกเกอร์
138
+ 3. `type: 'membership'`:
139
+ - `headerText`: e.g. `"Member for 6 months"`
140
+ - `message`: ข้อความฉลองการเป็นสมาชิก
141
+ 4. `type: 'membership_gift'`:
142
+ - `giftCount`: จำนวนสมาชิกที่แจก (e.g. `5`)
143
+ - `headerText`: e.g. `"Gifted 5 channel memberships"`
144
+
145
+ > 💡 **นอกจากนี้ยังสามารถแยกฟังเฉพาะ Event ย่อยได้ตามต้องการ**:
146
+ > - `live.on('superchat', data => ...)`
147
+ > - `live.on('supersticker', data => ...)`
148
+ > - `live.on('member', data => ...)`
149
+ > - `live.on('memberGift', data => ...)`
150
+
151
+ ### Event: `viewers` (หรือ `roomUser`)
152
+ ส่งออกทุกครั้งที่จำนวนคนดูสดมีการเปลี่ยนแปลง (มี alias `roomUser` เพื่อให้เข้ากันได้กับ tiktok-live-connector)
153
+ ```javascript
154
+ live.on('viewers', (data) => {
155
+ console.log(data.viewerCount); // 3125
156
+ console.log(data.viewerCountDisplay); // "3,125 watching now"
157
+ console.log(data.timestamp); // Date object
158
+ });
159
+ ```
160
+
161
+ ### Event อื่นๆ:
162
+ - `title`: เมื่อมีการเปลี่ยนชื่อไลฟ์สตรีม `{ title: string }`
163
+ - `like`: เมื่อยอดไลก์อัปเดต `{ likeCount: string }`
164
+ - `disconnected`: เมื่อการเชื่อมต่อถูกตัด `{ reason: string }`
165
+ - `warning`: คำเตือน (เช่น ไลฟ์นี้ถูกปิดคอมเมนต์)
166
+ - `error`: ข้อผิดพลาดในการเชื่อมต่อ
167
+ - `raw`: รับข้อมูลดิบ InnerTube Action ทั้งหมดจาก YouTube
168
+
169
+ ---
170
+
171
+ ## รองรับ YouTube Link ทุกรูปแบบ
172
+
173
+ คุณสามารถส่งลิงก์ในรูปแบบใดก็ได้เข้าสู่ `YouTubeLiveConnector`:
174
+ ```javascript
175
+ // 1. Standard Watch URL
176
+ new YouTubeLiveConnector('https://www.youtube.com/watch?v=sgQT3zcN1u4');
177
+
178
+ // 2. YouTube Live URL
179
+ new YouTubeLiveConnector('https://www.youtube.com/live/sgQT3zcN1u4');
180
+
181
+ // 3. Short URL
182
+ new YouTubeLiveConnector('https://youtu.be/sgQT3zcN1u4');
183
+
184
+ // 4. Channel Live Handle URL (ระบบจะดึง Live ที่กำลังออกอากาศสดอยู่ให้โดยอัตโนมัติ)
185
+ new YouTubeLiveConnector('https://www.youtube.com/@ChannelName/live');
186
+
187
+ // 5. Raw Video ID 11 ตัว
188
+ new YouTubeLiveConnector('sgQT3zcN1u4');
189
+ ```
190
+
191
+ ---
192
+
193
+ ## ตัวเลือกการตั้งค่า (Options)
194
+
195
+ ```javascript
196
+ const live = new YouTubeLiveConnector({
197
+ url: 'https://www.youtube.com/watch?v=sgQT3zcN1u4',
198
+ pollViewers: true, // เปิด/ปิด การวนอ่านจำนวนคนดูสด (default: true)
199
+ viewerIntervalMs: 5000, // ความถี่ในการอัปเดตคนดูสด (default: 5000 ms)
200
+ chatIntervalMs: 2000, // กำหนดความถี่อ่านแชท (ถ้าไม่กำหนดจะใช้ตามที่ YouTube แนะนำ)
201
+ ignoreInitialChat: false, // ข้ามแชทเก่าที่มีอยู่ก่อนหน้าบนหน้าจอหรือไม่ (default: false)
202
+ headers: { ... } // ปรับแต่ง HTTP headers เพิ่มเติม (ถ้ามี)
203
+ });
204
+ ```
205
+
206
+ ---
207
+
208
+ ## ตัวอย่างการใช้งานแบบเต็ม (Full Example)
209
+
210
+ สามารถรันตัวอย่างที่เตรียมไว้ได้ทันที:
211
+
212
+ ```bash
213
+ # รันตัวอย่างพื้นฐาน
214
+ npm run example:basic
215
+
216
+ # รันตัวอย่างแบบดักฟังทุก Event พร้อมแสดงข้อความแบบจัดเต็ม
217
+ node examples/full-listener.js https://www.youtube.com/watch?v=YOUR_LIVE_ID
218
+ ```
219
+
220
+ ---
221
+
222
+ ## License
223
+
224
+ MIT License
package/index.d.ts ADDED
@@ -0,0 +1,168 @@
1
+ import { EventEmitter } from 'events';
2
+
3
+ export interface AuthorBadge {
4
+ label: string;
5
+ iconType: string;
6
+ iconUrl: string;
7
+ }
8
+
9
+ export interface ChatAuthor {
10
+ name: string;
11
+ channelId: string;
12
+ profilePictureUrl: string;
13
+ isOwner: boolean;
14
+ isModerator: boolean;
15
+ isMember: boolean;
16
+ isVerified: boolean;
17
+ badges: AuthorBadge[];
18
+ }
19
+
20
+ export interface EmojiItem {
21
+ emojiId: string;
22
+ name: string;
23
+ isCustom: boolean;
24
+ url: string;
25
+ }
26
+
27
+ export interface ChatMessage {
28
+ id: string;
29
+ author: ChatAuthor;
30
+ message: string;
31
+ messageRuns: any[];
32
+ emojis: EmojiItem[];
33
+ timestamp: Date;
34
+ timestampUsec: number;
35
+ raw: any;
36
+ }
37
+
38
+ export interface SuperChatGift {
39
+ type: 'superchat';
40
+ id: string;
41
+ author: ChatAuthor;
42
+ amount: number;
43
+ currency: string;
44
+ amountDisplay: string;
45
+ message: string;
46
+ emojis: EmojiItem[];
47
+ colors: {
48
+ headerBackgroundColor: string | null;
49
+ headerTextColor: string | null;
50
+ bodyBackgroundColor: string | null;
51
+ bodyTextColor: string | null;
52
+ };
53
+ timestamp: Date;
54
+ timestampUsec: number;
55
+ raw: any;
56
+ }
57
+
58
+ export interface SuperStickerGift {
59
+ type: 'supersticker';
60
+ id: string;
61
+ author: ChatAuthor;
62
+ amount: number;
63
+ currency: string;
64
+ amountDisplay: string;
65
+ sticker: {
66
+ url: string;
67
+ alt: string;
68
+ };
69
+ backgroundColor: string | null;
70
+ timestamp: Date;
71
+ timestampUsec: number;
72
+ raw: any;
73
+ }
74
+
75
+ export interface MembershipGift {
76
+ type: 'membership';
77
+ id: string;
78
+ author: ChatAuthor;
79
+ headerText: string;
80
+ subtext: string;
81
+ message: string;
82
+ emojis: EmojiItem[];
83
+ timestamp: Date;
84
+ timestampUsec: number;
85
+ raw: any;
86
+ }
87
+
88
+ export interface MembershipGiftPurchase {
89
+ type: 'membership_gift';
90
+ id: string;
91
+ author: ChatAuthor;
92
+ giftCount: number;
93
+ headerText: string;
94
+ timestamp: Date;
95
+ timestampUsec: number;
96
+ raw: any;
97
+ }
98
+
99
+ export type GiftEvent = SuperChatGift | SuperStickerGift | MembershipGift | MembershipGiftPurchase;
100
+
101
+ export interface ViewersData {
102
+ viewerCount: number;
103
+ viewerCountDisplay: string;
104
+ timestamp: Date;
105
+ }
106
+
107
+ export interface StreamInfo {
108
+ videoId: string;
109
+ title: string;
110
+ channelName: string;
111
+ channelId: string;
112
+ channelUrl: string;
113
+ viewerCount: number;
114
+ viewerCountDisplay: string;
115
+ likeCount: string;
116
+ isLive: boolean;
117
+ url: string;
118
+ }
119
+
120
+ export interface ConnectorOptions {
121
+ url?: string;
122
+ liveId?: string;
123
+ channel?: string;
124
+ pollViewers?: boolean;
125
+ viewerIntervalMs?: number;
126
+ chatIntervalMs?: number;
127
+ ignoreInitialChat?: boolean;
128
+ headers?: Record<string, string>;
129
+ }
130
+
131
+ export declare class YouTubeLiveConnector extends EventEmitter {
132
+ constructor(options?: ConnectorOptions | string);
133
+
134
+ connect(url?: string): Promise<StreamInfo>;
135
+ disconnect(reason?: string): void;
136
+ isConnected(): boolean;
137
+ getStreamInfo(): StreamInfo | null;
138
+
139
+ on(event: 'connected', listener: (streamInfo: StreamInfo) => void): this;
140
+ on(event: 'disconnected', listener: (data: { reason: string }) => void): this;
141
+ on(event: 'chat', listener: (message: ChatMessage) => void): this;
142
+ on(event: 'gift', listener: (gift: GiftEvent) => void): this;
143
+ on(event: 'superchat', listener: (superChat: SuperChatGift) => void): this;
144
+ on(event: 'supersticker', listener: (sticker: SuperStickerGift) => void): this;
145
+ on(event: 'member', listener: (member: MembershipGift) => void): this;
146
+ on(event: 'memberGift', listener: (memberGift: MembershipGiftPurchase) => void): this;
147
+ on(event: 'viewers', listener: (data: ViewersData) => void): this;
148
+ on(event: 'roomUser', listener: (data: ViewersData) => void): this;
149
+ on(event: 'title', listener: (data: { title: string }) => void): this;
150
+ on(event: 'like', listener: (data: { likeCount: string }) => void): this;
151
+ on(event: 'warning', listener: (data: { type: string; message: string }) => void): this;
152
+ on(event: 'error', listener: (err: Error) => void): this;
153
+ on(event: 'raw', listener: (action: any) => void): this;
154
+ }
155
+
156
+ export declare function resolveVideoId(input: string, options?: any): Promise<{ videoId: string; finalUrl: string }>;
157
+ export declare function extractVideoIdSync(input: string): string | null;
158
+ export declare function parseChatMessage(renderer: any): ChatMessage | null;
159
+ export declare function parseSuperChat(renderer: any): SuperChatGift | null;
160
+ export declare function parseSuperSticker(renderer: any): SuperStickerGift | null;
161
+ export declare function parseMembership(renderer: any): MembershipGift | null;
162
+ export declare function parseGiftMemberships(renderer: any): MembershipGiftPurchase | null;
163
+ export declare function parseInitialStreamInfo(ytInitialData: any, videoId: string): StreamInfo;
164
+ export declare function parseUpdatedMetadataActions(actions: any[]): any;
165
+ export declare function parseViewerCount(str: string): number;
166
+ export declare function parsePrice(text: string): { amount: number; currency: string; raw: string };
167
+
168
+ export default YouTubeLiveConnector;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "youtube-live-connector",
3
+ "version": "1.0.0",
4
+ "description": "Connect to YouTube Live streams via URL or video ID to extract live chat comments, gifts (Super Chats, Super Stickers, Memberships), and real-time live viewer counts. Event-driven API similar to tiktok-live-connector.",
5
+ "main": "src/index.js",
6
+ "module": "src/index.mjs",
7
+ "types": "index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "import": "./src/index.mjs",
12
+ "require": "./src/index.js",
13
+ "default": "./src/index.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "src",
19
+ "index.d.ts",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "scripts": {
24
+ "test": "node --test test/*.test.js",
25
+ "prepublishOnly": "npm test",
26
+ "example:basic": "node examples/basic.js",
27
+ "example:full": "node examples/full-listener.js"
28
+ },
29
+ "keywords": [
30
+ "youtube",
31
+ "youtube-live",
32
+ "youtube-live-chat",
33
+ "live-chat",
34
+ "superchat",
35
+ "supersticker",
36
+ "viewers",
37
+ "connector",
38
+ "tiktok-live-connector",
39
+ "event-emitter"
40
+ ],
41
+ "author": "",
42
+ "license": "MIT",
43
+ "engines": {
44
+ "node": ">=18.0.0"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ }
49
+ }