2018-09-10 14:24:15 +00:00
|
|
|
from simplejson.errors import JSONDecodeError
|
2018-08-12 19:00:04 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2018-08-21 20:36:51 +00:00
|
|
|
from geoffrey.BotErrors import UsernameLookupFailed
|
2018-06-03 02:42:31 +00:00
|
|
|
|
|
|
|
uuid_lookup_url = 'https://api.mojang.com/users/profiles/minecraft/{}'
|
|
|
|
username_lookup_url = 'https://api.mojang.com/user/profiles/{}/names'
|
|
|
|
|
|
|
|
|
|
|
|
def grab_json(url):
|
|
|
|
try:
|
2018-08-05 22:51:55 +00:00
|
|
|
json = requests.get(url).json()
|
|
|
|
if 'error' in json:
|
|
|
|
raise UsernameLookupFailed
|
|
|
|
|
2018-06-03 02:42:31 +00:00
|
|
|
except JSONDecodeError:
|
|
|
|
raise UsernameLookupFailed
|
|
|
|
|
2018-08-05 22:51:55 +00:00
|
|
|
return json
|
|
|
|
|
2018-08-11 23:02:50 +00:00
|
|
|
|
2018-08-05 22:51:55 +00:00
|
|
|
def grab_UUID(username):
|
|
|
|
player_data = grab_json(uuid_lookup_url.format(username))
|
|
|
|
return player_data['id']
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-08-11 23:02:50 +00:00
|
|
|
|
2018-06-03 02:42:31 +00:00
|
|
|
def grab_playername(uuid):
|
|
|
|
player_data = grab_json(username_lookup_url.format(uuid))
|
2018-07-23 02:51:22 +00:00
|
|
|
|
2018-08-05 22:51:55 +00:00
|
|
|
if len(player_data) == 0:
|
|
|
|
raise UsernameLookupFailed
|
|
|
|
else:
|
2018-08-11 23:02:50 +00:00
|
|
|
last_index = len(player_data) - 1
|
2018-08-05 22:51:55 +00:00
|
|
|
|
2018-07-23 02:51:22 +00:00
|
|
|
return player_data[last_index]['name']
|