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,17 +138,31 @@ optional arguments:
### spansh.py ###
```
usage: spansh.py [-h] {oldstations} ...
usage: spansh.py [-h] {nearestsystem,oldstations} ...
Script for interfacing with Spanshs API.
optional arguments:
-h, --help show this help message and exit
-h, --help show this help message and exit
subcommands:
{oldstations} sub-command help
oldstations Searches for stations with old data (>1year without an
update.
{nearestsystem,oldstations}
sub-command help
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
\`\`\`
\`\`\`
EOF
./spansh.py nearestsystem -h >> README.md
cat >> README.md << EOF
\`\`\`
\`\`\`
EOF
./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():
params = {"json": JSON.dumps({"filters": FILTERS, "sort": SORT, "size": COUNT})}
json = requests.post(APIURL, params).json()
params = {
"json": JSON.dumps({
"filters": FILTERS,
"sort": SORT,
"size": args.count
})
}
json = requests.post(APIURLS["stations"], params).json()
ret =""
for station in json["results"]:
@ -25,8 +49,13 @@ def getOldStations():
def getOldStationsInSystem(system):
FILTERS.update(system_name={"value": system})
params = {"json": JSON.dumps({"filters": FILTERS, "sort": SORT})}
json = requests.post(APIURL, params).json()
params = {
"json": JSON.dumps({
"filters": FILTERS,
"sort": SORT
})
}
json = requests.post(APIURLS["stations"], params).json()
ret =""
for station in json["results"]:
@ -44,11 +73,18 @@ parser = argparse.ArgumentParser(description="Script for interfacing with "
subparsers = parser.add_subparsers(title="subcommands", help="sub-command help",
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",
help="Searches for stations with old data (>1year without an update.")
parser_oldstations.add_argument("--system", nargs="?",
help="a single system to query. If not present, get the oldest stations "
+ "overall.")
+ "overall.")
parser_oldstations.add_argument("--count", nargs="?", type=int, default=50,
help="how many stations to output. Defaults to 50.")
parser_oldstations.add_argument("--short", action='store_true',
@ -59,18 +95,30 @@ args = parser.parse_args()
# ===========================================================================
APIURL = "http://backend3.spansh.co.uk:3000/api/stations/search"
FILTERS = {"updated_at":
{"value":
["2017-11-06",
(datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")],
"comparison": "<=>"}}
SORT = {"updated_at": {"direction": "asc"}}
COUNT = args.count
APIURLS = {
"stations": "https://spansh.co.uk/api/stations/search",
"nearest": "https://spansh.co.uk/api/nearest"
}
FILTERS = {
"updated_at":
{
"value": [
"2017-11-06",
(datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
],
"comparison": "<=>"
}
}
SORT = {
"updated_at": {
"direction": "asc"
}
}
out =""
if args.subcommand == "oldstations":
if args.subcommand == "nearestsystem":
out = getNearestSystem(args.coordinate)
elif args.subcommand == "oldstations":
if args.system:
out = getOldStationsInSystem(args.system)
else: