Skip to content

Instantly share code, notes, and snippets.

@Lucas-C
Forked from lazypower/parse_ghost_backup.py
Last active March 22, 2019 18:34
Show Gist options
  • Save Lucas-C/7bd26443669bfe369107c03be8b05bb2 to your computer and use it in GitHub Desktop.
Save Lucas-C/7bd26443669bfe369107c03be8b05bb2 to your computer and use it in GitHub Desktop.
A quick and dirty data munger/parser for migrating from ghost to raw markdown - Tested with Ghost v0.11.8
#!/usr/bin/env python
import json
import os
import sys
import datetime
with open("old.json", "r") as f:
raw = f.read()
old = json.loads(raw)
posts = old["db"][0]["data"]["posts"]
postcount = len(posts)
# Data Munging
tags = {}
for item in old["db"][0]["data"]["tags"]:
tags[item["id"]] = item["slug"]
post_tags = {}
for item in old["db"][0]["data"]["posts_tags"]:
post_tags.setdefault(item["post_id"], []).append(tags[item["tag_id"]])
# Kickoff the processing
print("Found %s posts" % postcount)
if not os.path.exists('content'):
print("I don't think we're in the pelican directory. Exiting")
sys.exit(1)
def find_post_tags(postid):
if post_tags.has_key(postid):
return ", ".join(post_tags[postid])
return "untagged"
for post in posts:
date = datetime.datetime.strptime(post["created_at"], '%Y-%m-%d %H:%M:%S')
filename = "{}-{}.md".format(date.strftime("%Y-%m-%d"), post["slug"])
print "Processing {}".format(filename)
with open("content/{}".format(filename), 'w+') as f:
f.write("Title: {}\n".format(post['title']))
f.write("Date: {}\n".format(date.strftime("%Y-%m-%d %H:%m")))
f.write("Tags: {}\n".format(find_post_tags(post["id"])))
f.write("Slug: {}\n".format(post['slug']))
if post['status'] == 'draft':
f.write("Status: draft\n")
f.write("---\n")
f.write("{}".format(post['markdown'].encode('utf8')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment