Created
September 14, 2010 22:31
-
-
Save TkTech/579901 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Auth(object): | |
"""Provides an interface to services provided by minecraft.net""" | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.launcher_ver = 999999999 | |
def _get_version(self): | |
from urllib import urlopen | |
page = urlopen("http://www.minecraft.net/game/getversion.jsp?user={0}&password={1}&version={2}".format(self.username, self.password, self.launcher_ver)) | |
return page.read().split(":") | |
def _get_site_cookie(self): | |
"""Returns the login token from http://minecraft.net""" | |
from urllib import urlopen, urlencode | |
from httplib import HTTPConnection | |
from Cookie import SimpleCookie | |
fields= urlencode({"username": self.username, "password" : self.password}) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} | |
conn = HTTPConnection("minecraft.net:80") | |
conn.request("POST", "/login.jsp", fields, headers) | |
response = conn.getresponse() | |
if not response.status == 302: | |
return None | |
try: | |
cookies = SimpleCookie() | |
cookies.load(response.getheader("Set-Cookie")) | |
return (cookies["_uid"].value, cookies["JSESSIONID"].value) | |
except: | |
return None | |
def get_latest_version(self): | |
"""Returns the latest available alpha client version""" | |
return self._get_version()[0] | |
def get_download_ticket(self): | |
"""Returns a download ticket used for getting updated copies of alpha's minecraft.jar""" | |
return self._get_version()[1] | |
def get_case_correct_username(self): | |
"""Returns a case-corrected username of a valid alpha account""" | |
return self._get_version()[2] | |
def get_session_id(self): | |
"""Returns a session ID used for doing name authentication with alpha servers""" | |
return self._get_version()[3] | |
def get_alpha_minecraft_jar(self): | |
"""Gets the latest alpha minecraft.jar game file and returns a temporary filename where it's stored""" | |
from urllib import urlretrieve | |
fname, headers = urlretrieve("http://minecraft.net/game/minecraft.jar?user={0}&ticket={1}".format(self.username, self.get_download_ticket())) | |
return fname | |
def do_join_server(self, server_hash): | |
from urllib import urlopen | |
page = urlopen("http://www.minecraft.net/game/joinserver.jsp?user={0}&sessionId={1}&serverId={2}".format(self.username, self.get_session_id(), server_hash)) | |
if page.read().strip() == "ok": | |
return True | |
return False | |
def do_name_check(self, sername, server_hash): | |
from urllib import urlopen | |
page = urlopen("http://www.minecraft.net/game/checkserver.jsp?user={0}&serverId={1}".format(username, server_hash)) | |
if page.read().strip() == "YES": | |
return True | |
return False | |
def do_skin_change(self, skin_url): | |
from urllib import urlopen, urlencode | |
from httplib import HTTPConnection | |
import re | |
# Try to login to the site | |
cookie, sessionid = self._get_site_cookie() | |
# Check to make sure it worked | |
if cookie == None: | |
return False | |
# Do the actual skin change, which is far more annoying (thank you Notch!) than it need be. | |
params = urlencode({"url" : skin_url}) | |
headers = {"Cookie" : "_uid={0}; JSESSIONID={1}".format(cookie, sessionid)} | |
conn = HTTPConnection("minecraft.net:80") | |
conn.request("GET", "/skin/remote.jsp?{0}".format(params), headers = headers) | |
response = conn.getresponse() | |
# An "accept key" is required to confirm the skin change, so we parse it out of the HTML using | |
# regex. | |
accept_key = re.search("&accept=(.+)\">", response.read()).group(1) | |
# And now that we have that, doing the skin change is trivia | |
params = urlencode({"url" : skin_url, "accept" : accept_key}) | |
conn.request("GET", "/skin/remote.jsp?{0}".format(params), headers = headers) | |
#print conn.getresponse().read() | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment