2014-03-17 06:25:18 +01:00
|
|
|
#!/usr/bin/python3
|
2015-09-21 19:46:06 +02:00
|
|
|
from http.client import HTTPConnection,HTTPException
|
2014-03-17 06:25:18 +01:00
|
|
|
import json
|
|
|
|
import base64
|
2015-09-21 19:46:06 +02:00
|
|
|
from contextlib import closing
|
|
|
|
import sys,os,shutil
|
2015-09-21 21:15:01 +02:00
|
|
|
|
|
|
|
def die(message,code=23):
|
|
|
|
print(message,file=sys.stderr)
|
|
|
|
raise SystemExit(code)
|
2014-03-17 06:25:18 +01:00
|
|
|
|
|
|
|
server = "minetest.fensta.bplaced.net"
|
|
|
|
skinsdir = "u_skins/textures/"
|
|
|
|
metadir = "u_skins/meta/"
|
2015-09-21 21:15:01 +02:00
|
|
|
curskin = 0
|
|
|
|
pages = None
|
|
|
|
|
2015-09-21 19:46:06 +02:00
|
|
|
def replace(location,base,encoding=None,path=None):
|
|
|
|
if path is None:
|
|
|
|
path = os.path.join(location,base)
|
2015-09-21 21:15:01 +02:00
|
|
|
mode = "wt" if encoding else "wb"
|
|
|
|
# an unpredictable temp name only needed for a+rwxt directories
|
2015-09-21 19:46:06 +02:00
|
|
|
tmp = os.path.join(location,'.'+base+'-tmp')
|
2015-09-21 21:15:01 +02:00
|
|
|
def deco(handle):
|
|
|
|
with open(tmp,mode,encoding=encoding) as out:
|
2015-09-21 19:46:06 +02:00
|
|
|
handle(out)
|
2015-09-21 21:15:01 +02:00
|
|
|
os.rename(tmp,path)
|
|
|
|
return deco
|
|
|
|
|
2015-09-21 19:46:06 +02:00
|
|
|
def maybeReplace(location,base,encoding=None):
|
2015-09-21 21:15:01 +02:00
|
|
|
def deco(handle):
|
2015-09-21 19:46:06 +02:00
|
|
|
path = os.path.join(location,base)
|
2015-09-21 21:15:01 +02:00
|
|
|
if os.path.exists(path): return
|
2015-09-21 19:46:06 +02:00
|
|
|
return replace(location,base,encoding=encoding,path=path)(handle)
|
|
|
|
return deco
|
2014-03-17 06:25:18 +01:00
|
|
|
|
|
|
|
c = HTTPConnection(server)
|
|
|
|
def addpage(page):
|
2015-09-21 21:15:01 +02:00
|
|
|
global curskin, pages
|
2015-09-21 21:11:25 +02:00
|
|
|
print("Page: " + str(page))
|
|
|
|
r = 0
|
|
|
|
try:
|
|
|
|
c.request("GET", "/api/get.json.php?getlist&page=" + str(page) + "&outformat=base64")
|
|
|
|
r = c.getresponse()
|
|
|
|
except Exception:
|
|
|
|
if r != 0:
|
|
|
|
if r.status != 200:
|
2015-09-21 21:15:01 +02:00
|
|
|
die("Error", r.status)
|
2015-09-21 21:11:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
data = r.read().decode()
|
|
|
|
l = json.loads(data)
|
|
|
|
if not l["success"]:
|
2015-09-21 21:15:01 +02:00
|
|
|
die("Success != True")
|
2015-09-21 21:11:25 +02:00
|
|
|
r = 0
|
|
|
|
pages = int(l["pages"])
|
2015-09-21 21:15:01 +02:00
|
|
|
foundOne = False
|
2015-09-21 21:11:25 +02:00
|
|
|
for s in l["skins"]:
|
2015-09-21 21:15:01 +02:00
|
|
|
# make sure to increment this, even if the preview exists!
|
|
|
|
curskin = curskin + 1
|
2015-09-21 19:46:06 +02:00
|
|
|
previewbase = "character_" + str(curskin) + "_preview.png"
|
|
|
|
preview = os.path.join(skinsdir, previewbase)
|
|
|
|
if os.path.exists(preview):
|
|
|
|
print('skin',curskin,'already retrieved')
|
|
|
|
continue
|
|
|
|
print('updating skin',curskin)
|
2015-09-21 21:15:01 +02:00
|
|
|
foundOne = True
|
2015-09-21 19:46:06 +02:00
|
|
|
@maybeReplace(skinsdir, "character_" + str(curskin) + ".png")
|
2015-09-21 21:15:01 +02:00
|
|
|
def go(f):
|
|
|
|
f.write(base64.b64decode(bytes(s["img"], 'utf-8')))
|
|
|
|
f.close()
|
|
|
|
|
2015-09-21 19:46:06 +02:00
|
|
|
@maybeReplace(metadir, "character_" + str(curskin) + ".txt",
|
2015-09-21 21:15:01 +02:00
|
|
|
encoding='utf-8')
|
|
|
|
def go(f):
|
|
|
|
f.write(str(s["name"]) + '\n')
|
|
|
|
f.write(str(s["author"]) + '\n')
|
|
|
|
f.write(str(s["license"]))
|
2015-09-21 19:46:06 +02:00
|
|
|
url = "/skins/1/" + str(s["id"]) + ".png"
|
2015-09-21 21:11:25 +02:00
|
|
|
try:
|
2015-09-21 19:46:06 +02:00
|
|
|
c.request("GET", url)
|
|
|
|
with closing(c.getresponse()) as r:
|
|
|
|
if r.status != 200:
|
|
|
|
print("Error", r.status)
|
|
|
|
continue
|
|
|
|
@replace(skinsdir,previewbase,path=preview)
|
|
|
|
def go(f):
|
|
|
|
shutil.copyfileobj(r,f)
|
2015-09-21 21:15:01 +02:00
|
|
|
except HTTPException as e:
|
2015-09-21 19:46:06 +02:00
|
|
|
die("Couldn't get {} because of a {} (url={})".format(
|
|
|
|
s["id"],
|
|
|
|
e,
|
|
|
|
url))
|
2015-09-21 21:15:01 +02:00
|
|
|
if not foundOne:
|
|
|
|
print("No skins updated on this page. Seems we're done?")
|
|
|
|
raise SystemExit
|
2014-03-17 06:25:18 +01:00
|
|
|
addpage(1)
|
2015-09-21 21:15:01 +02:00
|
|
|
curpage = 1
|
|
|
|
while pages > curpage:
|
|
|
|
curpage = curpage + 1
|
|
|
|
addpage(curpage)
|
2014-08-29 22:02:35 +02:00
|
|
|
print("Skins have been updated!")
|