Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import json
  4. import os
  5. import sys
  6. import datetime
  7.  
  8. with open("old.json", "r") as f:
  9. raw = f.read()
  10.  
  11. old = json.loads(raw)
  12.  
  13. posts = old["db"][0]["data"]["posts"]
  14. postcount = len(posts)
  15.  
  16. # Data Munging
  17. tags = {}
  18. for item in old["db"][0]["data"]["tags"]:
  19. tags[item["id"]] = item["slug"]
  20. post_tags = {}
  21. for item in old["db"][0]["data"]["posts_tags"]:
  22. post_tags.setdefault(item["post_id"], []).append(tags[item["tag_id"]])
  23.  
  24. # Kickoff the processing
  25. print("Found %s posts" % postcount)
  26. if not os.path.exists('content'):
  27. print("I don't think we're in the pelican directory. Exiting")
  28. sys.exit(1)
  29.  
  30.  
  31. def find_post_tags(postid):
  32. if post_tags.has_key(postid):
  33. return ", ".join(post_tags[postid])
  34. return "untagged"
  35.  
  36. for post in posts:
  37. date = datetime.datetime.fromtimestamp(post["created_at"] / 1e3)
  38. filename = "{}-{}.md".format(date.strftime("%Y-%m-%d"), post["slug"])
  39. print "Processing {}".format(filename)
  40. with open("content/{}".format(filename), 'w+') as f:
  41. f.write("Title: {}\n".format(post['title']))
  42. f.write("Date: {}\n".format(date.strftime("%Y-%m-%d %H:%m")))
  43. f.write("Tags: {}\n".format(find_post_tags(post["id"])))
  44. f.write("Slug: {}\n".format(post['slug']))
  45. f.write("---\n")
  46. f.write("{}".format(post['markdown'].encode('utf8')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement