Package: pnwkit-py
pnwkit-pypnwkit-pyPolitics & War API LibraryExplore the docsJavaScript/TypeScript Version-Report Bug-Request Featurepnwkit-py is here to make interacting with the V3 Politics and War API easy. All you have to do is import the library, add your key, and make a query.Getting StartedTo get started using pnwkit-py you must first have Python and PIP installed.InstallingPython 3.9 or higher is required.Install the library using PIP.# Linux/MacOSpython3-mpipinstall-Upnwkit-py# Windowspy-3-mpipinstall-Upnwkit-pyUsageTo use pnwkit-py just import the library, create a QueryKit, then you can make synchronous or asynchronous queries.importpnwkitkit=pnwkit.QueryKit("YOUR_API_KEY")query=kit.query("nations",{"id":251584,"first":1},"nation_name")# get synchronouslyresult=query.get()# get asynchronouslyresult=awaitquery.get_async()# ORresult=awaitqueryprint(f"Nation name:{result.nations[0].nation_name}")If you want to paginate your query for more results, just ask to paginate the query. Instead of returning a tuple of results, pnwkit will return aPaginatorobject which you can iterate through. For asynchronous queries you can useasync forto iterate through the results. In addition, async paginators support batching queries to perform multiple queries simultaneously.# .batch is async only, will perform 2 queries# when it runs out of results instead of one at a timenations=query.paginate("nations")# async onlyasync_nations=query.paginate("nations").batch(2)fornationinnations:print(f"Nation name:{nation.nation_name}")asyncfornationinnations:print(f"Nation name:{nation.nation_name}")print(f"Current page:{nations.paginator_info.currentPage}")The queries are written in normal GraphQL, so you can get all the cities in a nation like thisquery=kit.query("nations",{"id",251584,"first":1},"""nation_namecities {name}""")result=query.get()print(f"First city of{result.nations[0].nation_name}:{result.nations[0].cities[0].name}")Unlike the JavaScript/TypeScript and Google Apps Script libraries, the Python library has a few additional features.Support for subscriptionsasyncdefcallback(nation):...# this function will be called every time an event is received# nation is a Nation object with the updated fieldssubscription=awaitkit.subscribe("nation","update")asyncfornationinsubscription:...# here nation is a Nation object with the updated fieldsAdditional arguments on a query will be concatenated with the first to form the query.You can also just pnwkit.Field to get support for nested fields without using raw GraphQL.query=kit.query("nations",{"id",251584,"first":1},"nation_name",pnwkit.Field("cities",{},"name"))result=query.get()print(f"First city of{result.nations[0].nation_name}:{result.nations[0].cities[0].name}")Keyword arguments provided to a query function will be passed in as query variables.When pnwkit.Variable, check the API docs for the correct type for your argument.query=kit.query("nations",{"id":pnwkit.Variable("id",pnwkit.VariableType.INT_ARRAY),"first":1},"nation_name",pnwkit.Field("cities",{},"name"),id=251584)# variables can also be set with the set_variables methodquery.set_variables(id=251584)result=query.get()print(f"First city of{result.nations[0].nation_name}:{result.nations[0].cities[0].name}")Extensions to access the daily data dumps and scrape data from the game.Access to the bankWithdraw and bankDeposit mutations.# the API requires a verified bot key to use mutationskit=pnwkit.QueryKit("YOUR_API_KEY",bot_key="YOUR_BOT_KEY",bot_key_api_key="YOUR_BOT_KEY_API_KEY")query=kit.mutation("bankDeposit",{"money":100},"id")result=query.get()print(f"Deposited ${result.bankDeposit.money}as bank record #{result.bankDeposit.id}")Query fields as aliasesquery=kit.query_as("nations","the_nations",{"id":251584,"first":1},"nation_name",pnwkit.Field("cities",{},"name"))result=query.get()print(f"First city of{result.the_nations[0].nation_name}:{result.the_nations[0].cities[0].name}")Ordering resultsquery=kit.query("nations",{"orderBy":pnwkit.OrderBy("date",pnwkit.Order.ASC)},"nation_name")result=query.get()print(f"Oldest nation{result.nations[0].nation_name}")You can look at the arguments and possible data to collect here by experimenting on theGraphQL Playground.Moving ForwardImproved support for query variablesArgument typingsIn-built cache management with subscriptionsSupport for query fragments