Import Ghost blog posts to Pelican with Python

quick script to import Markdown files from Ghost to Pelican format

Export from Ghost as JSON first, then:

import json
from markdownify import markdownify

count = 0 # to keep track

data = []
with open('/path/to/Ghost/backup/file/2022-02-28_MasterBackup.json') as f:
    data = json.load(f)

    for x in data['db'][0]['data']['posts']:
        count += 1

        published = x['published_at']

        if published not in [None, 'None']:

            published = published[:10] # string slicing to get date in YYYY-MM-DD format

            slug = x['slug']
            title = x['title']
            summary = x['custom_excerpt']

            feature_image = x['feature_image']
            image = feature_image[37:] # to get the relative path, removing the 'GHOST' prefix
            image_html = f'<img src="images/{image}" alt="{image}" width="200"/>'

            html = x['html']

            md = markdownify(html)

            output = image_html + '\n\n' + md

            category_name = 'XXX' # fill out Category to be assigned in Pelican

            tags = 'XXX, XXX' # fill out Tags to be assigned in Pelican (can be automated further if tag assignment logic required)

            author = '' # author name to be assigned in Pelican (can be automated further if author assignment logic required)

            output = f"Title: {title}\nDate: {published}\nModified: {published}\nCategory: {category_name}\nTags: {tags}\nSlug: {slug}\nAuthor: {author}\nSummary: {summary}\n{output}"

            print(count, slug)
            print(f"Title: {title}")
            print(f"Published: {published}")
            print(f"Summary: {summary}")
            print(f"Image: {image_html}")
            print('Markdown:\n', output)

            with open(f"/path/to/folder/in/Pelican/project/named/content/{slug}.md", 'w') as f:
                f.write(output)

        print()

links

social