from typing import Optional
from typing import Dict
import json
import zlib
import base64


def decode(bp: str) -> Optional[Dict]:
    """Parses factorio blueprint string into a python dictionary

    See https://wiki.factorio.com/Blueprint_string_format

    Args:
        bp (str): The blueprint string

    Returns:
        Optional[Dict]: Returns None if exception
    """
    try:
        if bp[0] != "0":
            return None
        return json.loads(zlib.decompress(base64.b64decode(bp[1:])))
    except:
        return None