Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from argparse import ArgumentParser, SUPPRESS
- import sterra.histerra as hst
- from sterra._sterrage_ import DEFAULT_EXPORT_PATH, HISTORY_ARG_KEYS, PROG, DESCRIPTION, EPILOG, HISTORY_ARGUMENTS, \
- HISTORY_STORE_TRUE
- from sterra.sterools import isId
- from sterra.core import main, _parser
- from sterra._outputs_ import _ as outputs
- _ = outputs()
- currentID = ""
- currentUsername = ""
- currentTarget = ""
- def parser() -> tuple:
- command = ["sterra", "export", "-ssid", currentID, "-u", currentUsername, "-t", currentTarget]
- print(command)
- ARG_FORMAT = "format of the file (default:\"excel\")"
- ARG_EXPORT_PATH = f"""path to the directory to export the file(s) (default:"{DEFAULT_EXPORT_PATH}")"""
- ARG_SELECTED_FILE = "id or path of the file"
- ARG_URL = "print url instead of usernames"
- def _formatDefault(a: dict) -> dict:
- """Capture les erreurs d'arguments, change les id de path en path."""
- _.colors = False if a.get("no_colors") else True
- _._raise = a.get("raw_raising")
- wich = a["wich"]
- no_false = {k: v for k, v in a.items() if
- (k if wich == "analyse" else v)} # Analyse module needs all value including None or False
- nfKeys = list(no_false.keys())
- if wich in ["compare", "analyse"]:
- if wich == "compare":
- compare_requ_args = ["not_common_usernames", "common_usernames"]
- if not set(compare_requ_args) & set(list(no_false.keys())):
- parser.error(
- f"""compare: compare requires at least one of the arguments: {", ".join([f'''--{x.replace("_", "-")}''' for x in compare_requ_args])}""")
- elif wich == "analyse":
- analyse_requ_args = ["personnal", "interests"]
- if not [item for item in list(no_false.keys()) if a[item] and item in analyse_requ_args]:
- no_false["personnal"] = True
- if wich in ["compare", "analyse"]:
- if a["no_print"] and not a["export"]:
- parser.error(
- f"{wich}: {wich} requires --export if you choose --no-print (avoids making a job without any output)")
- if wich == "history":
- if len([k for k, v in no_false.items() if k in HISTORY_ARG_KEYS]) != 1:
- parser.error("history: history module requires only one argument between the ones available")
- if no_false.get("no_exp_limit") and not a.get("express"):
- parser.error("export: --no-exp-limit can't be applied if --express is not enabled")
- if "fi" in nfKeys:
- if type(no_false["fi"]) is list:
- no_false["fi"] = [(hst.file_id(fi) if isId(fi) else fi) for fi in no_false["fi"]]
- if None in no_false["fi"]:
- _.r(IndexError('One of the input fileId have not been found in the history.'))
- else:
- no_false["fi"] = hst.file_id(no_false["fi"]) if isId(no_false["fi"]) else no_false["fi"]
- if not no_false["fi"]:
- _.r(IndexError('The input fileId have not been found in the history.'))
- if "format" in nfKeys:
- no_false["format"] = no_false["format"] if no_false["format"] != "excel" else "xlsx"
- if "name" in nfKeys:
- classic_err = "--name can't be used to export two files (they would have the same name)"
- if wich == "export":
- if no_false.get("target") == "both":
- parser.error(classic_err)
- elif wich == "compare":
- if "not_common_usernames" in nfKeys and "common_usernames" in nfKeys and "export" in nfKeys:
- parser.error(classic_err)
- elif wich == "analyse":
- if a["personnal"] and a["interests"] and a["export"]:
- parser.error(classic_err)
- no_false.pop("wich")
- return no_false
- parser = ArgumentParser(prog=PROG, description=DESCRIPTION, epilog=EPILOG)
- parser.add_argument("--raw-raising", action="store_true",
- help="raises Exceptions with python \"raise\"; useful for debug.")
- parser.add_argument("--no-colors", action="store_true",
- help="print all outputs in b&w")
- sub_parsers = parser.add_subparsers()
- # export
- export_parser = sub_parsers.add_parser("export", add_help=False,
- epilog="https://github.com/novitae/sterraxcyl/wiki/Export")
- exp_required = export_parser.add_argument_group(title="positional arguments")
- exp_exclued_username = exp_required.add_mutually_exclusive_group(required=True)
- exp_exclued_username.add_argument("-u", "--username", metavar="U", type=str,
- help="target's instagram username")
- exp_exclued_username.add_argument("-id", metavar="ID", type=int,
- help="target's instagram id")
- exp_exclued_target = exp_required.add_mutually_exclusive_group(required=True)
- exp_exclued_target.add_argument("-t", "--target", choices=["followers", "following", "both", "mutuals"],
- help="list to export")
- exp_exclued_target.add_argument("-p", "--part", const=True, nargs="?",
- help="id of the part") # The part id leads to a dict in history containing its path and the target list
- exp_login = export_parser.add_argument_group(title="login arguments")
- exp_exc_login = exp_login.add_mutually_exclusive_group(required=True)
- exp_exc_login.add_argument("-lcrd", "--login-credentials", metavar=("U", "P"), nargs=2,
- help="login by credentials (USERNAME PASSWORD)")
- exp_exc_login.add_argument("-ssid", "--login-session-id", metavar="S",
- help="login by sessionid")
- exp_export = export_parser.add_argument_group(title="export arguments")
- exp_export_data = exp_export.add_mutually_exclusive_group()
- exp_export_data.add_argument("--all-infos", action="store_true",
- help="exports extra informations (not interesting ones)")
- exp_export_data.add_argument("--only-usernames", action="store_true",
- help="exports only the usernames, not the data linked to")
- exp_export.add_argument("-f", "--format", choices=["excel", "csv", "json"], default="excel",
- help=ARG_FORMAT)
- exp_export.add_argument("--path", metavar="P", default=DEFAULT_EXPORT_PATH,
- help=ARG_EXPORT_PATH)
- exp_export.add_argument("--name", metavar="N",
- help="custom name for the exported file")
- exp_speed = export_parser.add_argument_group(title="speed arguments")
- exp_exc_speed = exp_speed.add_mutually_exclusive_group()
- exp_exc_speed.add_argument("-d", "--delay", type=int,
- help="delay between requests")
- exp_exc_speed.add_argument("-e", "--express", action="store_true",
- help="express requests (deactivated if > 109 total usernames to avoid blocking)")
- exp_optional = export_parser.add_argument_group(title="optional arguments")
- exp_optional.add_argument("--no-exp-limit", action="store_true",
- help="disable the username count limitation on express mode")
- # For the day instagram will find a way to put a more frequent ratelimit
- # exp_optional.add_argument("--auto-launchback", action="store_true",
- # help="relaunch automatically the scraping while all usernames haven't been scraped")
- exp_optional.add_argument('-h', '--help', action='help', default=SUPPRESS,
- help='show this help message and exit')
- export_parser.set_defaults(wich="export")
- # compare
- compare_parser = sub_parsers.add_parser("compare", epilog="https://github.com/novitae/sterraxcyl/wiki/Compare")
- compare_parser.add_argument("fi", metavar="{ID,PATH} {ID,PATH}", nargs=2, # Must do a metavar like this to avoid:
- help=f"{ARG_SELECTED_FILE}s to compare") # https://stackoverflow.com/questions/20680882/in-python-argparse-crashes-when-using-h
- compare_parser.add_argument("-n", "--no-print", action="store_true",
- help="doesn\"t print results")
- compare_parser.add_argument("-u", "--url", action="store_true",
- help=ARG_URL)
- compare_parser.add_argument("-i", "--invert", action="store_true",
- help="invert the order of the lists")
- com_want = compare_parser.add_argument_group(title="action")
- com_want.add_argument("--common-usernames", action="store_true",
- help="commons usernames between lists")
- com_want.add_argument("--not-common-usernames", action="store_true",
- help="not commons usernames between lists (result vary depending on the list order; use --invert)")
- comp_export = compare_parser.add_argument_group(title="export data")
- comp_export.add_argument("-e", "--export", action="store_true",
- help="export the result")
- comp_export.add_argument("-f", "--format", choices=["excel", "csv", "json"], default="excel",
- help=ARG_FORMAT)
- comp_export.add_argument("-p", "--path", default=DEFAULT_EXPORT_PATH,
- help=ARG_EXPORT_PATH)
- comp_export.add_argument("--name", metavar="N",
- help="custom name for the exported file")
- compare_parser.set_defaults(wich="compare")
- # analyse
- analyse_parser = sub_parsers.add_parser("analyse", epilog="https://github.com/novitae/sterraxcyl/wiki/Analyse")
- analyse_parser.add_argument("fi", metavar="{ID,PATH}",
- help=f"{ARG_SELECTED_FILE} to analyse")
- analyse_parser.add_argument("-i", "--invert", action="store_true",
- help="print by the end of the list")
- analyse_parser.add_argument("-e", "--export", action="store_true",
- help="export results")
- analyse_parser.add_argument("-f", "--format", choices=["excel", "csv", "json"], default="excel",
- help=ARG_FORMAT)
- analyse_parser.add_argument("--ignore-over", type=int,
- help="quantity of followers over wich we consider the account as impossible to be in the close circle of the target (default:10000)")
- analyse_parser.add_argument("--name", metavar="N",
- help="custom name for the exported file")
- analyse_parser.add_argument("--no-print", action="store_true",
- help="don't print results")
- analyse_parser.add_argument("-p", "--path", default=DEFAULT_EXPORT_PATH,
- help=ARG_EXPORT_PATH)
- analyse_parser.add_argument("--pctg", type=int,
- help="percentage under wich results will not be printed (between 0 and 98)")
- analyse_parser.add_argument("-s", "--size", type=int,
- help="size of the output")
- analyse_parser.add_argument("-u", "--url", action="store_true",
- help=ARG_URL)
- ana_want = analyse_parser.add_argument_group(title="analysis")
- ana_want.add_argument("--personnal", action="store_true",
- help="probabilities for close circle accounts")
- ana_want.add_argument("--interests", action="store_true",
- help="probabilities for interests account")
- analyse_parser.set_defaults(wich="analyse")
- # convert
- convert_parser = sub_parsers.add_parser("convert", epilog="https://github.com/novitae/sterraxcyl/wiki/Convert")
- convert_parser.add_argument('fi', metavar="{ID,PATH}",
- help=f'{ARG_SELECTED_FILE} to convert')
- convert_parser.add_argument('-f', '--format', choices=['excel', 'csv', 'json'], required=True,
- help=ARG_FORMAT)
- convert_parser.add_argument("--name", metavar="N",
- help="custom name for the exported file")
- convert_parser.add_argument("-p", "--path", metavar="P", default=DEFAULT_EXPORT_PATH,
- help=ARG_EXPORT_PATH)
- convert_parser.set_defaults(wich="convert")
- # history
- history_parser = sub_parsers.add_parser("history",
- usage=f"""sterra history [-h] {{{"|".join([(f"--{a}" if a in HISTORY_STORE_TRUE else f"--{a} {a.upper()}") for a in HISTORY_ARGUMENTS])}}}""",
- epilog="https://github.com/novitae/sterraxcyl/wiki/History")
- history_parser.add_argument("-a", f"--all", action="store_true",
- help=f"""show all the export history""")
- history_parser.add_argument(f"--clear", action="store_true",
- help=f"""clear the history""")
- history_parser.add_argument(f"--clear-parts", action="store_true",
- help=f"""delete all part stored""")
- history_parser.add_argument(f"--compare-tree",
- help=f"""shows the tree of a compare file""")
- history_parser.add_argument("-i", f"--file-id",
- help=f"""show the path associated to the filled id""")
- history_parser.add_argument("-m", f"--match",
- help=f"""show the items containing the filled string (can be a regex if it is used like ' ?"YOUR REGEX"? ')""")
- history_parser.add_argument("-p", f"--path",
- help=f"""show the id associated to the filled path""")
- history_parser.set_defaults(wich="history")
- args = parser.parse_args(command)
- print(args)
- try:
- print(vars(args))
- return args.wich, _formatDefault(vars(args))
- except AttributeError:
- parser.exit(parser.print_help())
- _parser = parser
- def register(ssid: str, user: str, target: str):
- global currentTarget, currentID, currentUsername
- currentID = ssid
- currentUsername = user
- currentTarget = target
- def run():
- main()
- register("TOKEN", "USER", "followers")
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement