cleanup.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. if not os.path.isfile(root + "/users/" + dirname + "/data.json"):
  14. continue
  15. content = json.load(open(root + "/users/" + dirname + "/content.json"))
  16. contents.append([content["modified"], dirname])
  17. # Delete oldest 20
  18. deleted = 0
  19. for modified, dirname in sorted(contents):
  20. days_old = (time.time() - modified) / (60 * 60 * 24)
  21. if days_old < 30:
  22. continue
  23. size = float(os.path.getsize(root + "/users/" + dirname + "/data.json"))/1024
  24. if size < 1:
  25. continue
  26. print " - Deleting messages from %s (%.0f days old, %.2fkb)" % (dirname, days_old, size)
  27. # Load publickey from data.json
  28. publickey = json.load(open(root + "/users/" + dirname + "/data.json"))["publickey"]
  29. # Insert publickey to content.json
  30. content = json.load(open(root + "/users/" + dirname + "/content.json"))
  31. content["publickey"] = publickey
  32. json.dump(content, open(root + "/users/" + dirname + "/content.json", "w"), indent=2)
  33. # Delete data.json
  34. os.unlink(root + "/users/" + dirname + "/data.json")
  35. cmd = "zeronet.py %s siteSign %s %s --inner_path data/users/%s/content.json --publish" % (" ".join(extra_params), site_address, site_privatekey, dirname)
  36. os.system(cmd)
  37. deleted += 1
  38. if deleted > 20:
  39. break
  40. time.sleep(60)