import json

# Function to format notes for printable output
def format_notes(notes, width=50):
    formatted_notes = ""
    for i in range(0, len(notes), width):
        formatted_notes += notes[i:i+width] + "\n"
    return formatted_notes

# Read the Bitwarden JSON export and create a printable TXT file
def process_bitwarden_export(input_file, output_file):
    try:
        # Load JSON data
        with open(input_file, 'r', encoding='utf-8') as file:
            data = json.load(file)

        # Open output file
        with open(output_file, 'w', encoding='utf-8') as output:
            # Iterate through items in the export
            for item in data.get('items', []):
                username = item.get('login', {}).get('username', 'N/A')
                password = item.get('login', {}).get('password', 'N/A')
                uri = item.get('login', {}).get('uris', [{}])[0].get('uri', 'N/A') if item.get('login', {}).get('uris') else 'N/A'
                notes = item.get('notes', None)

                # Write entry to the output file
                output.write(f"Username: {username}\n")
                output.write(f"Password: {password}\n")
                output.write(f"URI: {uri}\n")
                if notes:
                    output.write("Notes:\n")
                    output.write(format_notes(notes))
                output.write("-" * 50 + "\n")

        print(f"Successfully created printable TXT file: {output_file}")

    except Exception as e:
        print(f"An error occurred: {e}")


input_file = ""  # Replace with your input JSON file
output_file = "bitwarden_printable.txt"  # Replace with your desired output TXT file
process_bitwarden_export(input_file, output_file)