From 95f5de2377d19448edc40241f94a5d2e995b14a4 Mon Sep 17 00:00:00 2001 From: alterNERDtive Date: Wed, 4 Mar 2020 18:07:56 +0100 Subject: [PATCH] added spansh.py for Spansh API related shenanigans --- Makefile | 1 + README.md | 29 ++++++++++++++++++++ generate_docs.sh | 14 ++++++++++ spansh.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100755 spansh.py diff --git a/Makefile b/Makefile index 272ff67..02ccf96 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ exe: pip install --user --upgrade -r pyEDSM\requirements.txt python -OO -m PyInstaller --clean -yF edsm-getnearest.py python -OO -m PyInstaller --clean -yF explorationtools.py + python -OO -m PyInstaller --clean -yF spansh.py # probably won’t work unless you’re me :) release: clean diff --git a/README.md b/README.md index 0c7d767..6f7c72f 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,35 @@ optional arguments: -h, --help show this help message and exit ``` +### spansh.py ### + +``` +usage: spansh.py [-h] {oldstations} ... + +Script for interfacing with Spansh’s API. + +optional arguments: + -h, --help show this help message and exit + +subcommands: + {oldstations} sub-command help + oldstations Searches for stations with old data (>1 year without an + update. +``` + +``` +usage: spansh.py oldstations [-h] [--system [SYSTEM]] [--count [COUNT]] + [--systemlist] + +optional arguments: + -h, --help show this help message and exit + --system [SYSTEM] a single system to query. If not present, get the oldest + stations overall. + --count [COUNT] how many stations to output. Defaults to 50. + --systemlist outputs a list of systems to visit _only_, no station + names (for)easy system names c&p +``` + ## Need Help / Want to Contribute? ## Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new) diff --git a/generate_docs.sh b/generate_docs.sh index 3bfb2c7..b183f74 100644 --- a/generate_docs.sh +++ b/generate_docs.sh @@ -72,6 +72,20 @@ EOF cat >> README.md << EOF \`\`\` +### spansh.py ### + +\`\`\` +EOF +./spansh.py -h >> README.md +cat >> README.md << EOF +\`\`\` + +\`\`\` +EOF +./spansh.py oldstations -h >> README.md +cat >> README.md << EOF +\`\`\` + ## Need Help / Want to Contribute? ## Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new) diff --git a/spansh.py b/spansh.py new file mode 100755 index 0000000..fecfcaf --- /dev/null +++ b/spansh.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# PYTHON_ARGCOMPLETE_OK +import argcomplete, argparse +import requests +import sys + +# =========================================================================== + +def getOldStations(count, systemsonly): + url = "http://backend3.spansh.co.uk:3000/api/stations/search" + params = {"json": '{"filters":{"updated_at":{"value":["2017-11-06","2019-03-01"],"comparison":"<=>"}},"sort":[{"updated_at":{"direction":"asc"}}],"size":' + str(count) + '}'} + + json = requests.post(url, params).json() + + ret ="" + + if systemsonly: + for station in json["results"]: + ret += "{}\n".format(station["system_name"]) + else: + for station in json["results"]: + ret += "{}: {} ({})\n".format(station["system_name"], station["name"], + station["updated_at"]) + + return ret[:-1] + +def getOldStationsInSystem(system): + url = "http://backend3.spansh.co.uk:3000/api/stations/search" + params = {"json": '{"filters": {"system_name": {"value": "' + system + '"},"updated_at":{"value":["2017-11-06","2019-03-01"],"comparison":"<=>"}},"sort":[{"updated_at":{"direction":"asc"}}]}'} + + json = requests.post(url, params).json() + + ret ="" + + for station in json["results"]: + ret += "{}, ".format(station["name"]) + + return ret[:-2] + +# =========================================================================== + +parser = argparse.ArgumentParser(description="Script for interfacing with " + + "Spansh’s API.") +subparsers = parser.add_subparsers(title="subcommands", help="sub-command help", + dest="subcommand", required=True) + +parser_oldstations = subparsers.add_parser("oldstations", + help="Searches for stations with old data (>1 year without an update.") +parser_oldstations.add_argument("--system", nargs="?", + help="a single system to query. If not present, get the oldest stations " + + "overall.") +parser_oldstations.add_argument("--count", nargs="?", type=int, default=50, + help="how many stations to output. Defaults to 50.") +parser_oldstations.add_argument("--systemlist", action='store_true', + help="outputs a list of systems to visit _only_, no station names (for)" + + "easy system names c&p") + +argcomplete.autocomplete(parser) +args = parser.parse_args() + +# =========================================================================== + +out ="" +if args.subcommand == "oldstations": + if args.system: + out = getOldStationsInSystem(args.system) + else: + out = getOldStations(args.count, args.systemlist) + +print(out) +sys.exit(0)