#!/usr/bin/env python3
"""
Minecraft Token Extractor
Grabs your access token from the Minecraft Launcher on macOS/Windows/Linux.
"""
import os
import json
import sys
import platform
from pathlib import Path

def find_launcher_token():
    system = platform.system()
    paths = []

    if system == "Darwin":  # macOS
        paths = [
            Path.home() / "Library/Application Support/minecraft/com.mojang/authentication/storage.json",
        ]
    elif system == "Windows":
        appdata = os.environ.get("APPDATA", "")
        paths = [
            Path(appdata) / ".minecraft/com.mojang/authentication/store.yml",
            Path(appdata) / ".minecraft/com.mojang/authlib/store.json",
        ]
    else:  # Linux
        paths = [
            Path.home() / ".minecraft/com.mojang/authentication/storage.json",
        ]

    for path in paths:
        if path.exists():
            try:
                content = path.read_text()
                # Try JSON first
                data = json.loads(content)
                if isinstance(data, dict):
                    token = data.get("accessToken") or data.get("access_token")
                    if token:
                        return token, str(path)
                elif isinstance(data, list):
                    for entry in data:
                        token = entry.get("accessToken") or entry.get("access_token")
                        if token:
                            return token, str(path)
            except json.JSONDecodeError:
                # Try YAML-like format (Windows store.yml)
                for line in content.splitlines():
                    if "accessToken:" in line or "access_token:" in line:
                        token = line.split(":", 1)[1].strip().strip("'\"")
                        if token:
                            return token, str(path)

    return None, None

def main():
    print("=" * 60)
    print("  Minecraft Token Extractor")
    print("=" * 60)
    print()

    token, source = find_launcher_token()

    if token:
        print(f"✅ Found token!")
        print(f"   Source: {source}")
        print()
        print("-" * 60)
        print("YOUR ACCESS TOKEN:")
        print("-" * 60)
        print(token)
        print("-" * 60)
        print()
        print("Copy that token and paste it into the sniper GUI's")
        print("'Bearer Token' field in the Accounts tab.")
    else:
        print("❌ No token found in launcher.")
        print()
        print("Try one of these instead:")
        print()
        print("1. Log into https://minecraft.net/profile in your browser")
        print("2. Open DevTools (Cmd+Option+I on Mac)")
        print("3. Go to Network tab, refresh the page")
        print("4. Find a request to api.minecraftservices.com")
        print("5. Copy the Bearer token from the Authorization header")
        print()
        print("Or make sure the Minecraft Launcher has been run at least once.")

if __name__ == "__main__":
    main()
