2020-07-19 11:04:58 -04:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2020-07-22 12:05:15 -04:00
|
|
|
from .characters_ssbu import EVERYONE
|
2020-07-19 11:04:58 -04:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
2020-07-22 12:05:15 -04:00
|
|
|
def download_file(url, with_progressbar = True, proxy = None):
|
2020-07-19 11:04:58 -04:00
|
|
|
|
|
|
|
r = requests.get(
|
|
|
|
url,
|
|
|
|
stream = with_progressbar,
|
2020-07-22 12:05:15 -04:00
|
|
|
proxies = { "https": proxy, "http": proxy },
|
2020-07-19 11:04:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if not with_progressbar:
|
|
|
|
return io.BytesIO(r.content)
|
|
|
|
|
|
|
|
total = r.headers.get("content-length")
|
|
|
|
|
|
|
|
f = io.BytesIO()
|
|
|
|
|
|
|
|
if total is not None:
|
|
|
|
downloaded = 0
|
|
|
|
total = int(total)
|
|
|
|
for data in r.iter_content(
|
|
|
|
chunk_size = max(int(total/1000), 1024*1024),
|
|
|
|
):
|
|
|
|
f.write(data)
|
|
|
|
downloaded += len(data)
|
|
|
|
done = int(50*downloaded/total)
|
|
|
|
sys.stdout.write( "\r[{}{}] ({:02d}%)".format(
|
|
|
|
"█" * done,
|
|
|
|
" " * (50-done),
|
|
|
|
done*2,
|
|
|
|
) )
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
else:
|
|
|
|
f = write(r.content)
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
2020-07-22 12:05:15 -04:00
|
|
|
def download_res_ssbu(dstdir, proxy = None, log=None):
|
2020-07-19 11:04:58 -04:00
|
|
|
"""Downloads SSBU resources from spriters and rename them according to
|
|
|
|
lokrez expectations"""
|
|
|
|
|
|
|
|
stock_icons_url = "https://www.spriters-resource.com/download/111395/"
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Download stock icons
|
|
|
|
log.info("Downloading stock icons...")
|
2020-07-22 12:05:15 -04:00
|
|
|
fstocks = download_file(stock_icons_url, proxy = proxy)
|
2020-07-19 11:04:58 -04:00
|
|
|
zfstocks = zipfile.ZipFile(fstocks)
|
|
|
|
|
|
|
|
# Iter over each character
|
2020-07-22 12:05:15 -04:00
|
|
|
for character in EVERYONE:
|
2020-07-19 11:04:58 -04:00
|
|
|
log.info("Downloading images for {}...".format(character.name))
|
|
|
|
|
|
|
|
# Create directory for this character
|
|
|
|
chardir = os.path.join(
|
|
|
|
dstdir,
|
|
|
|
character.name,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
os.mkdir( chardir )
|
|
|
|
|
|
|
|
except FileExistsError:
|
|
|
|
log.warning(
|
|
|
|
"Directory already exists for {}".format(character.name)
|
|
|
|
)
|
|
|
|
|
|
|
|
if os.listdir( chardir ):
|
|
|
|
log.warning(
|
|
|
|
"Directory not empty for {}, skipping" \
|
|
|
|
.format(character.name)
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Download urls & write image files
|
|
|
|
for url in character.spritersurls:
|
2020-07-22 12:05:15 -04:00
|
|
|
f = download_file(url, proxy = proxy)
|
2020-07-19 11:04:58 -04:00
|
|
|
with zipfile.ZipFile(f) as zfchar:
|
|
|
|
for zf in [zfchar,zfstocks]:
|
|
|
|
for source_filename in zf.namelist():
|
|
|
|
|
|
|
|
if "No Gamma Fix" in source_filename:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.basename(source_filename) in ["","Tag.txt"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if character.codename not in source_filename:
|
|
|
|
continue
|
|
|
|
|
|
|
|
target_filename = os.path.basename(source_filename)
|
|
|
|
|
|
|
|
target_filename = target_filename.replace(
|
|
|
|
character.codename,
|
|
|
|
character.name,
|
|
|
|
)
|
|
|
|
|
|
|
|
log.debug("Writing file '{}'".format(target_filename))
|
|
|
|
|
|
|
|
target_filename = os.path.join(
|
|
|
|
chardir,
|
|
|
|
target_filename,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(target_filename, "wb") as tf:
|
|
|
|
tf.write(zf.read(source_filename))
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level = logging.DEBUG,
|
|
|
|
format = "%(message)s",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"dstdir",
|
|
|
|
default = None,
|
|
|
|
help = "directory where to store the downloaded resources " \
|
|
|
|
"(default to a temporary file)",
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.dstdir is None:
|
|
|
|
args.dstdir = tempfile.mkdtemp()
|
|
|
|
logging.warning(
|
|
|
|
"Storing in temporary directory : {}".format(args.dstdir)
|
|
|
|
)
|
|
|
|
|
|
|
|
download_res_ssbu(
|
|
|
|
dstdir = args.dstdir,
|
|
|
|
log = logging,
|
|
|
|
)
|