Technical Development As A Region: Part 1

This article is part of a series, with more content being published in the future.

During the creation of a region, it is often necessary to develop some form of technical toolset for easier functionality. Generally, regions will take advantage of prior-created tools, like auto-telegrammers using the NationStates (NS) Application Programming Interface (API), or endorsement monitors, or even custom created discord bots, like the AA Bot. However, some regions wish to develop their own in-house tools, whether it be for security, easier control, or other reasons. This article series will focus on how to develop some simple tools, using the NS API, and two programming languages – Bourne-Again Shell (BASH), and Python3 – and the Requests library.

First Script: Tracking Nation Totals (Python)

Many regions wish to analyze their growth over a time period. This data can be drawn from the NS History page, however, it is not always the granulated data desired. Hence, the NS API offers an easy method of data collection.

Before we get started, it is unlikely you will violate any NS rules while executing these code examples. If you are worried, try closing down any application you may have running that is accessing the NS API, such as API telegram clients, Spyglass, RATT, and KATT.

If you are on Windows 10, this is how you can set up your Python environment to access the NS API:

  1. Go to the Microsoft Store, and search ‘Python’. Install Python 3.8, or the latest Python 3 version available. Do not install any Python 2 version as it will not function for this.
  2. Install Windows Terminal from the Microsoft Store. While all systems have Powershell and CMD installed by default, this allows for both command processors to be run in the same window, and it supports tabbed terminals, which is an incredibly helpful feature.
  3. Open up your freshly installed Windows Terminal, type ‘python3’, and then press ‘Enter’. You should get something along the lines of this:
Screenshot captured on a Raspberry Pi 4. This is a general Python3 command-line interface – there will be no substantial difference between this and a Windows terminal.
Screenshot captured from the Windows Terminal app.

Feel free to play around with  it for a few minutes and try to get the hang of how Python functions, and then, when you’re ready to exit it, type ‘exit()’, and press ‘Enter’.
4. Now that you have python3 installed, it’s time to install the requests library. Package management in Python is quite simple. Type ‘python3 -m pip install requests’ and press ‘Enter’. Python will automatically collect and install the package. Congratulations, you have completed your setup!

If you are using Linux, you can connect to the NS API by following these steps:

I. Python generally comes pre-installed on Linux systems. Open up a terminal (usually by pressing Ctrl+Alt+T) and type ‘python3’. This should result in an image akin to the screenshot shown above.
II. Use the python shell some, and once you’re done, type ‘exit()’ to exit.
III. Type ‘python3 -m pip install requests’ to ensure that you have requests installed and up-to-date on your system.

Now that your Python environment has been configured, it’s time to start coding. From now on, I will not refer to any particular operating system directions. However, it is important to note that if you are using a Linux system, don’t forget to include the ‘shebang’ line at the start of the file – (#!/bin/python) – to ensure that your shell points to the correct interpreter. 

When beginning a project, it is recommended that you  create a new file folder to store all of your code. Once you have done so, move into that directory, and create a new file, and name it ‘nationTracker.py’. It is very important to include the ‘.py’ file extension on any Python file you create, otherwise the computer will not be able to tell that it is a Python file. You then need to open up the file in your Integrated Development Environment (IDE) and begin to edit.

` ` `

import requests

headers = {

    “User-Agent”: “<insert your descriptive and useful user agent here>”

}

params={

    “region” : “<your region name>”,

    “q” : “numnations”

}

r = requests.get(“https://www.nationstates.net/cgi-bin/api.cgi”, headers=headers, params = params)

print(r.text)

` ` `

If all goes well, you should see something like this:

Now you have your data! You might wonder, what can you do with it? It’s got these weird tags on it, and it’s still in the command line. How do you save your data?

That’s an excellent question and a slightly more advanced topic that we will cover in the next article. For now, you can take this information and manually place it into a file, perhaps a Google Sheet.

Congratulations! You have just written a successful Python script to get some information about your region!

We accept guest contributions!
If you have a knack for writing or want to cover something that might have been overlooked otherwise, we would love to hear from you. Join our Discord server and contact Chief Content Officer WhoAmIToDisagree#7584 (Blyatman) if interested.