async def shard(self, ctx: commands.Context, *shards: str): """ Retrieves the specified info from NationStates Uses UNIX-style arguments. Arguments will be shards, while flags will be keywords. An asterisk may be used to consume the rest of the arguments at once. Examples: [p]shard --nation Darcania census --scale "65 66" --mode score [p]shard numnations lastupdate delegate --region * 10000 Islands """ if not shards: return await ctx.send_help() request: dict = {} key = "q" for i, shard in enumerate(shards): if shard.startswith("--"): if key != "q": return await ctx.send( "No value provided for key {!r}".format(key)) key = shard[2:] elif shard.startswith("*"): request.setdefault(key, []).append(" ".join( (shard[1:], *shards[i + 1:])).strip()) key = "q" break else: request.setdefault(key, []).append(shard) key = "q" if key != "q": return await ctx.send("No value provided for key {!r}".format(key)) root = await Api(**request) await ctx.send_interactive(pagify(pretty_string(root), shorten_by=11), "xml")
async def _checkwa(self, wanation): """Check if Nation is in the WA""" Api.agent = "Kortexia" request = Api( "wa", nation=wanation, ) root = await request pretty = pretty_string(root) return pretty
async def _isinwa(self, wanation: str) -> bool: """Check if Nation is in the WA""" Api.agent = "10000 Islands Discord Bot contact Kortexia" request = Api( "wa", nation=wanation, ) try: root = await request except sans.errors.HTTPException: return False else: pretty = pretty_string(root) return "wa" in pretty.lower()
async def _ne(self, wanation): """Nations Endorsing the specified WA nation""" Api.agent = "Kortexia" request = Api( "endorsements", nation=wanation, ) root = await request pretty = pretty_string(root) x = pretty.partition("<ENDORSEMENTS>") y = ''.join(x[2]) z = y.rpartition("</ENDORSEMENTS>") end = ''.join(z[0]) return end
async def _nec(self, wanation): """Number of Nations Endorsing (Count) the specified WA nation""" Api.agent = "Kortexia" request = Api( "census", nation=wanation, mode="score", scale="66", ) root = await request pretty = pretty_string(root) x = pretty.partition("<SCORE>") y = ''.join(x[2]) z = y.rpartition("</SCORE>") end = ''.join(z[0]) return end
def main(): parser = argparse.ArgumentParser( allow_abbrev=False, description="SANS Console Entry", prog="sans", epilog="Any unknown args will be used to build the API request.", ) parser.add_argument("--version", action="version", version="%(prog)s " + sans.__version__) parser.add_argument("--agent", help="set the script's user agent") parser.add_argument( "--quit", action="store_true", help="quit the console program loop after this run", ) parser.usage = parser.format_usage() + " ..." known, unknown = parser.parse_known_args() if not any(vars(known).values()): parser.print_help(sys.stderr) sans.run_in_thread() if known.quit and not unknown: return print("Okay I'll just leave I guess...", file=sys.stderr) Api.agent = known.agent or input("User agent: ") args = () while True: print() if not args and unknown: args = (known, unknown) else: args = parser.parse_known_args( shlex.split(input(parser.prog + " "))) if args[0].agent: print( "You can't change the agent in the middle of the script.", file=sys.stderr, ) known, unknown = args if not unknown: if known.quit: return print("No query provided. Exiting...", file=sys.stderr) print("No query provided.", file=sys.stderr) parser.print_usage(sys.stderr) continue request = {} key = "q" for arg in unknown: if arg.startswith("--"): if key != "q": print("No value provided for key {!r}".format(key), file=sys.stderr) continue key = arg[2:] else: request.setdefault(key, []).append(arg) key = "q" if key != "q": print("No value provided for key {!r}".format(key), file=sys.stderr) continue request = Api(request) try: for element in request.threadsafe: print(pretty_string(element), end="") except HTTPException as e: print(e, file=sys.stderr) except Exception as e: logging.exception(e) if known.quit: return print("Exiting...", file=sys.stderr)