import "@johnlindquist/kit"
const YOUTUBE_API_KEY = await env("YOUTUBE_API_KEY", {
hint: "Get your API key from https://console.developers.google.com/",
secret: true
})
const channelInput = await arg({
placeholder: "Enter YouTube channel URL or @handle",
hint: "e.g., https://youtube.com/@channelname or @channelname"
})
let channelId = ""
let channelHandle = ""
if (channelInput.includes("youtube.com/")) {
const urlMatch = channelInput.match(/youtube\.com\/(@[^\/]+|channel\/([^\/]+)|c\/([^\/]+)|user\/([^\/]+))/)
if (urlMatch) {
if (urlMatch[1]?.startsWith("@")) {
channelHandle = urlMatch[1]
} else {
channelId = urlMatch[2] || urlMatch[3] || urlMatch[4]
}
}
} else if (channelInput.startsWith("@")) {
channelHandle = channelInput
} else {
channelId = channelInput
}
if (channelHandle) {
try {
const searchResponse = await get(`https://www.googleapis.com/youtube/v3/search`, {
params: {
part: "snippet",
q: channelHandle,
type: "channel",
key: YOUTUBE_API_KEY,
maxResults: 1
}
})
if (searchResponse.data.items?.length > 0) {
channelId = searchResponse.data.items[0].snippet.channelId
} else {
await div(md(`# Channel not found\nCouldn't find channel: ${channelHandle}`))
exit()
}
} catch (error) {
await div(md(`# Error\nFailed to search for channel: ${error.message}`))
exit()
}
}
if (!channelId) {
await div(md(`# Invalid Input\nPlease provide a valid YouTube channel URL or handle`))
exit()
}
try {
setStatus({ message: "Fetching channel videos...", status: "busy" })
const channelResponse = await get(`https://www.googleapis.com/youtube/v3/channels`, {
params: {
part: "snippet,statistics",
id: channelId,
key: YOUTUBE_API_KEY
}
})
if (!channelResponse.data.items?.length) {
await div(md(`# Channel Not Found\nNo channel found with ID: ${channelId}`))
exit()
}
const channel = channelResponse.data.items[0]
const channelName = channel.snippet.title
const videosResponse = await get(`https://www.googleapis.com/youtube/v3/search`, {
params: {
part: "snippet",
channelId: channelId,
type: "video",
order: "date",
maxResults: 50,
key: YOUTUBE_API_KEY
}
})
const videos = videosResponse.data.items
if (!videos?.length) {
await div(md(`# No Videos Found\nNo videos found for channel: ${channelName}`))
exit()
}
const randomVideo = videos[Math.floor(Math.random() * videos.length)]
const videoUrl = `https://www.youtube.com/watch?v=${randomVideo.id.videoId}`
const result = await div({
html: md(`
# Random Video from ${channelName}
## ${randomVideo.snippet.title}
**Published:** ${formatDate(new Date(randomVideo.snippet.publishedAt), 'PPP')}
**Description:**
${randomVideo.snippet.description.substring(0, 200)}${randomVideo.snippet.description.length > 200 ? '...' : ''}
---
[Watch on YouTube](${videoUrl})
`),
shortcuts: [
{
name: "Open Video",
key: `${cmd}+o`,
onPress: () => {
open(videoUrl)
},
bar: "right"
},
{
name: "Copy URL",
key: `${cmd}+c`,
onPress: () => {
copy(videoUrl)
toast("Video URL copied to clipboard!")
},
bar: "right"
},
{
name: "Another Random",
key: `${cmd}+r`,
onPress: () => {
submit("random")
},
bar: "right"
}
]
})
if (result === "random") {
await run(process.argv[1].replace(/\.[^/.]+$/, ""))
}
} catch (error) {
await div(md(`# Error\nFailed to fetch videos: ${error.message}\n\nMake sure your YouTube API key is valid and has the YouTube Data API v3 enabled.`))
}