Skip to content

Instantly share code, notes, and snippets.

@Lucas-C
Last active August 18, 2017 22:57
Show Gist options
  • Save Lucas-C/d3ff24ca636e09241eb5eea18e5a4c72 to your computer and use it in GitHub Desktop.
Save Lucas-C/d3ff24ca636e09241eb5eea18e5a4c72 to your computer and use it in GitHub Desktop.
Generate a .redirects file to include in nginx, from a Ghost blog JSON export
#!/usr/bin/env python
# USAGE: ./nginx_redirect_generator.py /lucas/blog < ghost_export.json
import datetime, json, os, sys
path_prefix = sys.argv[1]
ghost_db = json.loads(sys.stdin.read())
posts = ghost_db['db'][0]['data']['posts']
for post in posts:
slug = post['slug']
date = datetime.datetime.strptime(post['created_at'], '%Y-%m-%d %H:%M:%S')
old_path = '{}/{}'.format(date.strftime('%Y/%m/%d'), slug)
print('rewrite ^{path_prefix}/{old_path}/?$ {path_prefix}/{slug} permanent;'.format(**locals()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment