2014-02-28 16:56:32 +13:00
import json
2014-02-28 15:29:40 +13:00
from util import hook , http
NAME_URL = " https://account.minecraft.net/buy/frame/checkName/ {} "
PAID_URL = " http://www.minecraft.net/haspaid.jsp "
class McuError ( Exception ) :
pass
def get_status ( name ) :
""" takes a name and returns status """
try :
name_encoded = http . quote_plus ( name )
response = http . get ( NAME_URL . format ( name_encoded ) )
except ( http . URLError , http . HTTPError ) as e :
raise McuError ( " Could not get name status: {} " . format ( e ) )
if " OK " in response :
return " free "
elif " TAKEN " in response :
return " taken "
elif " invalid characters " in response :
return " invalid "
2014-02-28 16:56:32 +13:00
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 " ]
2014-02-28 17:28:54 +13:00
profile [ " legacy " ] = user . get ( " legacy " , False )
2014-02-28 15:29:40 +13:00
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 :
2014-02-28 16:56:32 +13:00
profile [ " paid " ] = True
2014-02-28 15:29:40 +13:00
else :
2014-02-28 16:56:32 +13:00
profile [ " paid " ] = False
return profile
2014-02-28 15:29:40 +13:00
@hook.command ( " haspaid " )
2014-02-28 15:52:05 +13:00
@hook.command ( " mcpaid " )
2014-02-28 15:29:40 +13:00
@hook.command
def mcuser ( inp ) :
""" mcpaid <username> -- Gets information about the Minecraft user <account>. """
user = inp . strip ( )
try :
2014-02-28 16:56:32 +13:00
# get status of name (does it exist?)
2014-02-28 15:29:40 +13:00
name_status = get_status ( user )
except McuError as e :
return e
if name_status == " taken " :
try :
2014-02-28 16:56:32 +13:00
# get information about user
profile = get_profile ( user )
2014-02-28 15:29:40 +13:00
except McuError as e :
2014-02-28 16:56:32 +13:00
return " Error: {} " . format ( e )
2014-02-28 15:29:40 +13:00
2014-02-28 17:28:54 +13:00
profile [ " lt " ] = " , legacy " if profile [ " legacy " ] else " "
2014-02-28 16:56:32 +13:00
if profile [ " paid " ] :
2014-03-01 18:40:36 +13:00
return u " The account \x02 {name} \x02 ( {id} {lt} ) exists. It is a \x02 paid \x02 " \
2014-02-28 16:56:32 +13:00
u " account. " . format ( * * profile )
2014-02-28 15:29:40 +13:00
else :
2014-02-28 17:28:54 +13:00
return u " The account \x02 {name} \x02 ( {id} {lt} ) exists. It \x03 4 \x02 is NOT \x02 \x0f a paid " \
2014-02-28 16:56:32 +13:00
u " account. " . format ( * * profile )
2014-02-28 15:29:40 +13:00
elif name_status == " free " :
return u " The account \x02 {} \x02 does not exist. " . format ( user )
elif name_status == " invalid " :
return u " The name \x02 {} \x02 contains invalid characters. " . format ( user )
else :
# if you see this, panic
return " Unknown Error. "