spansh.py: added raw output option

mostly for debugging
This commit is contained in:
alterNERDtive 2021-03-14 14:39:57 +01:00
parent bf31991875
commit 59619ca8bf

View file

@ -23,7 +23,7 @@ def querystations(url, params):
# =========================================================================== # ===========================================================================
def getNearestSystem(coords): def getNearestSystem(coords, raw=False):
params = { params = {
"x": coords[0], "x": coords[0],
"y": coords[1], "y": coords[1],
@ -35,6 +35,8 @@ def getNearestSystem(coords):
json = response.json() json = response.json()
if raw: return json
ret = None ret = None
system = json["system"] system = json["system"]
if args.short: if args.short:
@ -49,7 +51,7 @@ def getNearestSystem(coords):
return ret return ret
def getOldStations(): def getOldStations(raw=False):
params = { params = {
"json": JSON.dumps({ "json": JSON.dumps({
"filters": FILTERS, "filters": FILTERS,
@ -59,6 +61,8 @@ def getOldStations():
} }
json = querystations(APIURLS["stations"], params) json = querystations(APIURLS["stations"], params)
if raw: return json
ret = "" ret = ""
for station in json["results"]: for station in json["results"]:
if args.short: if args.short:
@ -69,7 +73,7 @@ def getOldStations():
return ret[:-1] return ret[:-1]
def getOldStationsInSystem(system): def getOldStationsInSystem(system, raw=False):
FILTERS.update(system_name={"value": system}) FILTERS.update(system_name={"value": system})
params = { params = {
"json": JSON.dumps({ "json": JSON.dumps({
@ -79,6 +83,8 @@ def getOldStationsInSystem(system):
} }
json = querystations(APIURLS["stations"], params) json = querystations(APIURLS["stations"], params)
if raw: return json
ret = "" ret = ""
# exclude carriers # exclude carriers
stations = list(filter(lambda station: not station["type"] == "Drake-Class Carrier", json["results"])) stations = list(filter(lambda station: not station["type"] == "Drake-Class Carrier", json["results"]))
@ -96,7 +102,7 @@ def getOldStationsInSystem(system):
return ret[:-1] return ret[:-1]
def systemExists(system): def systemExists(system, raw=False):
params = { params = {
"json": JSON.dumps({ "json": JSON.dumps({
"filters": { "filters": {
@ -111,6 +117,9 @@ def systemExists(system):
raise ServerError(url, params) raise ServerError(url, params)
json = response.json() json = response.json()
if raw: return json
if json["count"] == 0: if json["count"] == 0:
raise NotFoundError() raise NotFoundError()
@ -125,6 +134,8 @@ def systemExists(system):
parser = argparse.ArgumentParser(description="Script for interfacing with " parser = argparse.ArgumentParser(description="Script for interfacing with "
+ "Spanshs API.") + "Spanshs API.")
parser.add_argument("--raw", action='store_true',
help="output raw json (mostly for debugging)")
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)
@ -187,11 +198,11 @@ try:
} }
} }
if args.system: if args.system:
out = getOldStationsInSystem(args.system) out = getOldStationsInSystem(args.system, args.raw)
else: else:
out = getOldStations() out = getOldStations(args.raw)
elif args.subcommand == "systemexists": elif args.subcommand == "systemexists":
out = systemExists(args.system) out = systemExists(args.system, args.raw)
except ServerError as e: except ServerError as e:
print(e) print(e)
sys.exit(1) sys.exit(1)