- Go 86.4%
- Nix 13.6%
| .github/workflows | ||
| docs | ||
| scraper | ||
| .envrc | ||
| .gitignore | ||
| flake.lock | ||
| flake.nix | ||
| go.mod | ||
| LICENSE | ||
| main.go | ||
| README.md | ||
YouTube Channel ID Scraper
A lightweight Go tool and library to extract the YouTube Channel ID (e.g., UC...) from various YouTube URLs, including videos, handles, and short links.
Getting Started with Nix
This project provides a flake.nix for a reproducible development environment.
Prerequisites
- Nix with flakes enabled.
Entering the Environment
To enter the development shell with Go pre-installed and configured:
nix develop
This will drop you into a shell with the correct Go version available.
Usage
CLI Tool
You can run the tool directly to scrape an ID from a URL.
Build:
go build -o yt-scraper
Run:
./yt-scraper <URL>
# OR
./yt-scraper -url <URL>
Quick Run (without building):
go run main.go <URL>
Examples:
$ ./yt-scraper https://www.youtube.com/watch?v=dQw4w9WgXcQ
UCuAXFkgsw1L7xaCfnd5JJOw
$ ./yt-scraper https://www.youtube.com/@YouTube
UCBR8-60-B28hp2BmDPdntcQ
Output Format:
- Success: The tool prints the Channel ID (string starting with
UC) tostdoutand exits with code0. - Error: The tool prints the error message to
stdout(orstderrdepending on implementation, currentlystdoutfor simplicity in the main wrapper) and exits with code1.
Library Integration
You can import the scraper package to use the logic in your own Go projects.
Installation:
go get github.com/cedev-1/yt-id-scraper
Code Example:
package main
import (
"fmt"
"log"
"github.com/cedev-1/yt-id-scraper/scraper"
)
func main() {
url := "https://www.youtube.com/@YouTube"
channelID, err := scraper.GetChannelID(url)
if err != nil {
log.Fatalf("Failed to get channel ID: %v", err)
}
fmt.Printf("Channel ID: %s\n", channelID)
}
How it Works
The scraper fetches the public HTML of the provided YouTube URL and uses several strategies to locate the Channel ID:
- URL Parsing: Checks if the URL is already a canonical channel URL (
/channel/UC...). - HTML Scraping:
- Looks for the
<meta itemprop="channelId">tag. - Searches for the
ytInitialDataJSON blob which often contains"channelId":"...". - Checks for the canonical
<link>tag if the page redirects.
- Looks for the
This approach works for:
- Video URLs (
/watch?v=...) - Handle URLs (
/@Handle) - Custom URLs (
/c/CustomName) - Short URLs (
youtu.be/...)
License
LICENSE Apache License 2.0