A Python content management system designed for kirsle.net featuring a blog, comments and photo albums. https://rophako.kirsle.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

siikir-blog-migrate.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. """Migrate blog database files from a PerlSiikir site to Rophako.
  4. Usage: scripts/siikir-blog-migrate.py <path/to/siikir/db/root>
  5. Rophako supports one global blog, so the blog of UserID 1 in Siikir is used."""
  6. import sys
  7. import os
  8. import codecs
  9. import json
  10. import glob
  11. sys.path.append(".")
  12. import rophako.jsondb as JsonDB
  13. # Path to Siikir DB root.
  14. siikir = None
  15. def main():
  16. if len(sys.argv) == 1:
  17. print "Usage: {} <path/to/siikir/db/root>".format(__file__)
  18. sys.exit(1)
  19. global siikir
  20. siikir = sys.argv[1]
  21. print "Siikir DB:", siikir
  22. if raw_input("Confirm? [yN] ") != "y":
  23. sys.exit(1)
  24. convert_index()
  25. convert_posts()
  26. convert_comments()
  27. convert_subscriptions()
  28. def convert_index():
  29. print "Converting blog index"
  30. index = json_get("blog/index/1.json")
  31. new = {}
  32. for post_id, data in index.iteritems():
  33. del data["id"]
  34. # Enforce data types.
  35. data["author"] = int(data["author"])
  36. data["time"] = int(data["time"])
  37. data["sticky"] = bool(data["sticky"])
  38. new[post_id] = data
  39. JsonDB.commit("blog/index", new)
  40. def convert_posts():
  41. print "Converting blog entries..."
  42. for name in glob.glob(os.path.join(siikir, "blog/entries/1/*.json")):
  43. name = name.split("/")[-1]
  44. post = json_get("blog/entries/1/{}".format(name))
  45. post_id = post["id"]
  46. del post["id"]
  47. # Enforce data types.
  48. post["time"] = int(post["time"])
  49. post["author"] = int(post["author"])
  50. post["comments"] = bool(post["comments"])
  51. post["sticky"] = bool(post["sticky"])
  52. post["emoticons"] = bool(post["emoticons"])
  53. print "*", post["subject"]
  54. JsonDB.commit("blog/entries/{}".format(post_id), post)
  55. def convert_comments():
  56. print "Converting comments..."
  57. for name in glob.glob(os.path.join(siikir, "comments/1/*.json")):
  58. name = name.split("/")[-1]
  59. if name.startswith("photos-"): continue
  60. data = json_get("comments/1/{}".format(name))
  61. thread = name[:len(name)-5]
  62. # Enforce data types.
  63. for cid in data:
  64. data[cid]["time"] = int(data[cid]["time"])
  65. data[cid]["uid"] = int(data[cid]["uid"])
  66. print "*", thread
  67. JsonDB.commit("comments/threads/{}".format(thread), data)
  68. def convert_subscriptions():
  69. print "Converting subscriptions..."
  70. for name in glob.glob(os.path.join(siikir, "subscribers/1/*.json")):
  71. name = name.split("/")[-1]
  72. if name.startswith("photos-"): continue
  73. data = json_get("subscribers/1/{}".format(name))
  74. thread = name[:len(name)-5]
  75. # Enforce data types.
  76. for email in data:
  77. data[email] = int(data[email])
  78. print "*", thread
  79. JsonDB.commit("comments/subscribers/{}".format(thread), data)
  80. def json_get(document):
  81. fh = codecs.open(os.path.join(siikir, document), 'r', 'utf-8')
  82. text = fh.read()
  83. fh.close()
  84. return json.loads(text)
  85. if __name__ == "__main__":
  86. main()