1
0
mirror of https://github.com/SmallJoker/minetest-u_skinsdb.git synced 2024-11-10 12:10:22 +01:00

better file handling

Don't download again if the final saved file exists (the preview)

Don't modify files in-place.
This commit is contained in:
user 2015-09-21 19:15:01 +00:00
parent 953ad970c9
commit 2b7610a60f

View File

@ -2,16 +2,36 @@
from http.client import HTTPConnection
import json
import base64
import sys
def die(message,code=23):
print(message,file=sys.stderr)
raise SystemExit(code)
server = "minetest.fensta.bplaced.net"
skinsdir = "u_skins/textures/"
metadir = "u_skins/meta/"
i = 1
pages = 1
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)
c = HTTPConnection(server)
def addpage(page):
global i, pages
global curskin, pages
print("Page: " + str(page))
r = 0
try:
@ -20,42 +40,52 @@ def addpage(page):
except Exception:
if r != 0:
if r.status != 200:
print("Error", r.status)
exit(r.status)
die("Error", r.status)
return
data = r.read().decode()
l = json.loads(data)
if not l["success"]:
print("Success != True")
exit(1)
die("Success != True")
r = 0
pages = int(l["pages"])
foundOne = False
for s in l["skins"]:
f = open(skinsdir + "character_" + str(i) + ".png", "wb")
f.write(base64.b64decode(bytes(s["img"], 'utf-8')))
f.close()
f = open(metadir + "character_" + str(i) + ".txt", "w")
f.write(str(s["name"]) + '\n')
f.write(str(s["author"]) + '\n')
f.write(str(s["license"]))
f.close()
# 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"]))
try:
c.request("GET", "/skins/1/" + str(s["id"]) + ".png")
r = c.getresponse()
except Exception:
if r != 0:
if r.status != 200:
print("Error", r.status)
except HTTPException as e:
print(type(e),dir(e))
raise(e)
if r.status != 200:
print("Error", r.status)
continue
data = r.read()
f = open(skinsdir + "character_" + str(i) + "_preview.png", "wb")
f.write(data)
f.close()
i = i + 1
@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
addpage(1)
if pages > 1:
for p in range(pages-1):
addpage(p+2)
curpage = 1
while pages > curpage:
curpage = curpage + 1
addpage(curpage)
print("Skins have been updated!")