import re

content = open("/Users/brians/Documents/AveryArt/Gallery Drill/cards.js").read()
start = content.find("window.CARDS = [")
end = content.find("];", start)
print(f"Start: {start}, End: {end}, Length: {end - start}")
arr = content[start : end + 2]
print(f"First 200 chars of array:\n{arr[:200]}")
print(f"\nLast 100 chars of array:\n{arr[-100:]}")
print(f"\nOpen braces: {arr.count('{')}")
print(f"Close braces: {arr.count('}')}")
print(f"Total chars: {len(arr)}")

# Check if our brace-counter would find anything
i = 0
n = len(arr)
count = 0
while i < n:
    while i < n and arr[i] in " \t\n\r,":
        i += 1
    if i >= n or arr[i] != "{":
        break
    depth = 0
    start_brace = i
    in_string = False
    escape = False
    while i < n:
        c = arr[i]
        if escape:
            escape = False
            i += 1
            continue
        if c == "\\" and in_string:
            escape = True
            i += 1
            continue
        if c == '"':
            in_string = not in_string
        elif not in_string:
            if c == "{":
                depth += 1
            elif c == "}":
                depth -= 1
                if depth == 0:
                    i += 1
                    break
        i += 1
    count += 1
    if count <= 3:
        print(f"Card {count}: braces {start_brace}:{i} = {repr(arr[start_brace:i][:80])}")
print(f"Total cards found by brace counter: {count}")
