-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharchive.py
149 lines (135 loc) · 6.08 KB
/
archive.py
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
# blockBox is copyright 2009-2011 the Arc Team, the blockBox Team and other contributors.
# blockBox is licensed under the BSD 3-Clause Modified License.
# To view more details, please see the "LICENSING" file in the "docs" folder of the blockBox Package.
import cookielib, datetime, os, re, struct, sys, urllib, urllib2
from ConfigParser import SafeConfigParser as ConfigParser
from lib.twisted.internet import protocol, reactor
from blockbox.constants import *
from blockbox.protocol import BlockBoxServerProtocol
class RipClient(BlockBoxServerProtocol):
"""Once connected, send a message, then print the result."""
def connectionMade(self):
print ("Identifying to server...")
self.buffer = ""
self.name = None
self.gzipped = ""
self.sendPacked(TYPE_INITIAL, 7, self.factory.username, self.factory.mppass, 0)
def connectionLost(self, reason):
pass
def dataReceived(self, data):
# First, add the data we got onto our internal buffer
self.buffer += data
# While there's still data there...
while self.buffer:
# Examine the first byte, to see what the command is
type = ord(self.buffer[0])
try:
format = TYPE_FORMATS[type]
except KeyError:
self.transport.loseConnection()
# See if we have all its data
if len(self.buffer) - 1 < len(format):
# Nope, wait a bit
break
# OK, decode the data
parts = list(format.decode(self.buffer[1:]))
self.buffer = self.buffer[len(format)+1:]
if type == TYPE_INITIAL:
protocol, name, desc, naff = parts
print ("Server identifies as '%s'" % name)
self.lolname = name.replace("/", "")
self.name = self.lolname.replace(" ", "_")
if type == TYPE_PRECHUNK:
print ("Receiving level data...")
elif type == TYPE_CHUNK:
# Grab the chunked data
length, chunk, progress = parts
print ("...%i%%" % progress)
self.gzipped += chunk[:length]
elif type == TYPE_LEVELSIZE:
# Store level size
self.sx, self.sy, self.sz = parts
print ("Done. Got %i bytes of level." % len(self.gzipped))
print ("Level size (%s, %s, %s)" % (self.sx, self.sy, self.sz))
elif type == TYPE_SPAWNPOINT and self.name:
naff, nick, x, y, z, h, nafftoo = parts
basename = "mapdata/archives/"+"/".join([self.name, datetime.datetime.utcnow().strftime("%Y-%m-%d_%H_%M")])
try:
os.makedirs(basename)
except OSError:
pass
# Write out the blocks data
fh = open(os.path.join(basename, "blocks.gz"), mode="wb")
fh.write(self.gzipped)
fh.close()
# Write out meta info
config = ConfigParser()
x >>= 5
y >>= 5
z >>= 5
h = int(h * (360/255.0))
config.add_section("size")
config.add_section("spawn")
config.set("size", "x", str(self.sx))
config.set("size", "y", str(self.sy))
config.set("size", "z", str(self.sz))
config.set("spawn", "x", str(x))
config.set("spawn", "y", str(y))
config.set("spawn", "z", str(z))
config.set("spawn", "h", str(h))
fp = open(os.path.join(basename, "world.meta"), "w")
config.write(fp)
fp.close()
print ("Spawn point is at (%s, %s, %s, %s)" % (x, y, z, h))
print ("Saved as %s." % fh.name)
self.name = None
self.transport.loseConnection()
elif type == TYPE_ERROR:
print ("Error! %s" % parts[0])
self.transport.loseConnection()
class RipFactory(protocol.ClientFactory):
protocol = RipClient
def __init__(self, username, mppass):
self.username = username
self.mppass = mppass
def clientConnectionFailed(self, connector, reason):
print ("Connection failed.")
reactor.stop()
def clientConnectionLost(self, connector, reason):
print ("Connection terminated.")
reactor.stop()
# this connects the protocol to a server runing on port 8000
def rip(key, username, password):
login_url = 'http://minecraft.net/login.jsp'
play_url = 'http://minecraft.net/play.jsp?server=%s'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username': username, 'password': password})
print ("Logging in...")
opener.open(login_url, login_data)
print ("Fetching server info...")
html = opener.open(play_url % key).read()
ip = re.search(r'param name\="server" value="([0-9.]+)"', html).groups()[0]
port = int(re.search(r'param name\="port" value="([0-9]+)"', html).groups()[0])
mppass = re.search(r'param name\="mppass" value="([0-9a-zA-Z]+)"', html).groups()[0]
print ("Got details. Connecting...")
f = RipFactory(username, mppass)
reactor.connectTCP(ip, port, f)
reactor.run()
def main():
config = ConfigParser()
try:
config.read(os.path.join(os.path.dirname(__file__), "client.ini"))
except:
print("You need to rename client.dist.ini to client.ini.")
exit(1)
try:
username = config.get("client", "username")
password = config.get("client", "password")
except:
print("Please fill in the archive bot username and password.")
exit(1)
rip(sys.argv[1], config.get("client", "username"), config.get("client", "password"))
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()