Last active
January 11, 2018 17:52
-
-
Save DylanModesitt/ab2dccbb8cdb15e26536630378b42cd9 to your computer and use it in GitHub Desktop.
Zinnia blog (djang) to Ghost blog (node) Article Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import datetime, time | |
import re | |
# date lambdas | |
epoch = datetime.datetime.utcfromtimestamp(0) | |
unix_time_millis = lambda dt: int((dt - epoch).total_seconds() * 1000.0) | |
zulu_time = lambda s: unix_time_millis(datetime.datetime(*map(int, re.split('[^\d]', s)[:-1]))) | |
# zinnia data exported from the save feature | |
zinnia_data = json.load(open("./blogs.json")) # the location of the zinnia json data | |
# once can export this data by | |
ghost_data = {} | |
# initialize standard ghost headers | |
ghost_data["meta"] = { | |
"exported_on":unix_time_millis(datetime.datetime.now()), # export datetime | |
"version":"003" # current ghost version. This may change! | |
} | |
ghost_data["data"] = { | |
"posts":[], | |
"tags":[], | |
"posts_tags": [], | |
"users":[], | |
"role_users":[] | |
} | |
# populate ghost entries | |
print(len(zinnia_data)) | |
for i in range(0,len(zinnia_data)): | |
entry = zinnia_data[i] | |
if entry["model"] == "zinnia.entry": | |
fields = entry["fields"] | |
author_id = 1 # this will just use the default (owner) author on ghost. | |
ghost_data["data"]["posts"].append({ | |
"id":int(entry["pk"]), | |
"title":fields["title"], | |
"slug":fields["slug"], | |
"markdown":fields["content"], | |
"html":fields["content"], | |
"image":"/content/images/" + "/".join(fields["image"].split("/")[2:]), | |
"featured":0, | |
"page":0, | |
"status":"published", | |
"language":"en_US", | |
"meta_title":None, | |
"meta_description":None, | |
"author_id":author_id, | |
"created_at":zulu_time(fields["creation_date"]), | |
"created_by":author_id, | |
"updated_at":zulu_time(fields["last_update"]), | |
"updated_by":author_id, | |
"published_at":zulu_time(fields["publication_date"]), | |
"published_by":author_id, | |
}) | |
for tag in fields["tags"].split(","): | |
if len(tag) > 0: | |
tag_id = len(ghost_data["data"]["tags"]) | |
ghost_data["data"]["tags"].append({ | |
"id": tag_id, | |
"name": tag, | |
"slug": tag.lower(), | |
"description":"" | |
}) | |
ghost_data["data"]["posts_tags"].append({ | |
"tag_id":tag_id, | |
"post_id":entry["pk"] | |
}) | |
with open("ghost.json","w") as f: | |
f.write(json.dumps(ghost_data,indent=4)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment