videotomp3transcriptor/extract-and-upload-cookies.sh
debian.StillHammer 751a382ccd Add YouTube cookies management system with enhanced error messages
Features:
- New POST /admin/upload-cookies endpoint for cookie upload
- Enhanced YouTube bot detection error messages with solutions
- extract-and-upload-cookies.sh script for automated cookie extraction
- Improved error display in web interface with actionable solutions
- Cookie storage in both local and persistent locations

Technical changes:
- Add enhanceYouTubeError() in youtube.js service
- Add formatYouTubeError() in web app.js
- Add handleYouTubeError() helper in server.js
- Enhanced SSE error handling for all streaming endpoints
- Updated API documentation with cookies section

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-08 10:42:53 +00:00

136 lines
4.7 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Extract and Upload Cookies - All-in-one script
# This script extracts YouTube cookies from your browser and uploads them to the API
set -e # Exit on error
# Colors for pretty output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
API_URL="${API_URL:-http://localhost:8888}"
API_TOKEN="${API_TOKEN:-}"
TEMP_COOKIES="/tmp/youtube-cookies-temp.txt"
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}YouTube Cookies Extractor & Uploader${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Step 1: Check if API token is set
if [ -z "$API_TOKEN" ]; then
echo -e "${YELLOW}⚠️ API_TOKEN not set in environment${NC}"
echo -e "Please enter your API token:"
read -s API_TOKEN
echo ""
fi
# Step 2: Detect available browsers
echo -e "${BLUE}🔍 Detecting browsers...${NC}"
AVAILABLE_BROWSERS=()
if command -v google-chrome &> /dev/null || command -v chromium &> /dev/null || [ -d "$HOME/.config/google-chrome" ] || [ -d "$HOME/.config/chromium" ]; then
AVAILABLE_BROWSERS+=("chrome")
fi
if command -v firefox &> /dev/null || [ -d "$HOME/.mozilla/firefox" ]; then
AVAILABLE_BROWSERS+=("firefox")
fi
if command -v microsoft-edge &> /dev/null || [ -d "$HOME/.config/microsoft-edge" ]; then
AVAILABLE_BROWSERS+=("edge")
fi
if [ ${#AVAILABLE_BROWSERS[@]} -eq 0 ]; then
echo -e "${RED}❌ No supported browsers found${NC}"
echo -e "${YELLOW}Supported browsers: Chrome, Firefox, Edge${NC}"
echo ""
echo -e "${YELLOW}Alternative: Manually export cookies${NC}"
echo -e "1. Install browser extension 'Get cookies.txt LOCALLY'"
echo -e "2. Visit youtube.com and login"
echo -e "3. Export cookies to a file"
echo -e "4. Run: curl -X POST -H \"X-API-Key: \$API_TOKEN\" -F \"cookies=@youtube-cookies.txt\" $API_URL/admin/upload-cookies"
exit 1
fi
echo -e "${GREEN}✓ Found browsers:${NC} ${AVAILABLE_BROWSERS[*]}"
echo ""
# Step 3: Let user choose browser
if [ ${#AVAILABLE_BROWSERS[@]} -eq 1 ]; then
BROWSER="${AVAILABLE_BROWSERS[0]}"
echo -e "${GREEN}Using browser: $BROWSER${NC}"
else
echo -e "${YELLOW}Multiple browsers found. Choose one:${NC}"
select BROWSER in "${AVAILABLE_BROWSERS[@]}"; do
if [ -n "$BROWSER" ]; then
break
fi
done
fi
echo ""
# Step 4: Check if yt-dlp is installed
if ! command -v yt-dlp &> /dev/null; then
echo -e "${RED}❌ yt-dlp is not installed${NC}"
echo -e "${YELLOW}Install with: pip install yt-dlp${NC}"
echo -e "${YELLOW}Or: sudo apt install yt-dlp${NC}"
exit 1
fi
# Step 5: Extract cookies using yt-dlp
echo -e "${BLUE}🍪 Extracting cookies from $BROWSER...${NC}"
if yt-dlp --cookies-from-browser "$BROWSER" --cookies "$TEMP_COOKIES" --no-download "https://www.youtube.com" > /dev/null 2>&1; then
echo -e "${GREEN}✓ Cookies extracted successfully${NC}"
else
echo -e "${RED}❌ Failed to extract cookies${NC}"
echo -e "${YELLOW}Make sure:${NC}"
echo -e " 1. You're logged into YouTube in $BROWSER"
echo -e " 2. $BROWSER is closed (some browsers lock the cookies database)"
echo -e " 3. You have the necessary permissions"
exit 1
fi
# Step 6: Upload cookies to API
echo -e "${BLUE}📤 Uploading cookies to API...${NC}"
UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
-H "X-API-Key: $API_TOKEN" \
-F "cookies=@$TEMP_COOKIES" \
"$API_URL/admin/upload-cookies")
HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | head -n-1)
if [ "$HTTP_CODE" = "200" ]; then
echo -e "${GREEN}✓ Cookies uploaded successfully!${NC}"
echo ""
echo -e "${GREEN}═══════════════════════════════════════${NC}"
echo -e "${GREEN}SUCCESS! You're all set!${NC}"
echo -e "${GREEN}═══════════════════════════════════════${NC}"
echo ""
echo "$RESPONSE_BODY" | python3 -m json.tool 2>/dev/null || echo "$RESPONSE_BODY"
echo ""
echo -e "${BLUE} Your cookies are now active${NC}"
echo -e "${BLUE} YouTube downloads should work without bot detection${NC}"
echo -e "${BLUE} Cookies expire after a few weeks - run this script again if needed${NC}"
else
echo -e "${RED}❌ Failed to upload cookies${NC}"
echo -e "${RED}HTTP Status: $HTTP_CODE${NC}"
echo ""
echo "$RESPONSE_BODY"
exit 1
fi
# Step 7: Cleanup
rm -f "$TEMP_COOKIES"
echo ""
echo -e "${GREEN}🎉 All done!${NC}"