Last active
August 29, 2015 13:56
-
-
Save mikroskeem/9266556 to your computer and use it in GitHub Desktop.
Bukkit JSONAPI Python3 API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, urllib.request, hashlib | |
from urllib.parse import quote | |
#Stole from MinecraftAPI.py @ https://raw.github.com/alecgorge/jsonapi/master/sdk/py/MinecraftApi.py | |
def urlencode(query): | |
if isinstance(query, dict): | |
query = query.items() | |
pairs = [] | |
for item in query: | |
pairs.append("%s=%s" % (item[0], quote(str(item[1])))) | |
return "&".join(pairs) | |
class Bukkit(object): | |
def __init__(self, host, port, user, pw, salt=""): | |
self.user = user | |
self.pw = pw | |
self.salt = salt | |
self.url = "http://{}:{}/api/call?".format(host, port) | |
def call(self, method, *args): | |
auth = hashlib.sha256("{}{}{}{}".format(self.user, method, self.pw, self.salt).encode()).hexdigest() | |
result = { | |
"method": method, | |
"key": auth, | |
"args": json.dumps(args) | |
} | |
return json.loads(urllib.request.urlopen("{}{}".format(self.url, urlencode(result))).read().decode()) | |
x = Bukkit('localhost', '20059', 'admin', 'changeme') | |
print(x.call("runConsoleCommand", 'say', 'JSONAPI', 'TEST')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment