diff --git a/lokrez/__init__.py b/lokrez/__init__.py index 77caf7a..53bd6b3 100644 --- a/lokrez/__init__.py +++ b/lokrez/__init__.py @@ -16,6 +16,8 @@ from . import resources from . import smashgg from . import version +from .games import ssbu, pplus, melee + # ============================================================================= __version__ = version.__version__ __license__ = version.__license__ @@ -72,7 +74,7 @@ def get_infos_from_url( # Get infos from smash.gg and write the config file tournament, top_players = getTournamentTop( id_or_slug = url_parsed.path.split("/")[2], - get_prefixes = options.get("use_smashgg_prefixes", False), + import_options = options, top = top, token = token, proxy = proxy, @@ -100,6 +102,53 @@ def get_infos_from_url( )) ) +# ============================================================================= +def init_resources( + imgdir, + game, + source = None, + store_raw = False, + proxy = None, + log = LOG_DUMMY, + ): + + # Create imgdir + imgdir.mkdir(parents=True, exist_ok=True) + + # Start resources download according to game + if game in ssbu.GAME.list_names(): + resources.download_res( + dstdir = imgdir, + game = ssbu, + source = source, + store_raw = store_raw, + proxy = proxy, + log = log, + ) + elif game in melee.GAME.list_names(): + resources.download_res_ssbm( + dstdir = imgdir, + game = melee, + source = source, + store_raw = store_raw, + proxy = proxy, + log = log, + ) + elif game in pplus.GAME.list_names(): + resources.download_res( + dstdir = imgdir, + game = pplus, + source = source, + store_raw = store_raw, + proxy = proxy, + log = log, + ) + else: + log.error("Unknown game '{}'".format(game)) + return 1 + + return 0 + # ============================================================================= def generate_pic( infos_or_lkrzfile = None, @@ -185,6 +234,13 @@ def main(): help = "The game you want to initialize the resources for", ) + init_parser.add_argument( + "--source", "-s", + default = None, + choices = ["spriters", "smashlyon"], + help = "From where should the resources images be downloaded", + ) + init_parser.add_argument( "--imgdir", "-ID", type = pathlib.Path, @@ -192,6 +248,12 @@ def main(): help = "The directory we should download the resources to", ) + init_parser.add_argument( + "--raw", "-r", + action = "store_true", + help = "Download the raw zipfiles instead of extracting them", + ) + # ------------------------------------------------------------------------- top8_parser = subparsers.add_parser( "top8", @@ -201,13 +263,22 @@ def main(): top8_parser.add_argument( "tournament", default = None, - help = "The tournament slug or id", + help = "The tournament url, slug or id", ) top8_parser.add_argument( "--token", "-t", default = None, - help = "the authentication token to use", + help = "the authentication token to use ; needed if you're " \ + "generating the top8 from a smash.gg url", + ) + + top8_parser.add_argument( + "--playerskinsdb", "-P", + type = (lambda s: s if s.startswith("http") else pathlib.Path(s)), + default = ROOTDIR / "data" / "playerskinsdb.json", + help = "A JSON file path or url matching player tags, characters,"\ + " sponsors, and preferred skins", ) top8_parser.add_argument( @@ -217,13 +288,6 @@ def main(): help = "The directories containing images, be careful whether " \ "you specify an absolute path or a relative one.", ) - top8_parser.add_argument( - "--playerskinsdb", "-PD", - type = (lambda s: s if s.startswith("http") else pathlib.Path(s)), - default = ROOTDIR / "data" / "playerskinsdb.json", - help = "A JSON file path or url matching player tags, characters,"\ - " sponsors, and preferred skins", - ) top8_parser.add_argument( "--cachedir", "-CD", type = pathlib.Path, @@ -243,48 +307,41 @@ def main(): help = "The local result template to use", ) top8_parser.add_argument( - "--template-options", "-O", + "--template-options", "-TO", action = "append", default = [], - help = "Template-specific options", + help = "Template-specific options (like 'covid' or 'animated')", ) top8_parser.add_argument( - "--export-options", "-E", + "--export-options", "-EO", action = "append", default = [], - help = "Export options (like svg_embed_png)", + help = "Export options (like 'svg_embed_png')", ) top8_parser.add_argument( - "--lkrz-file", "-f", - type = pathlib.Path, - default = None, - help = "The lkrz file in which the results are stored ; if it " \ - "does not exist, one will be created from the smashgg data", + "--import-options", "-IO", + action = "append", + default = [], + help = "Import options (like 'use_smashgg_prefixes')", ) + top8_parser.add_argument( "--outfile", "-o", type = pathlib.Path, default = None, help = "The SVG or PNG local result file to output to ; if it's " \ - "not specified, it will use the tournament slug as name", + "not specified, it will default to SVG and use the " \ + "tournament slug as name ; if you're generating a " \ + "localresult from a url, a LKRZ file with the same name " \ + "will also be generated along the image file (unless you " \ + "use the --no-lkrz flag).", ) - top8_parser.add_argument( - "--name-seo-delimiter", - default = None, - help = "A character that will delimit in a tournament name what " \ - "really is its name, and what's actually here for SEO " \ - "purposes (example: in 'Cornismash #42 - Ultimate Weekly " \ - "Lyon', only the 'Cornismash #42' is the tournament name, "\ - "the rest is here to help find the tournament).", - ) - top8_parser.add_argument( - "--use-smashgg-prefixes", "-P", - action = "store_true", - help = "Use the prefixes (sponsor, team, etc) set by players on " \ - "smash.gg for the tournament", - ) + parser.add_argument( "--no-lkrz", "-nl", + default = False, + action = "store_true", + help = "Do not output a LKRZ file" ) # ------------------------------------------------------------------------- parser.add_argument( "--verbose", "-v", @@ -327,20 +384,28 @@ def main(): print(version.VERSION_NAME) return 0 + # Check if command is recognized # ------------------------------------------------------------------------- if args.command not in [ "init", "top8" ]: parser.print_help() return 1 + # -- init # ------------------------------------------------------------------------- if args.command == "init": - args.imgdir.mkdir(parents=True, exist_ok=True) - resources.download_res_ssbu( - dstdir = args.imgdir, + + rv = init_resources( + imgdir = args.imgdir, + game = args.game, + source = args.source, + store_raw = args.raw, proxy = args.proxy, log = log, ) - return 0 + + return rv + + # -- top8 # ------------------------------------------------------------------------- if args.command == "top8": @@ -410,20 +475,24 @@ def main(): else: - # Get infos from smash.gg and write the config file - tournament, top_players = getTournamentTop( - id_or_slug = args.tournament, - get_prefixes = args.use_smashgg_prefixes, - top = 8, + infos = get_infos_from_url( + url = args.tournament, token = args.token, + options = args.import_options, + outform = "dict", + top = 8, proxy = args.proxy, log = log, ) + tournament = infos["tournament"] + top_players = infos["players"] + if tournament is None or top_players is None: log.error("Could not load data from smash.gg") return 1 + # Save a lkrz file lkrz_data = "\n".join( [ tournament.conf() ] \ + list(map( @@ -483,7 +552,7 @@ def main(): # ----------------------------------------------------------------------------- def getTournamentTop( id_or_slug, - get_prefixes = True, + import_options = {} top = 8, token = "", proxy = None, @@ -622,7 +691,7 @@ def getTournamentTop( except TypeError: twitterHandle = None - if get_prefixes: + if import_options.get("use_smashgg_prefixes", True): prefix = participant_data["prefix"] else: prefix = ""