# YouTube Cookies Setup Guide

YouTube has implemented aggressive bot detection that blocks most automated downloads.  
To use the YouTube Intelligence Pipeline, you need to provide browser cookies.

## Quick Setup (5 minutes)

### Step 1: Install Cookie Export Extension

Choose your browser:

**Chrome/Brave/Edge:**
1. Install [Get cookies.txt LOCALLY](https://chrome.google.com/webstore/detail/get-cookiestxt-locally/cclelndahbckbenkjhflpdbgdldlbecc)
2. Visit https://www.youtube.com (make sure you're logged in)
3. Click the extension icon
4. Click "Export" to download `cookies.txt`

**Firefox:**
1. Install [cookies.txt](https://addons.mozilla.org/en-US/firefox/addon/cookies-txt/)
2. Visit https://www.youtube.com (make sure you're logged in)
3. Click the extension icon
4. Save the cookies.txt file

### Step 2: Save the Cookies File

```bash
# Move the downloaded cookies.txt to your home directory
mv ~/Downloads/cookies.txt ~/.youtube-cookies.txt

# Make it readable only by you (security)
chmod 600 ~/.youtube-cookies.txt
```

### Step 3: Test the Pipeline

```bash
cd /home/ccuser/rateright-growth/rivet

# Test with a short, popular video
python3 scripts/youtube-pipeline.py \
  "https://www.youtube.com/watch?v=dQw4w9WgXcQ" \
  --cookies ~/.youtube-cookies.txt
```

## Making It Permanent

Add to your bash/zsh profile so cookies are always used:

```bash
echo 'export YOUTUBE_COOKIES="$HOME/.youtube-cookies.txt"' >> ~/.bashrc
source ~/.bashrc
```

Then update the bash script to use the environment variable:

```bash
# In youtube-pipeline.sh, add after line 50:
if [ -n "$YOUTUBE_COOKIES" ] && [ -f "$YOUTUBE_COOKIES" ]; then
    COOKIE_ARG="--cookies $YOUTUBE_COOKIES"
else
    COOKIE_ARG=""
fi

# Then pass it to the Python script
python3 "$PYTHON_SCRIPT" "$URL" $COOKIE_ARG
```

## Alternative: Use Browser's Cookie Database Directly

This is simpler but requires the browser to be installed on the server:

```bash
# For Chrome (if installed)
python3 scripts/youtube-pipeline.py URL --cookies-from-browser chrome

# For Firefox (if installed)
python3 scripts/youtube-pipeline.py URL --cookies-from-browser firefox
```

**Note:** This requires updating the Python script to pass `--cookies-from-browser` to yt-dlp.

## Security Notes

⚠️ **Important:** Your cookies.txt file contains session tokens that give access to your YouTube account.

- **Never commit it to git** (it's in .gitignore)
- **Don't share it** with anyone
- **Use restricted permissions:** `chmod 600 ~/.youtube-cookies.txt`
- **Regenerate cookies** if they leak (just export again)
- **They expire** after ~6 months, so you'll need to re-export occasionally

## Troubleshooting

### Cookies not working

1. Make sure you're logged into YouTube when exporting
2. Export again (cookies may have expired)
3. Check file permissions: `ls -la ~/.youtube-cookies.txt`
4. Verify file format (should start with `# Netscape HTTP Cookie File`)

### Still getting "bot" errors

1. Try a different browser's cookies
2. Clear YouTube cache and re-login before exporting
3. Use a Google account that's older and has normal YouTube activity
4. Wait a few hours and try again (rate limiting)

### "Could not find chrome cookies database"

The browser isn't installed on this server. Use the manual cookie export method instead.

## Current Status

✅ **Scripts created and tested**
- `youtube-pipeline.py` - Main Python pipeline (supports --cookies option)
- `youtube-pipeline.sh` - Bash wrapper script
- Both support local audio file processing for testing

⚠️ **Blocked by YouTube bot detection**
- YouTube requires cookies for all downloads
- This is a YouTube restriction, not a bug in our code
- All major YouTube download tools have this issue

🎯 **Next Steps**
1. Export cookies from your browser (5 minutes)
2. Test with a short video
3. Integrate into Rivet workflows

## Testing Without YouTube

You can test the transcription pipeline with local audio files:

```bash
# Create or download a test audio file
# Then run:
python3 scripts/youtube-pipeline.py /path/to/audio.mp3 --title "Test Audio"
```

This will transcribe and summarize the audio without needing YouTube access.

## References

- [yt-dlp cookies documentation](https://github.com/yt-dlp/yt-dlp/wiki/FAQ#how-do-i-pass-cookies-to-yt-dlp)
- [Exporting YouTube cookies](https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies)
- [yt-dlp authentication guide](https://github.com/yt-dlp/yt-dlp#authentication)
