Skip to content

Instantly share code, notes, and snippets.

@ed588
Last active April 7, 2018 14:58
Show Gist options
  • Save ed588/e80fafe511b04ed61493b10fc0033d1f to your computer and use it in GitHub Desktop.
Save ed588/e80fafe511b04ed61493b10fc0033d1f to your computer and use it in GitHub Desktop.
NationStates issue checker

NationStates Issue Checker for Windows 10

This script checks every 10 (by default) minutes and notifies you if you have new issues on NationStates.

This only works on Windows 10 as it uses win10toast to notify you of new issues - that could be changed though if anyone is sufficiently interested.

Requirements

  • win10toast - display toast notifications
  • requests - make HTTP requests to the nationstates API
  • beautifulsoup - parsing the xml returned from the API(probably a bit overkill, but it works)
  • lxml - required for beautifulsoup to parse XML

Usage

  1. Download the script and install dependencies.
  2. Fill in the NATION_NAME constant with the name of your NS nation, with spaces replaced with underscores.
  3. Fill in the AUTOLOGIN constant with the autologin (encrypted password) of your nation, which can be obtained as explained below.
  4. (Optional) Change the TIME_TO_WAIT constant to the amount of time, in minutes, you want the script to wait before checking to see whether you have new issues.
  5. Run the script. You may want to make it run automatically on startup.

Obtaining the autologin string

Run something like the following:

curl -H "X-Password: YOUR NATIONSTATES PASSWORD HERE" -A "YOUR NATION NAME" -D - "https://www.nationstates.net/cgi-bin/api.cgi?nation=NATION_NAME_WITH_UNDERSCORES&q=ping"

or an equivalent using another https request tool. Note down the value of the x-autologin header in the response. This is your autologin string.

Notes

  • If you are logged in to NationStates in a browser and are active at the time this script makes a request, it may get an error because the two sessions conflict.
  • No validation is done to ensure the authentication details you provide are valid.
NATION_NAME = "" # nation name here
AUTOLOGIN = "" # autologin string goes here
TIME_TO_WAIT = 10 # in minutes
from win10toast import ToastNotifier
toaster = ToastNotifier()
import time
import requests
import lxml
from bs4 import BeautifulSoup
def check_and_notify():
url = "https://www.nationstates.net/cgi-bin/api.cgi?nation="+NATION_NAME+"&q=issues"
hd = {"User-agent" : NATION_NAME, "X-Autologin": AUTOLOGIN}
r = requests.get(url, headers=hd)
soup = BeautifulSoup(r.text, "xml")
if len(soup.NATION.ISSUES("ISSUE")) > 0:
toaster.show_toast("New NS issue!", NATION_NAME+" has a new nationstates issue!")
while True:
check_and_notify()
time.sleep(60*TIME_TO_WAIT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment