Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
3
Indexable
Never
import requests
import json
import time

# Replace these placeholders with your actual API key and file paths
API_KEY = "Key"
UUID_INPUT_FILE = r'C:\Users\krs2l\Downloads\uuid_list.txt'
USERNAME_OUTPUT_FILE = r'C:\Users\krs2l\Downloads\username_conversion.txt'

# Function to fetch username from Hypixel API
def fetch_username(uuid):
    url = f"https://api.hypixel.net/player?key={API_KEY}&uuid={uuid}"
    try:
        response = requests.get(url)

        if response.status_code == 200:
            data = response.json()
            if data["success"]:
                username = data["player"]["playername"]
                return username
            else:
                print(f"Error checking {uuid}: {data['cause']}")
        else:
            print(f"Error checking {uuid}: HTTP Status Code {response.status_code}")
    except Exception as e:
        print(f"Error checking {uuid}: {str(e)}")
    return None

# Read UUIDs from input file
with open(UUID_INPUT_FILE, 'r') as uuid_file:
    uuids = uuid_file.read().splitlines()

total_uuids = len(uuids)
uuid_counter = 1

# Rate limiting variables
requests_per_minute = 260
requests_time_interval = 300  # 5 minutes

# Track the last request time
last_request_time = 0

# Initialize a list to store usernames
usernames = []

try:
    for uuid in uuids:
        if uuid_counter > requests_per_minute:
            # If the rate limit is reached, wait before making more requests
            print("Limit reached. Waiting...")
            time.sleep(requests_time_interval)
            uuid_counter = 0  # Reset the counter after waiting

        print(f"[{uuid_counter}/{total_uuids}] Checking {uuid}...", end=" ")

        username = fetch_username(uuid)
        if username:
            print(f"Converted to {username}")
            usernames.append(username)
            # Write the username to the output file immediately
            with open(USERNAME_OUTPUT_FILE, 'a') as username_file:
                username_file.write(username + '\n')
        else:
            print(f"Error checking {uuid}")

        uuid_counter += 1

        # Delay between requests to avoid rate limiting
        time_since_last_request = time.time() - last_request_time
        if time_since_last_request < requests_time_interval:
            time.sleep(requests_time_interval - time_since_last_request)

        last_request_time = time.time()

    print("Usernames saved to", USERNAME_OUTPUT_FILE)

except KeyboardInterrupt:
    print("Program interrupted by user.")

except Exception as e:
    print("An error occurred:", str(e))