A robust Node.js application that uses Puppeteer to retrieve YouTube cookies from your Chrome browser, validate login status, and store cookies in a temporary file. Runs on a configurable cron schedule.
- π Retrieves YouTube cookies from your Chrome browser
- β Validates if you're logged in to YouTube
- πͺ Gracefully logs out if not logged in
- β° Runs on a configurable cron schedule
- π Built-in retry mechanism for robustness
- πΎ Stores cookies in a temporary file
- βοΈ Optional AWS S3 upload for cloud storage
- π‘οΈ Error handling and graceful shutdown
- Node.js (v14 or higher)
- Google Chrome installed
- YouTube account (optional, for logged-in state)
- Install dependencies:
npm install-
Build the Docker image:
docker build -t youtube-cookie-retriever . -
Run with Docker:
docker run -d \ --name youtube-cookie-retriever \ --env-file .env \ -e INTERVALS=20 \ youtube-cookie-retriever
-
Or use Docker Compose:
docker-compose up -d
Make sure your
.envfile is configured with all necessary variables.
Note: In Docker, the application uses Chromium instead of Chrome. The Chrome path detection automatically handles this.
PM2 is a process manager that keeps your application running continuously, automatically restarts on crashes, and provides monitoring capabilities.
-
Install dependencies (includes PM2):
npm install
-
Start with PM2:
npm run pm2:start
-
Useful PM2 commands:
# View status npm run pm2:status # View logs npm run pm2:logs # Restart the application npm run pm2:restart # Stop the application npm run pm2:stop # Delete from PM2 npm run pm2:delete # Monitor (real-time monitoring) npm run pm2:monit
-
Save PM2 process list (for auto-start on reboot):
pm2 save pm2 startup
Benefits of PM2:
- β Auto-restart on crashes
- β
Auto-restart on system reboot (with
pm2 startup) - β Log management
- β Process monitoring
- β Memory limit protection (auto-restart if memory exceeds 1GB)
The application tries multiple methods to use your Chrome browser with your existing login:
Priority Order:
- Connect to existing Chrome (via remote debugging) - Uses your actual logged-in session
- Use your Chrome profile - Uses your profile if Chrome is not running
- Temporary profile - Fallback (requires manual login)
This method connects to your already-running Chrome browser with your login:
- Close Chrome completely (if it's running)
- Launch Chrome with remote debugging:
# Easy way - use the helper script: ./launch-chrome.sh # Or manually: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
- Log in to YouTube in that Chrome window (if not already logged in)
- Run the application:
npm start
The app will connect to your Chrome and retrieve cookies from your logged-in YouTube account.
If Chrome is not running, the app will automatically use your Chrome profile (which has your login):
- Make sure Chrome is completely closed
- Run the application:
npm start
The app will launch Chrome with your profile and use your existing YouTube login.
If Chrome is already running and you don't use remote debugging, the app will use a temporary profile. You'll need to log in to YouTube manually in the browser window that opens.
Disable connection to existing Chrome:
CONNECT_TO_EXISTING=false npm startCustom debug port:
CHROME_DEBUG_PORT=9223 npm startThe default cron schedule runs every 6 hours. You can customize it in two ways:
Option 1: Using INTERVALS (Recommended - Easy)
Set the interval in minutes using the INTERVALS environment variable:
# Every 20 minutes
INTERVALS=20 npm start
# Every 30 minutes
INTERVALS=30 npm start
# Every 60 minutes (1 hour)
INTERVALS=60 npm start
# In .env file:
INTERVALS=20Option 2: Using CRON_SCHEDULE (Advanced)
For more complex schedules, set the CRON_SCHEDULE environment variable directly:
# Every 30 minutes (for testing)
CRON_SCHEDULE="*/30 * * * *" npm start
# Every 6 hours (default)
CRON_SCHEDULE="0 */6 * * *" npm start
# Every day at midnight
CRON_SCHEDULE="0 0 * * *" npm startNote: If both INTERVALS and CRON_SCHEDULE are set, INTERVALS takes precedence.
The application automatically detects Chrome based on your OS:
- macOS:
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome - Linux:
/usr/bin/google-chrome - Windows:
C:\Program Files\Google\Chrome\Application\chrome.exe
If Chrome is installed in a different location, modify the getChromePath() function in index.js.
npm startThe application will:
- Run immediately on startup
- Continue running and execute on the configured cron schedule
- Store cookies in:
/tmp/youtube-cookies.json(or your OS temp directory)
Cookies are stored in Netscape HTTP Cookie File format (compatible with curl and other tools).
The file is saved to: /tmp/youtube-cookies.txt (or your OS temp directory)
Format example:
# Netscape HTTP Cookie File
# https://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
.youtube.com TRUE / FALSE 0 wide 1
.youtube.com TRUE / TRUE 1797282419 LOGIN_INFO AFmmF2swRQIhA...
The application can automatically upload cookies to AWS S3 if configured. Set the following environment variables:
export AWS_S3_BUCKET=your-bucket-name
export AWS_S3_KEY=path/to/cookies.txt
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_REGION=us-east-1 # Optional, defaults to us-east-1When these environment variables are set, the application will:
- Save cookies to the local temp file
- Automatically upload to S3 at the specified key
- Replace any existing file at that S3 key
Note: If AWS credentials are not configured, the application will only save locally (no error).
Example format:
YouTube Cookies - Retrieved: 2024-01-01T12:00:00.000Z
================================================================================
Total cookies: 15
Domain: .youtube.com
--------------------------------------------------------------------------------
Cookie 1:
Name: VISITOR_INFO1_LIVE
Value: abc123...
Domain: .youtube.com
Path: /
Secure: Yes
HttpOnly: No
Expires: 2025-01-01T12:00:00.000Z
...
- Launch Chrome: Connects to Chrome using Puppeteer
- Navigate to YouTube: Opens YouTube in a new tab
- Check Login Status: Validates if you're logged in by checking for:
- User avatar/account button (logged in)
- Sign-in button (not logged in)
- Retrieve Cookies: If logged in, extracts all YouTube/Google cookies
- Logout if Needed: If not logged in, attempts to log out gracefully
- Save Cookies: Stores cookies in a temporary file
- Retry Logic: Automatically retries up to 3 times on failure
- Automatic retry mechanism (3 attempts with 5-second delays)
- Graceful error handling and logging
- Continues running even if individual executions fail
- Graceful shutdown on SIGINT/SIGTERM
- Verify Chrome is installed
- Check the Chrome path in
index.jsmatches your installation
- Ensure you're logged in to YouTube in Chrome
- Check browser console for errors
- Verify Chrome profile permissions
- Check the cron schedule format
- Verify the application is still running
- Check logs for errors
The application exports several functions for programmatic use:
const { retrieveCookies, loadCookies, executeWithRetry } = require('./index.js');
// Retrieve cookies manually
const result = await retrieveCookies();
// Load saved cookies
const cookieData = await loadCookies();ISC