Skip to content

Instantly share code, notes, and snippets.

@JayCuthrell
Last active September 4, 2023 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayCuthrell/fbd78d3eae36bc4e25769371c9fbfa5b to your computer and use it in GitHub Desktop.
Save JayCuthrell/fbd78d3eae36bc4e25769371c9fbfa5b to your computer and use it in GitHub Desktop.
Buttondown offers export of all your emails as a CSV file 'emails.csv' and a folder 'emails' with all your emails. Since you can import to Buttondown, some of those emails might be HTML imports from other newsletter services.
import csv
import os
from markdownify import markdownify
# Define the variables for the columns of the CSV file
id = "id"
secondary_id = "secondary_id"
subject = "subject"
publish_date = "publish_date"
source = "source"
email_type = "email_type"
slug = "slug"
def read_csv_file(csv_file_path):
"""Reads a CSV file and returns a list of rows."""
with open(csv_file_path, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_reader) # Skip the header row
rows = []
for row in csv_reader:
rows.append(row)
return rows
def create_markdown_file(markdown_file_path, subject, publish_date):
"""Creates a markdown file with frontmatter entries for 'title: $subject' and 'date: $publish_date'."""
frontmatter = f"""---
title: "{subject}"
description: 'originally written on {publish_date} on LAMP with vi, WordPress, Jekyll, Gatsby Cloud, Netlify, Revue, Substack, or Buttondown'
date: {publish_date}
---
"""
with open(markdown_file_path, 'w') as markdown_file:
markdown_file.write(frontmatter)
def read_exported_markdown_contents(markdown_file_path):
"""Reads the contents of a markdown file."""
with open(markdown_file_path, 'r') as markdown_file:
markdown_content = markdown_file.read()
return markdown_content
def convert_html_to_markdown(html_content):
"""Converts HTML content to Markdown."""
markdown_content = markdownify(html_content)
return markdown_content
def insert_exported_markdown_contents(markdown_file_path, markdown_content):
"""Inserts the contents of a markdown file after the frontmatter section."""
with open(markdown_file_path, 'r') as markdown_file:
frontmatter = markdown_file.readlines()[:11]
with open(markdown_file_path, 'w') as markdown_file:
markdown_file.writelines(frontmatter)
markdown_file.write(markdown_content)
def main():
"""Reads the CSV file and creates markdown files with frontmatter and converted HTML contents."""
csv_file_path = 'emails.csv'
import_ready_dir = 'import_ready'
exported_dir = 'emails'
# Read the CSV file.
csv_rows = read_csv_file(csv_file_path)
# Iterate over the CSV rows and create markdown files with frontmatter.
for row in csv_rows:
slug = row[6]
markdown_file_path = f'{import_ready_dir}/{slug}.md'
# Get the subject and publish date from the CSV row.
subject = row[2]
publish_date = row[3]
# Create the markdown file.
create_markdown_file(markdown_file_path, subject, publish_date)
# Read the contents of the exported markdown file.
exported_html_content = read_exported_markdown_contents(f'{exported_dir}/{slug}.md')
# Convert the HTML content to Markdown.
markdown_content = convert_html_to_markdown(exported_html_content)
# Insert the converted HTML contents after the frontmatter section.
insert_exported_markdown_contents(markdown_file_path, markdown_content)
if __name__ == '__main__':
main()
@JayCuthrell
Copy link
Author

This python script will read a CSV file named "emails.csv" with columns id,secondary_id,subject,publish_date,source,email_type,slug skipping the first line, create variables for the CSV file columns as 'subject', 'publish_date', and 'slug', then insert frontmatter entries for 'title: $subject', 'date: $publish_date' to create markdown files '$slug'.md in a directory named "import_ready", then read the contents of the directory "emails" for '$slug'.md files convert the contents from html to markdown using markdownify and insert the markdown contents after the frontmatter section for the markdown files '$slug'.md in the directory named "import_ready"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment