import mimetypes


def extension_for_type(mime_type):
    """
    mimetypes.guess_extension introduces some bizarre randomness.
    This function removes it for the extensions that interest us most.
    """
    ext = None
    if "image/png" == mime_type:
        ext = ".png"
    elif "text/plain" == mime_type:
        ext = ".txt"
    elif "text/html" == mime_type:
        ext = ".html"
    elif "application/json" == mime_type:
        ext = ".json"
    elif mime_type:
        ext = mimetypes.guess_extension(mime_type)
    return ext or ".bin"