Improved http module, improved mcuser

This commit is contained in:
Luke Rogers 2014-02-28 16:56:32 +13:00
parent 0acb22e0a7
commit 17b75a2ba9
2 changed files with 43 additions and 11 deletions

View File

@ -1,3 +1,4 @@
import json
from util import hook, http
NAME_URL = "https://account.minecraft.net/buy/frame/checkName/{}"
@ -24,18 +25,41 @@ def get_status(name):
return "invalid"
def is_paid(name):
""" takes a name and returns true if it's assosiated with a paid minecraft account
if the account does not exist or is not paid, returns false """
def get_profile(name):
profile = {}
# form the profile request
request = {
"name": name,
"agent": "minecraft"
}
# submit the profile request
try:
headers = {"Content-Type": "application/json"}
r = http.get_json(
'https://api.mojang.com/profiles/page/1',
post_data=json.dumps(request),
headers=headers
)
except (http.URLError, http.HTTPError) as e:
raise McuError("Could not get profile status: {}".format(e))
user = r["profiles"][0]
profile["name"] = user["name"]
profile["id"] = user["id"]
try:
response = http.get(PAID_URL, user=name)
except (http.URLError, http.HTTPError) as e:
raise McuError("Could not get payment status: {}".format(e))
if "true" in response:
return True
profile["paid"] = True
else:
return False
profile["paid"] = False
return profile
@hook.command("haspaid")
@ -46,20 +70,24 @@ def mcuser(inp):
user = inp.strip()
try:
# get status of name (does it exist?)
name_status = get_status(user)
except McuError as e:
return e
if name_status == "taken":
try:
paid = is_paid(user)
# get information about user
profile = get_profile(user)
except McuError as e:
return e
return "Error: {}".format(e)
if paid:
return u"The account \x02{}\x02 exists and \x02is a paid\x02 minecraft account.".format(user)
if profile["paid"]:
return u"The account \x02{name}\x02 ({id}) exists and \x02is a paid\x02 minecraft" \
u" account.".format(**profile)
else:
return u"The account \x02{}\x02 exists, but \x02is not\x02 a paid minecraft account.".format(user)
return u"The account \x02{name}\x02 ({id}) exists, but \x02is NOT\x02 a paid minecraft" \
u" account.".format(**profile)
elif name_status == "free":
return u"The account \x02{}\x02 does not exist.".format(user)
elif name_status == "invalid":

View File

@ -52,7 +52,7 @@ def get_json(*args, **kwargs):
def open(url, query_params=None, user_agent=None, post_data=None,
referer=None, get_method=None, cookies=False, timeout=None, **kwargs):
referer=None, get_method=None, cookies=False, timeout=None, headers=None, **kwargs):
if query_params is None:
query_params = {}
@ -68,6 +68,10 @@ def open(url, query_params=None, user_agent=None, post_data=None,
if get_method is not None:
request.get_method = lambda: get_method
if headers is not None:
for header_key, header_value in headers.iteritems():
request.add_header(header_key, header_value)
request.add_header('User-Agent', user_agent)
if referer is not None: