spansh.py: added nearestsystem subcommand

Used to get the nearest system in the database, e.g. for plotting
a neutron route.

Useful since the elastic search powering the plotter might be behind
current EDDN data, so having a system in EDDN/EDSM doesn’t necessarily
mean it’s already available in the router.
This commit is contained in:
alterNERDtive 2020-03-09 17:39:44 +01:00
parent 19e8cbfdff
commit 07ca9ec406
3 changed files with 88 additions and 20 deletions

View file

@ -138,7 +138,7 @@ optional arguments:
### spansh.py ### ### spansh.py ###
``` ```
usage: spansh.py [-h] {oldstations} ... usage: spansh.py [-h] {nearestsystem,oldstations} ...
Script for interfacing with Spanshs API. Script for interfacing with Spanshs API.
@ -146,9 +146,23 @@ optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
subcommands: subcommands:
{oldstations} sub-command help {nearestsystem,oldstations}
oldstations Searches for stations with old data (>1year without an sub-command help
update. nearestsystem Searches for the nearest system in the database to
given coordinates.
oldstations Searches for stations with old data (>1year without
an update.
```
```
usage: spansh.py nearestsystem [-h] [--short] coordinate coordinate coordinate
positional arguments:
coordinate the coordinates to search for (order: x, y, z)
optional arguments:
-h, --help show this help message and exit
--short short output format (system name only)
``` ```
``` ```

View file

@ -80,6 +80,12 @@ EOF
cat >> README.md << EOF cat >> README.md << EOF
\`\`\` \`\`\`
\`\`\`
EOF
./spansh.py nearestsystem -h >> README.md
cat >> README.md << EOF
\`\`\`
\`\`\` \`\`\`
EOF EOF
./spansh.py oldstations -h >> README.md ./spansh.py oldstations -h >> README.md

View file

@ -9,9 +9,33 @@ from datetime import datetime, timedelta
# =========================================================================== # ===========================================================================
def getNearestSystem(coords):
params = {
"x": coords[0],
"y": coords[1],
"z": coords[2]
}
json = requests.post(APIURLS["nearest"], params).json()
ret = ""
if args.short:
ret = json["system"]["name"]
else:
system = json["system"]
ret = "{} ({}, {}, {}), {}ly".format(system["name"], system["x"],
system["y"], system["z"], round(system["distance"], 2))
return ret
def getOldStations(): def getOldStations():
params = {"json": JSON.dumps({"filters": FILTERS, "sort": SORT, "size": COUNT})} params = {
json = requests.post(APIURL, params).json() "json": JSON.dumps({
"filters": FILTERS,
"sort": SORT,
"size": args.count
})
}
json = requests.post(APIURLS["stations"], params).json()
ret ="" ret =""
for station in json["results"]: for station in json["results"]:
@ -25,8 +49,13 @@ def getOldStations():
def getOldStationsInSystem(system): def getOldStationsInSystem(system):
FILTERS.update(system_name={"value": system}) FILTERS.update(system_name={"value": system})
params = {"json": JSON.dumps({"filters": FILTERS, "sort": SORT})} params = {
json = requests.post(APIURL, params).json() "json": JSON.dumps({
"filters": FILTERS,
"sort": SORT
})
}
json = requests.post(APIURLS["stations"], params).json()
ret ="" ret =""
for station in json["results"]: for station in json["results"]:
@ -44,6 +73,13 @@ parser = argparse.ArgumentParser(description="Script for interfacing with "
subparsers = parser.add_subparsers(title="subcommands", help="sub-command help", subparsers = parser.add_subparsers(title="subcommands", help="sub-command help",
dest="subcommand", required=True) dest="subcommand", required=True)
parser_nearestsystem = subparsers.add_parser("nearestsystem",
help="Searches for the nearest system in the database to given coordinates.")
parser_nearestsystem.add_argument("coordinate", nargs=3, type=int,
help="the coordinates to search for (order: x, y, z)")
parser_nearestsystem.add_argument("--short", action='store_true',
help="short output format (system name only)")
parser_oldstations = subparsers.add_parser("oldstations", parser_oldstations = subparsers.add_parser("oldstations",
help="Searches for stations with old data (>1year without an update.") help="Searches for stations with old data (>1year without an update.")
parser_oldstations.add_argument("--system", nargs="?", parser_oldstations.add_argument("--system", nargs="?",
@ -59,18 +95,30 @@ args = parser.parse_args()
# =========================================================================== # ===========================================================================
APIURL = "http://backend3.spansh.co.uk:3000/api/stations/search" APIURLS = {
"stations": "https://spansh.co.uk/api/stations/search",
FILTERS = {"updated_at": "nearest": "https://spansh.co.uk/api/nearest"
{"value": }
["2017-11-06", FILTERS = {
(datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")], "updated_at":
"comparison": "<=>"}} {
SORT = {"updated_at": {"direction": "asc"}} "value": [
COUNT = args.count "2017-11-06",
(datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
],
"comparison": "<=>"
}
}
SORT = {
"updated_at": {
"direction": "asc"
}
}
out ="" out =""
if args.subcommand == "oldstations": if args.subcommand == "nearestsystem":
out = getNearestSystem(args.coordinate)
elif args.subcommand == "oldstations":
if args.system: if args.system:
out = getOldStationsInSystem(args.system) out = getOldStationsInSystem(args.system)
else: else: