This commit is contained in:
friedemann.blume 2024-07-19 12:44:15 +02:00
commit a0706c95c3
6 changed files with 61 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
mbox-files/*

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
# Use the official Python image from the Docker Hub
FROM python:3.11-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Create and set the working directory
WORKDIR /app
# Copy the Python script and any other necessary files into the container
COPY mbox_to_markdown.py /app/
COPY requirements.txt /app/
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Define the command to run the Python script
CMD ["python", "mbox_to_markdown.py"]

7
Readme.md Normal file
View File

@ -0,0 +1,7 @@
## Mbox to Markdown converter
docker build -t mbox-to-markdown .
docker run --rm -v ./mbox-files:/mnt/input -v /path/to/output/directory:/mnt/output mbox-to-markdown python mbox_to_markdown.py /mnt/input/yourfile.mbox /mnt/output/

31
mbox_to_markdown.py Normal file
View File

@ -0,0 +1,31 @@
import mailbox
import os
from markdownify import markdownify
# Configuration
mbox_file = 'path/to/your/file.mbox'
output_dir = 'path/to/output/directory'
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
def save_email_as_markdown(email, index):
subject = email.get('subject', 'No Subject')
date = email.get('date', 'No Date')
body = email.get_payload(decode=True).decode(errors='ignore')
body_markdown = markdownify(body)
# Create a Markdown file for each email
filename = os.path.join(output_dir, f'email_{index}.md')
with open(filename, 'w', encoding='utf-8') as file:
file.write(f'# {subject}\n')
file.write(f'*Date: {date}*\n\n')
file.write(body_markdown)
def convert_mbox_to_markdown(mbox_file):
mbox = mailbox.mbox(mbox_file)
for i, email in enumerate(mbox):
save_email_as_markdown(email, i)
convert_mbox_to_markdown(mbox_file)

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
mailbox
markdownify