Merge pull request #15 from me5ng3/master
UUID-fix: Empty usernames from Mojang API (status code 204: no content) no longer passed to metrics.
This commit is contained in:
commit
46661024f3
1 changed files with 15 additions and 7 deletions
|
@ -9,7 +9,6 @@ import schedule
|
|||
from mcrcon import MCRcon
|
||||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
|
||||
class MinecraftCollector(object):
|
||||
def __init__(self):
|
||||
self.statsdirectory = "/world/stats"
|
||||
|
@ -36,9 +35,12 @@ class MinecraftCollector(object):
|
|||
if uuid in self.map:
|
||||
return self.map[uuid]
|
||||
else:
|
||||
try:
|
||||
result = requests.get('https://api.mojang.com/user/profiles/' + uuid + '/names')
|
||||
self.map[uuid] = result.json()[-1]['name']
|
||||
return(result.json()[-1]['name'])
|
||||
except:
|
||||
return
|
||||
|
||||
def rcon_command(self,command):
|
||||
if self.rcon == None:
|
||||
|
@ -149,8 +151,11 @@ class MinecraftCollector(object):
|
|||
return data
|
||||
|
||||
def update_metrics_for_player(self,uuid):
|
||||
data = self.get_player_stats(uuid)
|
||||
name = self.uuid_to_player(uuid)
|
||||
if not name: return
|
||||
|
||||
data = self.get_player_stats(uuid)
|
||||
|
||||
blocks_mined = Metric('blocks_mined','Blocks a Player mined',"counter")
|
||||
blocks_picked_up = Metric('blocks_picked_up','Blocks a Player picked up',"counter")
|
||||
player_deaths = Metric('player_deaths','How often a Player died',"counter")
|
||||
|
@ -288,11 +293,14 @@ class MinecraftCollector(object):
|
|||
|
||||
def collect(self):
|
||||
for player in self.get_players():
|
||||
for metric in self.update_metrics_for_player(player):
|
||||
yield metric
|
||||
for metric in self.get_server_stats():
|
||||
metrics = self.update_metrics_for_player(player)
|
||||
if not metrics: continue
|
||||
|
||||
for metric in metrics:
|
||||
yield metric
|
||||
|
||||
for metric in self.get_server_stats():
|
||||
yield metric
|
||||
|
||||
if __name__ == '__main__':
|
||||
if all(x in os.environ for x in ['RCON_HOST','RCON_PASSWORD']):
|
||||
|
|
Loading…
Reference in a new issue