Advertisement
Guest User

Untitled

a guest
Sep 26th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.36 KB | None | 0 0
  1. from argparse import ArgumentParser, SUPPRESS
  2.  
  3. import sterra.histerra as hst
  4. from sterra._sterrage_ import DEFAULT_EXPORT_PATH, HISTORY_ARG_KEYS, PROG, DESCRIPTION, EPILOG, HISTORY_ARGUMENTS, \
  5.     HISTORY_STORE_TRUE
  6. from sterra.sterools import isId
  7. from sterra.core import main, _parser
  8. from sterra._outputs_ import _ as outputs
  9.  
  10. _ = outputs()
  11. currentID = ""
  12. currentUsername = ""
  13. currentTarget = ""
  14. def parser() -> tuple:
  15.     command = ["sterra", "export", "-ssid", currentID, "-u", currentUsername, "-t", currentTarget]
  16.     print(command)
  17.     ARG_FORMAT = "format of the file (default:\"excel\")"
  18.     ARG_EXPORT_PATH = f"""path to the directory to export the file(s) (default:"{DEFAULT_EXPORT_PATH}")"""
  19.     ARG_SELECTED_FILE = "id or path of the file"
  20.     ARG_URL = "print url instead of usernames"
  21.  
  22.     def _formatDefault(a: dict) -> dict:
  23.         """Capture les erreurs d'arguments, change les id de path en path."""
  24.         _.colors = False if a.get("no_colors") else True
  25.         _._raise = a.get("raw_raising")
  26.  
  27.         wich = a["wich"]
  28.         no_false = {k: v for k, v in a.items() if
  29.                     (k if wich == "analyse" else v)}  # Analyse module needs all value including None or False
  30.         nfKeys = list(no_false.keys())
  31.  
  32.         if wich in ["compare", "analyse"]:
  33.             if wich == "compare":
  34.                 compare_requ_args = ["not_common_usernames", "common_usernames"]
  35.                 if not set(compare_requ_args) & set(list(no_false.keys())):
  36.                     parser.error(
  37.                         f"""compare: compare requires at least one of the arguments: {", ".join([f'''--{x.replace("_", "-")}''' for x in compare_requ_args])}""")
  38.  
  39.             elif wich == "analyse":
  40.                 analyse_requ_args = ["personnal", "interests"]
  41.                 if not [item for item in list(no_false.keys()) if a[item] and item in analyse_requ_args]:
  42.                     no_false["personnal"] = True
  43.  
  44.             if wich in ["compare", "analyse"]:
  45.                 if a["no_print"] and not a["export"]:
  46.                     parser.error(
  47.                         f"{wich}: {wich} requires --export if you choose --no-print (avoids making a job without any output)")
  48.  
  49.         if wich == "history":
  50.             if len([k for k, v in no_false.items() if k in HISTORY_ARG_KEYS]) != 1:
  51.                 parser.error("history: history module requires only one argument between the ones available")
  52.  
  53.         if no_false.get("no_exp_limit") and not a.get("express"):
  54.             parser.error("export: --no-exp-limit can't be applied if --express is not enabled")
  55.  
  56.         if "fi" in nfKeys:
  57.             if type(no_false["fi"]) is list:
  58.                 no_false["fi"] = [(hst.file_id(fi) if isId(fi) else fi) for fi in no_false["fi"]]
  59.                 if None in no_false["fi"]:
  60.                     _.r(IndexError('One of the input fileId have not been found in the history.'))
  61.             else:
  62.                 no_false["fi"] = hst.file_id(no_false["fi"]) if isId(no_false["fi"]) else no_false["fi"]
  63.                 if not no_false["fi"]:
  64.                     _.r(IndexError('The input fileId have not been found in the history.'))
  65.  
  66.         if "format" in nfKeys:
  67.             no_false["format"] = no_false["format"] if no_false["format"] != "excel" else "xlsx"
  68.  
  69.         if "name" in nfKeys:
  70.             classic_err = "--name can't be used to export two files (they would have the same name)"
  71.             if wich == "export":
  72.                 if no_false.get("target") == "both":
  73.                     parser.error(classic_err)
  74.  
  75.             elif wich == "compare":
  76.                 if "not_common_usernames" in nfKeys and "common_usernames" in nfKeys and "export" in nfKeys:
  77.                     parser.error(classic_err)
  78.  
  79.             elif wich == "analyse":
  80.                 if a["personnal"] and a["interests"] and a["export"]:
  81.                     parser.error(classic_err)
  82.  
  83.         no_false.pop("wich")
  84.         return no_false
  85.  
  86.     parser = ArgumentParser(prog=PROG, description=DESCRIPTION, epilog=EPILOG)
  87.  
  88.     parser.add_argument("--raw-raising", action="store_true",
  89.                         help="raises Exceptions with python \"raise\"; useful for debug.")
  90.     parser.add_argument("--no-colors", action="store_true",
  91.                         help="print all outputs in b&w")
  92.  
  93.     sub_parsers = parser.add_subparsers()
  94.  
  95.     # export
  96.     export_parser = sub_parsers.add_parser("export", add_help=False,
  97.                                            epilog="https://github.com/novitae/sterraxcyl/wiki/Export")
  98.  
  99.     exp_required = export_parser.add_argument_group(title="positional arguments")
  100.     exp_exclued_username = exp_required.add_mutually_exclusive_group(required=True)
  101.     exp_exclued_username.add_argument("-u", "--username", metavar="U", type=str,
  102.                                       help="target's instagram username")
  103.     exp_exclued_username.add_argument("-id", metavar="ID", type=int,
  104.                                       help="target's instagram id")
  105.     exp_exclued_target = exp_required.add_mutually_exclusive_group(required=True)
  106.     exp_exclued_target.add_argument("-t", "--target", choices=["followers", "following", "both", "mutuals"],
  107.                                     help="list to export")
  108.     exp_exclued_target.add_argument("-p", "--part", const=True, nargs="?",
  109.                                     help="id of the part")  # The part id leads to a dict in history containing its path and the target list
  110.  
  111.     exp_login = export_parser.add_argument_group(title="login arguments")
  112.     exp_exc_login = exp_login.add_mutually_exclusive_group(required=True)
  113.     exp_exc_login.add_argument("-lcrd", "--login-credentials", metavar=("U", "P"), nargs=2,
  114.                                help="login by credentials (USERNAME PASSWORD)")
  115.     exp_exc_login.add_argument("-ssid", "--login-session-id", metavar="S",
  116.                                help="login by sessionid")
  117.  
  118.     exp_export = export_parser.add_argument_group(title="export arguments")
  119.     exp_export_data = exp_export.add_mutually_exclusive_group()
  120.     exp_export_data.add_argument("--all-infos", action="store_true",
  121.                                  help="exports extra informations (not interesting ones)")
  122.     exp_export_data.add_argument("--only-usernames", action="store_true",
  123.                                  help="exports only the usernames, not the data linked to")
  124.     exp_export.add_argument("-f", "--format", choices=["excel", "csv", "json"], default="excel",
  125.                             help=ARG_FORMAT)
  126.     exp_export.add_argument("--path", metavar="P", default=DEFAULT_EXPORT_PATH,
  127.                             help=ARG_EXPORT_PATH)
  128.     exp_export.add_argument("--name", metavar="N",
  129.                             help="custom name for the exported file")
  130.  
  131.     exp_speed = export_parser.add_argument_group(title="speed arguments")
  132.     exp_exc_speed = exp_speed.add_mutually_exclusive_group()
  133.     exp_exc_speed.add_argument("-d", "--delay", type=int,
  134.                                help="delay between requests")
  135.     exp_exc_speed.add_argument("-e", "--express", action="store_true",
  136.                                help="express requests (deactivated if > 109 total usernames to avoid blocking)")
  137.  
  138.     exp_optional = export_parser.add_argument_group(title="optional arguments")
  139.     exp_optional.add_argument("--no-exp-limit", action="store_true",
  140.                               help="disable the username count limitation on express mode")
  141.     # For the day instagram will find a way to put a more frequent ratelimit
  142.     # exp_optional.add_argument("--auto-launchback", action="store_true",
  143.     #                    help="relaunch automatically the scraping while all usernames haven't been scraped")
  144.     exp_optional.add_argument('-h', '--help', action='help', default=SUPPRESS,
  145.                               help='show this help message and exit')
  146.  
  147.     export_parser.set_defaults(wich="export")
  148.  
  149.     # compare
  150.     compare_parser = sub_parsers.add_parser("compare", epilog="https://github.com/novitae/sterraxcyl/wiki/Compare")
  151.  
  152.     compare_parser.add_argument("fi", metavar="{ID,PATH} {ID,PATH}", nargs=2,  # Must do a metavar like this to avoid:
  153.                                 help=f"{ARG_SELECTED_FILE}s to compare")  # https://stackoverflow.com/questions/20680882/in-python-argparse-crashes-when-using-h
  154.     compare_parser.add_argument("-n", "--no-print", action="store_true",
  155.                                 help="doesn\"t print results")
  156.     compare_parser.add_argument("-u", "--url", action="store_true",
  157.                                 help=ARG_URL)
  158.     compare_parser.add_argument("-i", "--invert", action="store_true",
  159.                                 help="invert the order of the lists")
  160.  
  161.     com_want = compare_parser.add_argument_group(title="action")
  162.     com_want.add_argument("--common-usernames", action="store_true",
  163.                           help="commons usernames between lists")
  164.     com_want.add_argument("--not-common-usernames", action="store_true",
  165.                           help="not commons usernames between lists (result vary depending on the list order; use --invert)")
  166.  
  167.     comp_export = compare_parser.add_argument_group(title="export data")
  168.     comp_export.add_argument("-e", "--export", action="store_true",
  169.                              help="export the result")
  170.     comp_export.add_argument("-f", "--format", choices=["excel", "csv", "json"], default="excel",
  171.                              help=ARG_FORMAT)
  172.     comp_export.add_argument("-p", "--path", default=DEFAULT_EXPORT_PATH,
  173.                              help=ARG_EXPORT_PATH)
  174.     comp_export.add_argument("--name", metavar="N",
  175.                              help="custom name for the exported file")
  176.  
  177.     compare_parser.set_defaults(wich="compare")
  178.  
  179.     # analyse
  180.     analyse_parser = sub_parsers.add_parser("analyse", epilog="https://github.com/novitae/sterraxcyl/wiki/Analyse")
  181.  
  182.     analyse_parser.add_argument("fi", metavar="{ID,PATH}",
  183.                                 help=f"{ARG_SELECTED_FILE} to analyse")
  184.     analyse_parser.add_argument("-i", "--invert", action="store_true",
  185.                                 help="print by the end of the list")
  186.     analyse_parser.add_argument("-e", "--export", action="store_true",
  187.                                 help="export results")
  188.     analyse_parser.add_argument("-f", "--format", choices=["excel", "csv", "json"], default="excel",
  189.                                 help=ARG_FORMAT)
  190.     analyse_parser.add_argument("--ignore-over", type=int,
  191.                                 help="quantity of followers over wich we consider the account as impossible to be in the close circle of the target (default:10000)")
  192.     analyse_parser.add_argument("--name", metavar="N",
  193.                                 help="custom name for the exported file")
  194.     analyse_parser.add_argument("--no-print", action="store_true",
  195.                                 help="don't print results")
  196.     analyse_parser.add_argument("-p", "--path", default=DEFAULT_EXPORT_PATH,
  197.                                 help=ARG_EXPORT_PATH)
  198.     analyse_parser.add_argument("--pctg", type=int,
  199.                                 help="percentage under wich results will not be printed (between 0 and 98)")
  200.     analyse_parser.add_argument("-s", "--size", type=int,
  201.                                 help="size of the output")
  202.     analyse_parser.add_argument("-u", "--url", action="store_true",
  203.                                 help=ARG_URL)
  204.  
  205.     ana_want = analyse_parser.add_argument_group(title="analysis")
  206.     ana_want.add_argument("--personnal", action="store_true",
  207.                           help="probabilities for close circle accounts")
  208.     ana_want.add_argument("--interests", action="store_true",
  209.                           help="probabilities for interests account")
  210.  
  211.     analyse_parser.set_defaults(wich="analyse")
  212.  
  213.     # convert
  214.     convert_parser = sub_parsers.add_parser("convert", epilog="https://github.com/novitae/sterraxcyl/wiki/Convert")
  215.  
  216.     convert_parser.add_argument('fi', metavar="{ID,PATH}",
  217.                                 help=f'{ARG_SELECTED_FILE} to convert')
  218.     convert_parser.add_argument('-f', '--format', choices=['excel', 'csv', 'json'], required=True,
  219.                                 help=ARG_FORMAT)
  220.     convert_parser.add_argument("--name", metavar="N",
  221.                                 help="custom name for the exported file")
  222.     convert_parser.add_argument("-p", "--path", metavar="P", default=DEFAULT_EXPORT_PATH,
  223.                                 help=ARG_EXPORT_PATH)
  224.  
  225.     convert_parser.set_defaults(wich="convert")
  226.  
  227.     # history
  228.     history_parser = sub_parsers.add_parser("history",
  229.                                             usage=f"""sterra history [-h] {{{"|".join([(f"--{a}" if a in HISTORY_STORE_TRUE else f"--{a} {a.upper()}") for a in HISTORY_ARGUMENTS])}}}""",
  230.                                             epilog="https://github.com/novitae/sterraxcyl/wiki/History")
  231.  
  232.     history_parser.add_argument("-a", f"--all", action="store_true",
  233.                                 help=f"""show all the export history""")
  234.     history_parser.add_argument(f"--clear", action="store_true",
  235.                                 help=f"""clear the history""")
  236.     history_parser.add_argument(f"--clear-parts", action="store_true",
  237.                                 help=f"""delete all part stored""")
  238.     history_parser.add_argument(f"--compare-tree",
  239.                                 help=f"""shows the tree of a compare file""")
  240.     history_parser.add_argument("-i", f"--file-id",
  241.                                 help=f"""show the path associated to the filled id""")
  242.     history_parser.add_argument("-m", f"--match",
  243.                                 help=f"""show the items containing the filled string (can be a regex if it is used like ' ?"YOUR REGEX"? ')""")
  244.     history_parser.add_argument("-p", f"--path",
  245.                                 help=f"""show the id associated to the filled path""")
  246.  
  247.     history_parser.set_defaults(wich="history")
  248.  
  249.     args = parser.parse_args(command)
  250.     print(args)
  251.  
  252.     try:
  253.         print(vars(args))
  254.         return args.wich, _formatDefault(vars(args))
  255.     except AttributeError:
  256.         parser.exit(parser.print_help())
  257.  
  258. _parser = parser
  259.  
  260. def register(ssid: str, user: str, target: str):
  261.     global currentTarget, currentID, currentUsername
  262.     currentID = ssid
  263.     currentUsername = user
  264.     currentTarget = target
  265. def run():
  266.     main()
  267.  
  268. register("TOKEN", "USER", "followers")
  269. run()
  270.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement