Skip to content

About

Project designed to get cookies from browser and set it in s3 for other automation tools to utilize.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

2 Commits

Folders and files

Repository files navigation

YouTube Cookie Retriever

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.

Features

  • πŸ” 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

Prerequisites

  • Node.js (v14 or higher)
  • Google Chrome installed
  • YouTube account (optional, for logged-in state)

Installation

Option 1: Local Installation

  1. Install dependencies:
npm install

Option 2: Docker Installation

  1. Build the Docker image:

    docker build -t youtube-cookie-retriever .
  2. Run with Docker:

    docker run -d \
      --name youtube-cookie-retriever \
      --env-file .env \
      -e INTERVALS=20 \
      youtube-cookie-retriever
  3. Or use Docker Compose:

    docker-compose up -d

    Make sure your .env file is configured with all necessary variables.

Note: In Docker, the application uses Chromium instead of Chrome. The Chrome path detection automatically handles this.

Option 3: Running with PM2 (Recommended for Production)

PM2 is a process manager that keeps your application running continuously, automatically restarts on crashes, and provides monitoring capabilities.

  1. Install dependencies (includes PM2):

    npm install
  2. Start with PM2:

    npm run pm2:start
  3. 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
  4. 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)

Configuration

Using Your Chrome Browser with Login

The application tries multiple methods to use your Chrome browser with your existing login:

Priority Order:

  1. Connect to existing Chrome (via remote debugging) - Uses your actual logged-in session
  2. Use your Chrome profile - Uses your profile if Chrome is not running
  3. Temporary profile - Fallback (requires manual login)

Option 1: Use Existing Chrome Session (Recommended)

This method connects to your already-running Chrome browser with your login:

  1. Close Chrome completely (if it's running)
  2. 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
  3. Log in to YouTube in that Chrome window (if not already logged in)
  4. Run the application:
    npm start

The app will connect to your Chrome and retrieve cookies from your logged-in YouTube account.

Option 2: Use Your Chrome Profile (If Chrome is Closed)

If Chrome is not running, the app will automatically use your Chrome profile (which has your login):

  1. Make sure Chrome is completely closed
  2. Run the application:
    npm start

The app will launch Chrome with your profile and use your existing YouTube login.

Option 3: Temporary Profile (Fallback)

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 start

Custom debug port:

CHROME_DEBUG_PORT=9223 npm start

Cron Schedule

The 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=20

Option 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 start

Note: If both INTERVALS and CRON_SCHEDULE are set, INTERVALS takes precedence.

Chrome Path

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.

Usage

Start the application:

npm start

The application will:

  1. Run immediately on startup
  2. Continue running and execute on the configured cron schedule
  3. Store cookies in: /tmp/youtube-cookies.json (or your OS temp directory)

Cookie File Format

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...

AWS S3 Upload (Optional)

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-1

When these environment variables are set, the application will:

  1. Save cookies to the local temp file
  2. Automatically upload to S3 at the specified key
  3. 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
  ...

How It Works

  1. Launch Chrome: Connects to Chrome using Puppeteer
  2. Navigate to YouTube: Opens YouTube in a new tab
  3. Check Login Status: Validates if you're logged in by checking for:
    • User avatar/account button (logged in)
    • Sign-in button (not logged in)
  4. Retrieve Cookies: If logged in, extracts all YouTube/Google cookies
  5. Logout if Needed: If not logged in, attempts to log out gracefully
  6. Save Cookies: Stores cookies in a temporary file
  7. Retry Logic: Automatically retries up to 3 times on failure

Error Handling

  • 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

Troubleshooting

Chrome not found

  • Verify Chrome is installed
  • Check the Chrome path in index.js matches your installation

Cookies not retrieved

  • Ensure you're logged in to YouTube in Chrome
  • Check browser console for errors
  • Verify Chrome profile permissions

Cron not running

  • Check the cron schedule format
  • Verify the application is still running
  • Check logs for errors

Development

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();

License

ISC

About

Project designed to get cookies from browser and set it in s3 for other automation tools to utilize.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages