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:
Joshi 2021-03-27 17:35:29 +01:00 committed by GitHub
commit 46661024f3
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,6 @@ import schedule
from mcrcon import MCRcon from mcrcon import MCRcon
from os import listdir from os import listdir
from os.path import isfile, join from os.path import isfile, join
class MinecraftCollector(object): class MinecraftCollector(object):
def __init__(self): def __init__(self):
self.statsdirectory = "/world/stats" self.statsdirectory = "/world/stats"
@ -36,9 +35,12 @@ class MinecraftCollector(object):
if uuid in self.map: if uuid in self.map:
return self.map[uuid] return self.map[uuid]
else: else:
try:
result = requests.get('https://api.mojang.com/user/profiles/' + uuid + '/names') result = requests.get('https://api.mojang.com/user/profiles/' + uuid + '/names')
self.map[uuid] = result.json()[-1]['name'] self.map[uuid] = result.json()[-1]['name']
return(result.json()[-1]['name']) return(result.json()[-1]['name'])
except:
return
def rcon_command(self,command): def rcon_command(self,command):
if self.rcon == None: if self.rcon == None:
@ -149,8 +151,11 @@ class MinecraftCollector(object):
return data return data
def update_metrics_for_player(self,uuid): def update_metrics_for_player(self,uuid):
data = self.get_player_stats(uuid)
name = self.uuid_to_player(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_mined = Metric('blocks_mined','Blocks a Player mined',"counter")
blocks_picked_up = Metric('blocks_picked_up','Blocks a Player picked up',"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") player_deaths = Metric('player_deaths','How often a Player died',"counter")
@ -288,11 +293,14 @@ class MinecraftCollector(object):
def collect(self): def collect(self):
for player in self.get_players(): for player in self.get_players():
for metric in self.update_metrics_for_player(player): metrics = self.update_metrics_for_player(player)
yield metric if not metrics: continue
for metric in self.get_server_stats():
for metric in metrics:
yield metric yield metric
for metric in self.get_server_stats():
yield metric
if __name__ == '__main__': if __name__ == '__main__':
if all(x in os.environ for x in ['RCON_HOST','RCON_PASSWORD']): if all(x in os.environ for x in ['RCON_HOST','RCON_PASSWORD']):