Skip to content

Instantly share code, notes, and snippets.

@megamattron megamattron/realms.py
Last active Jan 5, 2017

Embed
What would you like to do?
LED Minecraft server status for Raspberry Pi and Sense HAT
import requests
from mcstatus import MinecraftServer
from sense_hat import SenseHat
mojang_username = "<your mojang login, usually your email address>"
minecraft_playername = "<your minecraft playername>"
password = "<your mojang password>"
def get_cookie(user_uuid):
return "sid=token:" + get_token() + ":"+user_uuid+";user="+minecraft_playername+";version=1.7.9"
def get_uuid():
url = "https://api.mojang.com/users/profiles/minecraft/" + minecraft_playername
response = requests.request("GET", url, headers=None)
if response.status_code == requests.codes.ok:
return response.json()['id']
else:
print "Error getting username UUID: "+str(response.status_code)
return None
def get_token():
url = "https://authserver.mojang.com/authenticate"
payload = "{\n \"agent\": { \n \"name\": \"Minecraft\", \n \"version\": 1 \n \n },\n \"username\": \""+mojang_username+"\",\n \"password\": \""+password+"\",\n \"clientToken\": \"\", \n \"requestUser\": true \n}"
print payload
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "3cef436e-dfc5-97c8-737f-dda5a0873eea"
}
response = requests.request("POST", url, data=payload, headers=headers)
if response.status_code == requests.codes.ok:
return response.json()['accessToken']
else:
print "Error getting access token: "+str(response.status_code)
return None
def get_address( cookie, id ):
url = "https://mcoapi.minecraft.net/worlds/"+str(id)+"/join"
headers = {
'cookie': cookie,
'cache-control': "no-cache",
'postman-token': "183b30f0-3844-8b4d-ba8a-2bc4387389ef"
}
response = requests.request("GET", url, headers=headers)
if response.status_code == requests.codes.ok:
return response.json()['address']
else:
return None
def draw_status( cookie, row, id ):
address = get_address(cookie, id)
if address is not None:
server = MinecraftServer.lookup(address)
# 'status' is supported by all Minecraft servers that are version 1.7 or higher.
status = server.status()
online = status.players.online
print("The server has {0} players and replied in {1} ms".format(online, status.latency))
sense.set_pixel(0, row, 0, 255, 0)
for x in range(1, 1+online):
sense.set_pixel(x, row, 0, 0, 255)
for x in range(1+online, 8):
sense.set_pixel(x, row, 0, 0, 0)
else:
print("Error contacting server for row ", row, " at address ", address)
sense.set_pixel(0, row, 255, 0, 0)
for x in range(1, 8):
sense.set_pixel(x, row, 0, 0, 0)
sense = SenseHat()
sense.clear()
uuid = get_uuid()
print "User UUID: " + uuid
cookie = get_cookie(uuid)
print "Cookie str: " + cookie
headers = {
'cookie': cookie,
'cache-control': "no-cache",
'postman-token': "13cbda46-56ad-e90c-20c3-5cc12a92ec34"
}
url = "https://mcoapi.minecraft.net/worlds"
response = requests.request("GET", url, headers=headers)
# print "Response code", response.status_code
json = response.json()
print(json)
servers = json['servers']
row = 0
for server in servers:
draw_status(cookie, row, server['id'])
#players = server['players']
#print (server['name'], players)
#if players is not None:
#print "Players! ", players
# sense.set_pixel(0, row, 255, 0, 0)
row += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.