Skip to main content

Synchronous / Asynchronous HTTPX extension for NationStates

Project description

sans

pypi Licensed under the MIT License Downloads Ruff Code style: black pre-commit.ci status Documentation Status

Synchronous / Asynchronous NationStates

A fully typed extension for HTTPX for the NationStates API.

Installing

python3 -m pip install -U sans

Development version:

python3 -m pip install -U https://github.com/zephyrkul/sans/archive/master.zip#egg=sans

Examples

Synchronous

import sans
from xml.etree import ElementTree as ET

def main():
   sans.set_agent("Darcania")
   request = sans.Nation(
      "darcania",
      "fullname population flag census",
      mode="score",
      scale="65 66",
   )
   root = sans.get(request).xml
   sans.indent(root)
   print(ET.tostring(root, encoding="unicode"))

   with sans.stream("GET", sans.RegionsDump()) as response:
      for region in response.iter_xml():
         sans.indent(region)
         print(ET.tostring(region, encoding="unicode"))

if __name__ == "__main__":
   main()

Asynchronous

import asyncio
import sans
from xml.etree import ElementTree as ET

async def main():
   sans.set_agent("Darcania")
   async with sans.AsyncClient() as client:
      request = sans.Nation(
         "darcania",
         "fullname population flag census",
         mode="score",
         scale="65 66",
      )
      root = (await client.get(request)).xml
      sans.indent(root)
      print(ET.tostring(root, encoding="unicode"))

      async with client.stream("GET", sans.RegionsDump()) as response:
         async for region in response.aiter_xml():
            sans.indent(region)
            print(ET.tostring(region, encoding="unicode"))

if __name__ == "__main__":
   asyncio.run(main())

Authentication

auth = sans.NSAuth(password="hunter2")
sans.get(sans.Nation("testlandia", "ping"), auth=auth)
# X-Autologin is automatically retrieved and stored for when the auth object is re-used
print(auth.autologin)
# X-Pin is cached internally for repeated requests
root = sans.get(sans.Nation("testlandia", "packs"), auth=auth).xml

Telegrams

limiter = sans.TelegramLimiter(recruitment=False)
# The Telegram API can be used without a TelegramLimiter, but marking it ahead of time can save an API call.
response = sans.get(sans.Telegram(client="abcd1234", tgid="1234", key="abcdef1234567890", to="testlandia"), auth=limiter)
assert response.content = b"queued"

Command Line

sans --nation darcania census --scale "65 66" --mode score --agent Darcania
<NATION id="darcania">
   <CENSUS>
      <SCALE id="65">
         <SCORE>8145.00</SCORE>
      </SCALE>
      <SCALE id="66">
         <SCORE>0.00</SCORE>
      </SCALE>
   </CENSUS>
</NATION>

sans --nation testlandia fullname
<NATION id="testlandia">
   <FULLNAME>The Hive Mind of Testlandia</FULLNAME>
</NATION>

sans --region "the north pacific" numnations lastupdate
<REGION id="the_north_pacific">
   <LASTUPDATE>1685681810</LASTUPDATE>
   <NUMNATIONS>9535</NUMNATIONS>
</REGION>

sans --quit
No query provided. Exiting...

Requirements

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sans-1.2.3.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

sans-1.2.3-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file sans-1.2.3.tar.gz.

File metadata

  • Download URL: sans-1.2.3.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for sans-1.2.3.tar.gz
Algorithm Hash digest
SHA256 98b82ae04bdc4ec6a59645b8b2c15b9d3a8fc2ec9cfc910c1cc64391468b7618
MD5 e3ad70c79fca6f72e964e8b20e00ac60
BLAKE2b-256 891b3d75cfbd93e8605730dcfebdc03dbf4e710b077e49990a5c548a63ec5728

See more details on using hashes here.

File details

Details for the file sans-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: sans-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for sans-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1179de3b0f72bf27b7aa554af792f55f4ebbc9daa3476d96ec657748ce762021
MD5 ba63a76677c6d9baf89c949e63babeba
BLAKE2b-256 cca19e438e850e3624da321850a6d6e4b460d308628d266ac650565b4ddfc0ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page