Files
bitwarden-md-generator/app.py
T

65 lines
2.9 KiB
Python
Raw Normal View History

2025-03-18 10:38:59 +01:00
import json
from datetime import datetime
class BitwardenMDGenerator:
def __init__(self, json_file_path, output_md_path):
# Initialize the class with file paths and prepare the markdown header
self.json_file_path = json_file_path
self.output_md_path = output_md_path
self.data = None
# Start the markdown content with a header including the current date and time
self.markdown = f"# Password export ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})\n\n"
def format_date(self, value: str) -> str:
# Convert a date string from ISO format to a more readable format
date_object = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
return date_object.strftime("%B %d, %Y %H:%M:%S")
def generate(self) -> None:
# Load the JSON data from the file
self.load_data()
# Iterate over each folder in the data
for folder in self.data['folders']:
# Add the folder name as a section header in the markdown
self.markdown += f"## {folder['name']}\n\n"
# Iterate over each item in the data
for item in self.data['items']:
# Check if the item belongs to the current folder
if item['folderId'] == folder['id']:
# Add the item name as a subsection header
self.markdown += f"### {item['name']}\n\n"
# Add creation and modification dates
self.markdown += f"**Created:** {self.format_date(item['creationDate'])}\n"
self.markdown += f"**Modified:** {self.format_date(item['revisionDate'])}\n"
# Add login details if available
if 'login' in item:
self.markdown += f"**Username:** {item['login'].get('username', 'N/A')}\n"
self.markdown += f"**Password:** {item['login'].get('password', 'N/A')}\n\n"
# Add notes if available
if 'notes' in item:
self.markdown += f"**Notes:**\n{item['notes']}\n\n"
# Save the generated markdown content to a file
self.save_md()
def save_md(self) -> None:
# Write the markdown content to the specified output file
with open(self.output_md_path, 'w') as file:
file.write(self.markdown)
def load_data(self) -> None:
# Load JSON data from the specified file path
with open(self.json_file_path, 'r') as file:
self.data = json.load(file)
def main():
# Create an instance of BitwardenMDGenerator and generate the markdown file
BitwardenMDGenerator(json_file_path='/home/markus/Downloads/bitwarden_export.json',
output_md_path='/home/markus/Downloads/passwords.md').generate()
if __name__ == "__main__":
# Run the main function when the script is executed
main()
print("MD created successfully!")