Created
March 13, 2012 06:15
-
-
Save TkTech/2027203 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf8 -*- | |
import re | |
import requests | |
# Regex used for authentication token. | |
_token_r = re.compile(r'name="authenticityToken" value="([0-9a-f]+)">') | |
# Regex used for server list entries. | |
_list_r = re.compile( | |
'<a href="/classic/play/([0-9a-f]+)">([^<]+)</a>\s+</td>\s+<td>(\d+)' | |
'</td>\s+<td>(\d+)</td>\s+<td>(\d+\w)</td>' | |
) | |
# Regexes used for various server fields. | |
_play_ip = re.compile(r'name="server" value="([^"]+)"') | |
_play_port = re.compile(r'name="port" value="(\d+)"') | |
_play_token = re.compile(r'name="mppass" value="([0-9a-f]+)"') | |
def login(username, password): | |
""" | |
Login to minecraft.net with the given username/password. If there was an | |
error, returns `False`, otherwise returns the session key. | |
""" | |
r = requests.get('http://www.minecraft.net/login') | |
token = _token_r.findall(r.text) | |
if not token: | |
return False | |
r = requests.post('https://www.minecraft.net/login', verify=True, data={ | |
'username': username, | |
'password': password, | |
'authenticityToken': token[0] | |
}) | |
return r.cookies.get('PLAY_SESSION', False) | |
def server_list(session_key=None, username=None, password=None): | |
""" | |
Returns a list of Creative servers using the session given by | |
`session_key`, otherwise using `username` & `password` to login. | |
""" | |
if session_key is None: | |
session_key = login(username, password) | |
if not session_key: | |
return [] | |
r = requests.get('http://www.minecraft.net/classic/list', cookies={ | |
'PLAY_SESSION': session_key | |
}) | |
fields = ('hash', 'name', 'players', 'max', 'uptime') | |
return (dict(zip(fields, x)) for x in _list_r.findall(r.text)) | |
def server_details(server_hash, session_key=None, username=None, password=None): | |
""" | |
Returns connections details for the server given by `server_hash`. Like | |
`server_list()`, uses `session_key` if given, otherwise attempts to login | |
with `username` & `password`. | |
""" | |
if session_key is None: | |
session_key = login(username, password) | |
if not session_key: | |
return {} | |
r = requests.get('http://www.minecraft.net/classic/play/%s' % server_hash, | |
cookies={ | |
'PLAY_SESSION': session_key | |
}) | |
return { | |
'ip': _play_ip.findall(r.text)[0], | |
'port': _play_port.findall(r.text)[0], | |
'token': _play_token.findall(r.text)[0] | |
} | |
if __name__ == '__main__': | |
import getpass | |
username = raw_input('Username >') | |
password = getpass.getpass('Password >') | |
# Login, then do the server list, which is more efficient for repeat calls. | |
token = login(username, password) | |
for server in server_list(token): | |
print server_details(server['hash'], token) | |
# ... OR do it using the convience method. | |
#print server_list(username=username, password=password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment