Skip to content

Commit

Permalink
Merge pull request #13 from TheHiddenGamer23/master
Browse files Browse the repository at this point in the history
getUUID - properly handle non-existent users
  • Loading branch information
destruc7i0n committed Aug 24, 2018
2 parents ed305d7 + 835a01a commit e8c296c
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pypixel.py
Expand Up @@ -16,9 +16,11 @@
if (sys.version_info >= (3, 0)):
from urllib.request import Request, urlopen
curl = lambda req: urlopen(req).read().decode("utf-8")
py2 = False
else:
from urllib2 import Request, urlopen
curl = lambda req: urlopen(req).read()
py2 = True

# If you want to use a custom printer function, you can overwrite pypixel.printer.
printer = print
Expand Down Expand Up @@ -106,7 +108,7 @@ def expandUrlData(data):
string += "&".join(dataStrings)
return string

def urlopen(url, params={}):
def urlread(url, params={}):
"""
string, dict -> data from the url
"""
Expand All @@ -119,15 +121,13 @@ def getUUID(username, url="https://api.mojang.com/users/profiles/minecraft/%s",
string, string -> get UUID from username via different API
string, string, string -> return another dictionary element from result
"""
return json.loads(urlopen(url % username, {"at":str(int(time.time()))})).get(returnthis)

def hasPaid(username, url="https://mcapi.ca/other/haspaid/%s", returnthis="premium"):
"""
string -> if USERNAME has a premium account
string, string -> get has paid via different API
string, string, string -> return another dictionary element from result
"""
return json.loads(urlopen(url % username)).get(returnthis)
resp = urlopen(url % username)
if resp.getcode() == 204:
raise LookupError("user: %s" % username)
resp = resp.read() if py2 else resp.read().decode('utf-8')
if '"demo":' not in resp:
return json.loads(resp).get(returnthis)
else: raise LookupError("user: %s" % username)

class HypixelAPI:
"""
Expand Down Expand Up @@ -233,7 +233,7 @@ def main(self, action, args={}):
"""
url = self.base + action
params = dict(args, **self.baseParams)
return json.loads(urlopen(url, params))
return json.loads(urlread(url, params))


class MultiKeyAPI(HypixelAPI):
Expand Down

0 comments on commit e8c296c

Please sign in to comment.