This repository was archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyCraft.py
More file actions
174 lines (159 loc) · 5.52 KB
/
PyCraft.py
File metadata and controls
174 lines (159 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
The PyCraft API.
Designed for easy access to the wide variety of Minecraft APIs.
You are allowed to use this in any of your projects provided you give credit.
See something you think you can contribute and make it better? Fork now and make a pull request!
Made by @ELChris414
Version 1.1 (Release)
"""
from __future__ import print_function
import json
import sys
import base64
"""
Sys is used to recognise the version of python, so anyone can use it with Python 2 or 3.
"""
if (sys.version_info >= (3, 0)):
import urllib.request
def urlspecial(link):
return urllib.request.urlopen(link).read()
def urlopen(link):
return urllib.request.urlopen(link).read().decode("utf-8")
def urlsave(link, filename):
urllib.request.urlretrieve(link, filename)
return "Saved " + link + " as " + filename
else:
import urllib2
def urlspecial(link):
return urllib2.urlopen(link).read()
def urlopen(link):
return urllib2.urlopen(link).read()
def urlsave(link, filename):
urllib2.urlretrieve(link, filename)
return "Saved " + link + " as " + filename
def getUUID(username):
"""
It returns the UUID value of the specified username as a string
"""
return json.loads(urlopen("https://api.mojang.com/users/profiles/minecraft/" + username)).get("id")
def getNameHistory(username):
"""
It returns a JSON tree of all the name changes that are found for the given username
You might want to use something like json.loads(pycraft.getNameHistory(*username that you want*))[*information that you want*]
"""
return urlopen("https://api.mojang.com/user/profiles/" + getUUID(username) + "/names")
def getMojangServerStatus():
"""
It returns a JSON tree of all the Mojang Server Statuses
You might want to use something like json.loads(pycraft.getMojangServerStatus())[*information that you want*]
Or you can use the next function which gives more precise information
"""
return urlopen("https://status.mojang.com/check")
def getSpecificMojangServerStatus(server):
"""
It is given a string with the server name and it outputs the status of that server
Possible server:
minecraft.net
session.minecraft.net
account.mojang.com
auth.mojang.com
sessionserver.mojang.com
api.mojang.com
textures.minecraft.net
mojang.com
"""
if (server == "minecraft.net"):
serverID = 0
elif (server == "session.minecraft.net"):
serverID = 1
elif (server == "account.mojang.com"):
serverID = 2
elif (server == "auth.mojang.com"):
serverID = 3
elif (server == "skins.minecraft.net"):
serverID = 4
elif (server == "authserver.mojang.com"):
serverID = 5
elif (server == "sessionserver.mojang.com"):
serverID = 6
elif (server == "api.mojang.com"):
serverID = 7
elif (server == "textures.minecraft.net"):
serverID = 8
elif (server == "mojang.com"):
serverID = 9
else:
raise TypeError("That Server doesn't exist!")
return json.loads(getMojangServerStatus())[serverID][server]
def getProfile(username):
"""
It returns a JSON tree of all information about the given username's profile.
You might want to use something like json.loads(pycraft.getProfile(*username that you want*))[*information that you want*]
Or you can use the next functions which give more precise information
"""
return urlopen("https://sessionserver.mojang.com/session/minecraft/profile/" + getUUID(username))
def getSpecificProfile(username, info):
"""
It returns the desired information for the desired username
Possible information:
id
name
"""
if (info == "id"):
return json.loads(getProfile(username))["id"]
elif (info == "name"):
return json.loads(getProfile(username))["name"]
else:
raise TypeError("That Information doesn't exist!")
def getProfileValue(username):
"""
It returns a JSON tree of all information about the given username's profile value. That includes profileId, profileName, skin and cape (if existant)
You might want to use something like json.loads(pycraft.getProfileValue(*username that you want*))[*information that you want*]
Or you can use the next functions which give more precise information
"""
return base64.b64decode(json.loads(getProfile(username))["properties"][0]["value"]).decode("utf-8")
def getSpecificProfileValue(username, info):
"""
It returns the desired information for the desired username
Possible information:
timestamp
profileId
profileName
SKIN
CAPE
"""
if (info == "timestamp"):
return json.loads(getProfileValue(username))["timestamp"]
elif (info == "profileId"):
return json.loads(getProfileValue(username))["profileId"]
elif (info == "profileName"):
return json.loads(getProfileValue(username))["profileName"]
elif (info == "SKIN"):
return json.loads(getProfileValue(username))["textures"]["SKIN"]["url"]
elif (info == "CAPE"):
try:
return json.loads(getProfileValue(username))["textures"]["CAPE"]["url"]
except:
raise TypeError("User doesn't have a cape!")
def saveSkin(username, filename):
"""
It saves the skin of the desired username to the desired filename
"""
urlsave(getSpecificProfileValue(username, "SKIN"), filename)
return "Saved " + username + "'s skin as " + filename
def saveCape(username, filename):
"""
It saves the cape of the desired username to the desired filename
"""
urlsave(getSpecificProfileValue(username, "CAPE"), filename)
return "Saved " + username + "'s cape as " + filename
def loadSkin(username):
"""
It returns the skin of the desired username as bytes
"""
return urlspecial(getSpecificProfileValue(username, "SKIN"))
def loadCape(username):
"""
It returns the cape of the desired username as bytes
"""
return urlspecial(getSpecificProfileValue(username, "CAPE"))