Files
playlist_dl/export_youtube_cookies.py

105 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""
Export YouTube cookies from browser to cookies.txt
Uses browser-cookie3 library for direct browser access.
"""
import sys
from pathlib import Path
def export_cookies_with_browser_cookie3():
"""Export cookies using browser-cookie3."""
try:
import browser_cookie3
except ImportError:
print("❌ browser-cookie3 not installed")
print("Install it with: pip install browser-cookie3")
return False
print("🍪 Exporting YouTube cookies from browser...")
try:
# Try to get cookies from Firefox first
try:
cj = browser_cookie3.firefox()
print("✅ Using Firefox cookies")
except Exception:
try:
cj = browser_cookie3.chrome()
print("✅ Using Chrome cookies")
except Exception:
try:
cj = browser_cookie3.chromium()
print("✅ Using Chromium cookies")
except Exception:
cj = browser_cookie3.load()
print("✅ Using default browser cookies")
# Save cookies to file in Netscape format
cookies_file = Path("cookies.txt")
with open(cookies_file, 'w') as f:
f.write("# Netscape HTTP Cookie File\n")
f.write("# This is a generated file! Do not edit.\n\n")
for cookie in cj:
if 'youtube' in cookie.domain.lower():
# Format: domain flag path secure expiration name value
domain = cookie.domain
flag = "TRUE" if cookie.domain.startswith('.') else "FALSE"
path = cookie.path or "/"
secure = "TRUE" if cookie.secure else "FALSE"
expires = str(int(cookie.expires)) if cookie.expires else "0"
name = cookie.name
value = cookie.value
f.write(f"{domain}\t{flag}\t{path}\t{secure}\t{expires}\t{name}\t{value}\n")
print(f"✅ Cookies exported to: {cookies_file.absolute()}")
print(f"📊 Total YouTube cookies: {len([c for c in cj if 'youtube' in c.domain.lower()])}")
return True
except Exception as e:
print(f"❌ Error: {e}")
return False
def main():
"""Main entry point."""
print("YouTube Cookie Exporter")
print("=" * 50)
print()
print("⚠️ Make sure you're logged in to YouTube in your browser!")
print()
# Try browser-cookie3 first
if export_cookies_with_browser_cookie3():
print()
print("✅ Cookies are ready! The app will use them automatically.")
print()
print("The cookies.txt file will be used for all future downloads.")
return 0
print()
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print("Alternative method using yt-dlp:")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print()
print("1. Make sure you're logged in to YouTube in your browser")
print("2. Run one of these commands:")
print()
print(" For Firefox:")
print(" yt-dlp --cookies-from-browser firefox -o /dev/null https://www.youtube.com")
print()
print(" For Chrome:")
print(" yt-dlp --cookies-from-browser chrome -o /dev/null https://www.youtube.com")
print()
print(" For Edge:")
print(" yt-dlp --cookies-from-browser edge -o /dev/null https://www.youtube.com")
print()
print("This will create a cookies.txt file that the app will use automatically.")
print()
return 1
if __name__ == "__main__":
sys.exit(main())