Advertisement
tyler569

PlotData_2to2+UUID.py

Aug 12th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import sys
  4. import uuid
  5. import urllib
  6. import json
  7. import time
  8.  
  9. import PersistentData
  10.  
  11.  
  12. def name_to_uuid(name, page="https://api.mojang.com/users/profiles/minecraft/"):
  13.         while True:
  14.                 path = page + name
  15.                 url = urllib.urlopen(path)
  16.                 NS = uuid.UUID('80c336a2-2226-11e4-89e8-0401211ac001')
  17.                 text = url.read()
  18.                 try:
  19.                         jdata = json.loads(text)
  20.                 except ValueError:
  21.                         ret = uuid.uuid5(NS, name)
  22.                         return ret
  23.                 try:
  24.                         ret = str(uuid.UUID(jdata["id"]))
  25.                         return ret
  26.                 except KeyError:
  27.                         if "TooManyRequestsException" in text:
  28.                                 print("TooManyRequestsException, waiting 30 seconds", file=sys.stderr)
  29.                                 error = 1
  30.                                 time.sleep(30)
  31.                         else:
  32.                                 ret = uuid.uuid5(NS, name)
  33.                                 return ret
  34.  
  35. if __name__ == "__main__":
  36.  
  37.         if len(sys.argv) > 1:
  38.                 path = sys.argv[1]
  39.         else:
  40.                 path = "PlotData.json"
  41.  
  42.  
  43.         n = PersistentData.Node()
  44.         be = PersistentData.JSONBackend()
  45.         be.Load(n, open(path, "r").read())
  46.         d = n.Dict()
  47.  
  48.         new = {}
  49.  
  50.         new["ORE"] = {}
  51.         new["ORE"]["Players"] = {}
  52.         new["ORE"]["Plots"] = {}
  53.         new["ORE"]["Size"] = d["ORE"]["Size"]
  54.  
  55.         new["ORE"]["Players"] = {name_to_uuid(name):{"Name":name, "remPlots":val["remPlots"]}
  56.                 for (name,val) in d["ORE"]["Players"].iteritems()}
  57.  
  58.         for k, v in d["ORE"]["Plots"].iteritems():
  59.                 new["ORE"]["Plots"][k] = v
  60.                 if v["status"] == 0:
  61.                         continue
  62.                 new["ORE"]["Plots"][k]["ownerid"] = name_to_uuid(v["owner"])
  63.                 del new["ORE"]["Plots"][k]["owner"]
  64.  
  65.         newn = PersistentData.Node()
  66.         newbe = PersistentData.JSONBackend()
  67.         newbe.LoadDict(new)
  68.         text = newbe.Save(newn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement