spansh.py: added systemexists subcommand

This commit is contained in:
alterNERDtive 2020-03-09 19:53:49 +01:00
parent 5704593f4f
commit fec66bfebb
3 changed files with 54 additions and 3 deletions

View file

@ -138,7 +138,7 @@ optional arguments:
### spansh.py ### ### spansh.py ###
``` ```
usage: spansh.py [-h] {nearestsystem,oldstations} ... usage: spansh.py [-h] {nearestsystem,oldstations,systemexists} ...
Script for interfacing with Spanshs API. Script for interfacing with Spanshs API.
@ -146,12 +146,14 @@ optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
subcommands: subcommands:
{nearestsystem,oldstations} {nearestsystem,oldstations,systemexists}
sub-command help sub-command help
nearestsystem Searches for the nearest system in the database to nearestsystem Searches for the nearest system in the database to
given coordinates. given coordinates.
oldstations Searches for stations with old data (>1year without oldstations Searches for stations with old data (>1year without
an update. an update.
systemexists Checks if a given system exists in the search
database.
``` ```
``` ```
@ -179,6 +181,16 @@ optional arguments:
--short short output format (system/station names only) --short short output format (system/station names only)
``` ```
```
usage: spansh.py systemexists [-h] system
positional arguments:
system the system to search for
optional arguments:
-h, --help show this help message and exit
```
## Need Help / Want to Contribute? ## ## Need Help / Want to Contribute? ##
Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new) Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new)

View file

@ -92,6 +92,12 @@ EOF
cat >> README.md << EOF cat >> README.md << EOF
\`\`\` \`\`\`
\`\`\`
EOF
./spansh.py systemexists -h >> README.md
cat >> README.md << EOF
\`\`\`
## Need Help / Want to Contribute? ## ## Need Help / Want to Contribute? ##
Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new) Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new)

View file

@ -88,6 +88,31 @@ def getOldStationsInSystem(system):
return ret[:-1] return ret[:-1]
def systemExists(system):
params = {
"json": JSON.dumps({
"filters": {
"name": {
"value": system
}
}
})
}
response = requests.post(APIURLS["systems"], params)
if response.status_code != 200:
raise ServerError(url, params)
json = response.json()
if json["count"] == 0:
raise NotFoundError()
ret = ""
for system in json["results"]:
ret += "{} ({}, {}, {})\n".format(system["name"], system["x"], system["y"],
system["z"])
return ret[:-1]
# =========================================================================== # ===========================================================================
parser = argparse.ArgumentParser(description="Script for interfacing with " parser = argparse.ArgumentParser(description="Script for interfacing with "
@ -115,14 +140,20 @@ parser_oldstations.add_argument("--count", nargs="?", type=int, default=50,
parser_oldstations.add_argument("--short", action='store_true', parser_oldstations.add_argument("--short", action='store_true',
help="short output format (system/station names only)") help="short output format (system/station names only)")
parser_systemexists = subparsers.add_parser("systemexists",
help="Checks if a given system exists in the search database.")
parser_systemexists.add_argument("system", nargs=1,
help="the system to search for")
argcomplete.autocomplete(parser) argcomplete.autocomplete(parser)
args = parser.parse_args() args = parser.parse_args()
# =========================================================================== # ===========================================================================
APIURLS = { APIURLS = {
"nearest": "https://spansh.co.uk/api/nearest",
"stations": "https://spansh.co.uk/api/stations/search", "stations": "https://spansh.co.uk/api/stations/search",
"nearest": "https://spansh.co.uk/api/nearest" "systems": "https://spansh.co.uk/api/systems/search",
} }
FILTERS = { FILTERS = {
"updated_at": "updated_at":
@ -148,6 +179,8 @@ try:
out = getOldStationsInSystem(args.system) out = getOldStationsInSystem(args.system)
else: else:
out = getOldStations() out = getOldStations()
elif args.subcommand == "systemexists":
out = systemExists(args.system)
except ServerError as e: except ServerError as e:
print(e) print(e)
sys.exit(1) sys.exit(1)