2014-03-17 06:25:18 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
from http.client import HTTPConnection
|
|
|
|
import json
|
|
|
|
import base64
|
2015-09-21 21:15:01 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def replace(path,encoding=None):
|
|
|
|
mode = "wt" if encoding else "wb"
|
|
|
|
# an unpredictable temp name only needed for a+rwxt directories
|
|
|
|
tmp = '.'+path+'-tmp'
|
|
|
|
def deco(handle):
|
|
|
|
with open(tmp,mode,encoding=encoding) as out:
|
|
|
|
yield out
|
|
|
|
os.rename(tmp,path)
|
|
|
|
return deco
|
|
|
|
|
|
|
|
def maybeReplace(path,encoding=None):
|
|
|
|
def deco(handle):
|
|
|
|
if os.path.exists(path): return
|
|
|
|
return replace(path,encoding)(handle)
|
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
|
|
|
|
preview = skinsdir + "character_" + str(curskin) + "_preview.png"
|
|
|
|
if os.path.exists(preview): continue
|
|
|
|
foundOne = True
|
|
|
|
tmp = dest+'-tmp'
|
|
|
|
@maybeReplace(skinsdir + "character_" + str(curskin) + ".png")
|
|
|
|
def go(f):
|
|
|
|
f.write(base64.b64decode(bytes(s["img"], 'utf-8')))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
@maybeReplace(metadir + "character_" + str(curskin) + ".txt",
|
|
|
|
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 21:11:25 +02:00
|
|
|
try:
|
|
|
|
c.request("GET", "/skins/1/" + str(s["id"]) + ".png")
|
|
|
|
r = c.getresponse()
|
2015-09-21 21:15:01 +02:00
|
|
|
except HTTPException as e:
|
|
|
|
print(type(e),dir(e))
|
|
|
|
raise(e)
|
|
|
|
if r.status != 200:
|
|
|
|
print("Error", r.status)
|
2015-09-21 21:11:25 +02:00
|
|
|
continue
|
2015-09-21 21:15:01 +02:00
|
|
|
@replace(preview)
|
|
|
|
def go(f):
|
|
|
|
shutil.copyfileobj(r,f)
|
|
|
|
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!")
|