diff --git a/README.md b/README.md index bd7950d..40f2382 100644 --- a/README.md +++ b/README.md @@ -138,17 +138,31 @@ optional arguments: ### spansh.py ### ``` -usage: spansh.py [-h] {oldstations} ... +usage: spansh.py [-h] {nearestsystem,oldstations} ... Script for interfacing with Spansh’s 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 (>1 year 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 (>1 year 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) ``` ``` diff --git a/generate_docs.sh b/generate_docs.sh index b183f74..cb9a73f 100644 --- a/generate_docs.sh +++ b/generate_docs.sh @@ -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 diff --git a/spansh.py b/spansh.py index 4c6c798..d7381c8 100755 --- a/spansh.py +++ b/spansh.py @@ -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 (>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.") + + "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: