A Node.js application to fetch and log all comments from a specific YouTube video using the YouTube Data API v3.
Built in April 2021. This application provides a simple way to extract all comments from any public YouTube video and save them to a TXT file for analysis, archival, or research purposes.
- π₯ Fetches all comments from any YouTube video
- π Handles pagination automatically for videos with thousands of comments
- β±οΈ Rate limiting support to respect API quotas
- π Real-time progress tracking
- πΎ Exports comments to TXT file
- β Validates video existence and API key
- π Secure API key management (external file)
- ποΈ Built-in backup utility for project archival
- π Internet connection validation
- π Supports up to 100,000 comments per session
- Node.js (v14 or higher)
- npm or pnpm
- YouTube Data API v3 key (get it from Google Cloud Console)
- Clone the repository:
git clone https://github.com/orassayag/youtube-comments.git
cd youtube-comments- Install dependencies:
npm install- Create API Key File
Create a JSON file with your YouTube API key. Example format (see misc/examples/apiKey.json):
{
"api_key": "YOUR_YOUTUBE_API_KEY_HERE"
}- Configure Settings
Edit src/settings/settings.js:
VIDEO_ID: The YouTube video ID (from the URL, e.g., 'dQw4w9WgXcQ')API_KEY_PATH: Path to your API key JSON fileDIST_FILE_NAME: Output filename (default: 'comments')MAXIMUM_COMMENTS_COUNT: Max comments to fetch (default: 100,000)MILLISECONDS_FETCH_DELAY_COUNT: Delay between API calls (default: 1000ms)MAXIMUM_FETCH_COMMENTS_COUNT: Comments per API request (max: 100)
Fetches all comments from the configured YouTube video:
npm startCreates a timestamped backup of the project:
npm run backup your-backup-titleRuns sandbox test file:
npm run sandForcefully stops all Node.js processes (Windows only):
npm run stopyoutube-comments/
βββ src/
β βββ core/
β β βββ enums/ # Enumerations (status, placeholders, etc.)
β β βββ models/ # Data models
β βββ logics/ # Business logic
β βββ scripts/ # Entry point scripts
β βββ services/ # Service layer
β βββ settings/ # Configuration
β βββ tests/ # Test files
β βββ utils/ # Utility functions
βββ dist/ # Output directory for comments
βββ backups/ # Project backups
βββ misc/ # Miscellaneous files
β βββ examples/ # Example files
βββ package.json
graph TD
A[comments.script.js] --> B[CommentsLogic]
B --> C[Validation Service]
B --> D[YouTube Service]
B --> E[Log Service]
B --> F[Path Service]
C --> G[Internet Connection Check]
D --> H[Load API Key]
D --> I[Validate Video ID]
D --> J[Fetch Comments with Pagination]
J --> K[YouTube Data API v3]
E --> L[Write to TXT File]
M[backup.script.js] --> N[BackupLogic]
N --> O[File Utils]
N --> P[Create Versioned Backup]
style A fill:#e1f5ff
style M fill:#e1f5ff
style K fill:#ffcccc
style L fill:#ccffcc
- Initialization: Validates settings and internet connection
- API Key Loading: Reads YouTube API key from external JSON file
- Video Validation: Checks if the video exists and is accessible
- Comment Count: Retrieves expected total comment count
- Pagination Loop: Fetches comments in batches (max 100 per request)
- Progress Tracking: Displays real-time progress in console
- File Writing: Saves comments to TXT file in
dist/directory - Rate Limiting: Applies configurable delays between API calls
YouTube Data API v3 has a daily quota of 200,000 read operations:
- Each video details request: 1 unit
- Each comment threads request: 1 unit
To stay within limits:
- Adjust
MILLISECONDS_FETCH_DELAY_COUNTto add delays - Use
MAXIMUM_COMMENTS_COUNTto limit total comments - Monitor quota in Google Cloud Console
All errors include unique codes (1000001-1000027) for easy troubleshooting:
- 1000001-1000002: Backup errors
- 1000013-1000027: YouTube API and comment fetching errors
See error messages for specific codes and descriptions.
Successful execution will look like:
===IMPORTANT SETTINGS===
VIDEO_ID: dQw4w9WgXcQ
API_BASE_URL: https://www.googleapis.com/youtube/v3/
DIST_FILE_NAME: comments
MAXIMUM_COMMENTS_COUNT: 100000
MILLISECONDS_FETCH_DELAY_COUNT: 1000
MAXIMUM_FETCH_COMMENTS_COUNT: 100
========================
OK to run? (y = yes)
y
===VALIDATE GENERAL SETTINGS===
===INITIATE THE SERVICES===
===FETCH COMMENTS===
===Writing comments: 5,432/8,123 | 66.87%===
===EXIT: FINISH===
The output file (dist/comments.txt) will contain all comments, one per line.
- Node.js - JavaScript runtime
- axios - HTTP client for API requests
- fs-extra - Enhanced file system operations
- is-reachable - Internet connection validation
- YouTube Data API v3 - Comment data source
The project uses:
- ES Modules (type: "module")
- ESLint for code quality
- Modular architecture with services, utilities, and models
Contributions to this project are released to the public under the project's open source license.
Everyone is welcome to contribute. Contributing doesn't just mean submitting pull requestsβthere are many different ways to get involved, including answering questions and reporting issues.
Please feel free to contact me with any question, comment, pull-request, issue, or any other thing you have in mind.
- Or Assayag - Initial work - orassayag
- Or Assayag orassayag@gmail.com
- GitHub: https://github.com/orassayag
- StackOverflow: https://stackoverflow.com/users/4442606/or-assayag?tab=profile
- LinkedIn: https://linkedin.com/in/orassayag
This application has an MIT license - see the LICENSE file for details.