Add Minecraft players to /stats/ API

This commit is contained in:
Tanner Collin 2020-04-21 00:12:28 +00:00
parent 459bff6756
commit 118c32ca7c
3 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand, CommandError
from django.utils.timezone import now
from apiserver.api import models, utils, utils_stats
import time
class Command(BaseCommand):
help = 'Tasks to run on the portal minutely.'
def handle(self, *args, **options):
self.stdout.write('{} - Beginning minutely tasks'.format(str(now())))
start = time.time()
players = utils_stats.check_minecraft_server()
self.stdout.write('Found Minecraft players: ' + str(players))
self.stdout.write('Completed tasks in {} s'.format(
str(time.time() - start)[:4]
))

View File

@ -1,8 +1,13 @@
import logging
logger = logging.getLogger(__name__)
import time
import datetime
import requests
from django.core.cache import cache
from django.utils.timezone import now
from apiserver.api import models
from apiserver import secrets
DEFAULTS = {
'last_card_change': time.time(),
@ -13,6 +18,7 @@ DEFAULTS = {
'green_count': None,
'bay_108_temp': None,
'bay_110_temp': None,
'minecraft_players': [],
}
def changed_card():
@ -49,3 +55,19 @@ def calc_member_counts():
cache.set('member_count', num_not_paused)
cache.set('paused_count', num_paused)
cache.set('green_count', num_current + num_prepaid)
def check_minecraft_server():
if secrets.MINECRAFT:
url = 'https://api.minetools.eu/ping/' + secrets.MINECRAFT
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
players = [x['name'] for x in r.json()['players']['sample']]
cache.set('minecraft_players', players)
return players
except BaseException as e:
cache.set('minecraft_players', [])
logger.error('Problem checking Minecraft: {} - {}'.format(e.__class__.__name__, str(e)))
return []

View File

@ -36,6 +36,7 @@ DOOR_API_TOKEN = ''
# Protospace general info
DOOR_CODE = ''
WIFI_PASS = ''
MINECRAFT = ''
# Backup API tokens
# These tokens allow each user to download a backup of member data.