Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON format with "mcstatus [address] json" #74

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions mcstatus/scripts/mcstatus.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click
from json import dumps as json_dumps

from .. import MinecraftServer

Expand Down Expand Up @@ -70,6 +71,39 @@ def status():
)


@cli.command(short_help="all available server information in json")
def json():
"""
Prints server status and query in json. Supported by all Minecraft
servers that are version 1.7 or higher.
"""
data = {'online': False}
# Build data with responses and quit on exception
try:
ping_res = server.ping()
data['online'] = True
data['ping'] = ping_res

status_res = server.status(retries=1)
data['version'] = status_res.version.name
data['protocol'] = status_res.version.protocol
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kevinkjt2000 Authored by kevinkjt2000
Nov 15, 2019


Outdated (history rewrite) - original diff


@@ -70,6 +71,43 @@ def status():
     )
 
 
+@cli.command(short_help="all available server information in json")
+def json():
+    """
+    Prints server status and query in json. Supported by all Minecraft
+    servers that are version 1.7 or higher.
+    """
+    data = {'online': False}
+    # Build data with responses and quit on exception
+    try:
+        ping_res = server.ping()
+        data['online'] = True
+        data['ping'] = ping_res
+        data = {
+            'online': True,
+            'ping': ping_res,
+        }

I notice that 'online' and 'ping' are repeated above. Please remove lines 86-89, since lines 84-85 already do the same.

data['motd'] = status_res.description
data['player_count'] = status_res.players.online
data['player_max'] = status_res.players.max
data['players'] = []
if status_res.players.sample is not None:
data['players'] = [{'name': player.name, 'id': player.id} for player in status_res.players.sample]

query_res = server.query(retries=1)
data['host_ip'] = query_res.raw['hostip']
data['host_port'] = query_res.raw['hostport']
data['map'] = query_res.map
data['plugins'] = query_res.software.plugins
except:
pass
click.echo(json_dumps(data))


@cli.command(short_help="detailed server information")
def query():
"""
Expand Down