archive.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import json
  2. import os
  3. import time
  4. import sys
  5. root = os.path.dirname(__file__)
  6. site_address = sys.argv[1]
  7. site_privatekey = sys.argv[2]
  8. extra_params = sys.argv[3:]
  9. contents = []
  10. for dirname in os.listdir(root + "/users"):
  11. if not os.path.isdir(root + "/users/" + dirname):
  12. continue
  13. try:
  14. content = json.load(open(root + "/users/" + dirname + "/content.json"))
  15. contents.append([content["modified"], dirname])
  16. except Exception, err:
  17. raw_input("Error load: %s" % err)
  18. # Delete oldest 20
  19. deleted = 0
  20. for modified, dirname in sorted(contents):
  21. days_old = (time.time() - modified) / (60 * 60 * 24)
  22. if days_old < 30:
  23. continue
  24. if not os.path.isfile(root + "/users/" + dirname + "/data.json"):
  25. size = 0
  26. else:
  27. size = float(os.path.getsize(root + "/users/" + dirname + "/data.json")) / 1024
  28. print " - Deleting messages from %s (%.0f days old, %.2fkb)" % (dirname, days_old, size)
  29. cert_user_id = json.load(open(root + "/users/" + dirname + "/content.json"))["cert_user_id"]
  30. # Load publickey from data.json
  31. try:
  32. publickey = json.load(open(root + "/users/" + dirname + "/data.json"))["publickey"]
  33. except Exception, err:
  34. publickey = json.load(open(root + "/users/" + dirname + "/content.json"))["publickey"]
  35. # Insert publickey to archived.json
  36. archived = json.load(open(root + "/archived.json"))
  37. archived[dirname] = {"publickey": publickey, "cert_user_id": cert_user_id}
  38. json.dump(archived, open(root + "/archived.json", "w"), indent=2)
  39. # Insert dirname to archived dirnames
  40. users_content = json.load(open(root + "/users/content.json"))
  41. users_content["user_contents"]["archived"][dirname] = int(time.time())
  42. json.dump(users_content, open(root + "/users/content.json", "w"), indent=2)
  43. # Delete user files
  44. try:
  45. os.unlink(root + "/users/" + dirname + "/content.json")
  46. except Exception, err:
  47. print "Error deleting content.json", err
  48. try:
  49. os.unlink(root + "/users/" + dirname + "/data.json")
  50. except Exception, err:
  51. print "Error deleting data.json", err
  52. try:
  53. os.rmdir(root + "/users/" + dirname)
  54. except Exception, err:
  55. print "Error deleting dir", err
  56. deleted += 1
  57. if deleted > 100:
  58. break
  59. cmd = "zeronet.py %s siteSign %s %s --inner_path data/users/%s/content.json --publish" % (" ".join(extra_params), site_address, site_privatekey, dirname)
  60. #os.system(cmd)
  61. print cmd