118 lines
4 KiB
Python
118 lines
4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Quick test script to debug a single cookie file
|
|
Usage: python3 test_single_cookie.py <cookie_file>
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from cookie_parser import read_cookies_file, clean_cookie_content, netscape_to_dict, json_to_dict
|
|
import json
|
|
import debug_logger as log
|
|
|
|
def test_cookie_file(file_path):
|
|
log.info(f"Testing cookie file: {file_path}")
|
|
log.info("=" * 80)
|
|
|
|
path = Path(file_path)
|
|
if not path.exists():
|
|
log.error(f"File not found: {file_path}")
|
|
return False
|
|
|
|
# Read raw content
|
|
print("\n1. Reading file...")
|
|
try:
|
|
raw_content = path.read_text(encoding='utf-8')
|
|
log.info(f"✓ File read successfully, size: {len(raw_content)} bytes")
|
|
print(f"First 500 characters:")
|
|
print("-" * 80)
|
|
print(raw_content[:500])
|
|
print("-" * 80)
|
|
except Exception as e:
|
|
log.error(f"✗ Failed to read file: {e}")
|
|
return False
|
|
|
|
# Clean content
|
|
print("\n2. Cleaning content...")
|
|
try:
|
|
cleaned = clean_cookie_content(raw_content)
|
|
log.info(f"✓ Content cleaned, size: {len(cleaned)} bytes")
|
|
print(f"Cleaned content (first 500 chars):")
|
|
print("-" * 80)
|
|
print(cleaned[:500])
|
|
print("-" * 80)
|
|
except Exception as e:
|
|
log.error(f"✗ Failed to clean content: {e}")
|
|
return False
|
|
|
|
# Try to parse
|
|
print("\n3. Attempting to parse...")
|
|
cookies_dict = None
|
|
|
|
# Try JSON first
|
|
try:
|
|
cookies_dict = json_to_dict(cleaned)
|
|
log.info(f"✓ Parsed as JSON format")
|
|
print(f"Cookies found: {len(cookies_dict)}")
|
|
print(f"Cookie names: {list(cookies_dict.keys())}")
|
|
except json.JSONDecodeError as e:
|
|
log.debug(f"Not JSON: {e}")
|
|
|
|
# Try Netscape
|
|
try:
|
|
cookies_dict = netscape_to_dict(cleaned)
|
|
log.info(f"✓ Parsed as Netscape format")
|
|
print(f"Cookies found: {len(cookies_dict)}")
|
|
print(f"Cookie names: {list(cookies_dict.keys())}")
|
|
except Exception as e:
|
|
log.error(f"✗ Failed to parse as Netscape: {e}")
|
|
return False
|
|
|
|
# Check for required cookies
|
|
print("\n4. Checking for required cookies...")
|
|
required_cookies = ['auth_token', 'ct0']
|
|
for cookie_name in required_cookies:
|
|
if cookie_name in cookies_dict:
|
|
value = cookies_dict[cookie_name]
|
|
print(f"✓ {cookie_name}: {value[:30]}..." if len(value) > 30 else f"✓ {cookie_name}: {value}")
|
|
else:
|
|
print(f"✗ {cookie_name}: NOT FOUND")
|
|
|
|
# Test full read_cookies_file function
|
|
print("\n5. Testing read_cookies_file function...")
|
|
try:
|
|
cookie_string = read_cookies_file(str(file_path))
|
|
if cookie_string:
|
|
log.info(f"✓ read_cookies_file successful")
|
|
print(f"Cookie string length: {len(cookie_string)}")
|
|
print(f"Cookie string preview (first 200 chars):")
|
|
print(cookie_string[:200])
|
|
else:
|
|
log.error(f"✗ read_cookies_file returned None")
|
|
return False
|
|
except Exception as e:
|
|
log.error(f"✗ read_cookies_file failed: {e}")
|
|
log.exception("Exception:")
|
|
return False
|
|
|
|
log.info("=" * 80)
|
|
log.info("✓ All tests passed!")
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
# Test first file in cookies directory
|
|
cookies_dir = Path("cookies")
|
|
if cookies_dir.exists():
|
|
cookie_files = list(cookies_dir.glob("*.txt")) + list(cookies_dir.glob("*.json"))
|
|
if cookie_files:
|
|
test_file = cookie_files[0]
|
|
print(f"No file specified, testing first file: {test_file}")
|
|
test_cookie_file(test_file)
|
|
else:
|
|
print("No cookie files found in cookies/ directory")
|
|
else:
|
|
print("Usage: python3 test_single_cookie.py <cookie_file>")
|
|
print("Or place cookie files in cookies/ directory")
|
|
else:
|
|
test_cookie_file(sys.argv[1])
|