Skip to content

Commit 26e9802

Browse files
committed
support live links and usernames [skip ci]
1 parent 3249ef1 commit 26e9802

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

scripts/extract-youtube-channel-id.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* Examples:
1414
* node scripts/extract-youtube-channel-id.js "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
1515
* node scripts/extract-youtube-channel-id.js "https://youtu.be/dQw4w9WgXcQ"
16+
* node scripts/extract-youtube-channel-id.js "https://www.youtube.com/live/6V5DhkGkuvc"
17+
* node scripts/extract-youtube-channel-id.js "https://www.youtube.com/@gianpaj"
1618
* node scripts/extract-youtube-channel-id.js "lexfridman"
1719
* node scripts/extract-youtube-channel-id.js "@lexfridman"
1820
* node scripts/extract-youtube-channel-id.js "UC2D6eRvCeMtcF5OGHf1-trw"
@@ -72,7 +74,7 @@ function getDescription(description) {
7274
*/
7375
function extractVideoId(url) {
7476
const patterns = [
75-
/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/)([^&\n?#]+)/,
77+
/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/|youtube\.com\/live\/)([^&\n?#]+)/,
7678
/youtube\.com\/watch\?.*v=([^&\n?#]+)/,
7779
];
7880

@@ -86,6 +88,21 @@ function extractVideoId(url) {
8688
return null;
8789
}
8890

91+
/**
92+
* Extract username from YouTube channel URL (e.g., https://www.youtube.com/@username)
93+
*/
94+
function extractUsernameFromChannelUrl(url) {
95+
const match = url.match(/youtube\.com\/@([a-zA-Z0-9_-]+)/);
96+
return match ? match[1] : null;
97+
}
98+
99+
/**
100+
* Check if input is a YouTube channel URL with @username format
101+
*/
102+
function isChannelUrl(input) {
103+
return /youtube\.com\/@/.test(input);
104+
}
105+
89106
/**
90107
* Check if input is already a YouTube channel ID (24 characters, alphanumeric)
91108
*/
@@ -222,6 +239,8 @@ async function main() {
222239
log('Where <input> can be:', 'blue');
223240
log(' - YouTube video URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'blue');
224241
log(' - YouTube short URL: https://youtu.be/dQw4w9WgXcQ', 'blue');
242+
log(' - YouTube live URL: https://www.youtube.com/live/dQw4w9WgXcQ', 'blue');
243+
log(' - Channel URL: https://www.youtube.com/@gianpaj', 'blue');
225244
log(' - Channel username: lexfridman or @lexfridman', 'blue');
226245
log(' - Channel ID: UC2D6eRvCeMtcF5OGHf1-trw', 'blue');
227246
log('', 'reset');
@@ -247,6 +266,41 @@ async function main() {
247266
log(` Channel Title: ${channelInfo.channelTitle}`, 'reset');
248267
log(` Description: ${getDescription(channelInfo.description)}`, 'reset');
249268
logChannelId(channelInfo.channelId);
269+
} else if (isChannelUrl(input)) {
270+
// Extract username from channel URL
271+
log(`Extracting username from channel URL: ${input}`, 'blue');
272+
273+
const username = extractUsernameFromChannelUrl(input);
274+
if (!username) {
275+
throw new Error('Could not extract username from channel URL');
276+
}
277+
278+
log(`Extracted username: ${username}`, 'blue');
279+
280+
const results = await searchChannelByName(username);
281+
282+
if (results.length === 1) {
283+
const channel = results[0];
284+
log('\n✅ Channel found:', 'green');
285+
log(` Channel ID: ${channel.channelId}`, 'reset');
286+
log(` Channel Title: ${channel.channelTitle}`, 'reset');
287+
log(` Description: ${getDescription(channel.description)}`, 'reset');
288+
logChannelId(channel.channelId);
289+
} else {
290+
log(`\n✅ Found ${results.length} channels:`, 'green');
291+
292+
results.forEach((channel, index) => {
293+
log(`\n${index + 1}. ${channel.channelTitle}`, 'blue');
294+
log(` Channel ID: ${channel.channelId}`, 'reset');
295+
log(` Description: ${getDescription(channel.description)}`, 'reset');
296+
});
297+
298+
log('\nTip: Use the exact channel ID for precise results', 'yellow');
299+
300+
if (!verbose && results.length > 0) {
301+
logChannelId(results[0].channelId);
302+
}
303+
}
250304
} else if (isVideoUrl(input)) {
251305
// Extract channel ID from video URL
252306
log(`Extracting channel ID from video URL: ${input}`, 'blue');

0 commit comments

Comments
 (0)