2019-10-04 00:45:33 +02:00
|
|
|
|
#!/usr/bin/env python3
|
2020-01-08 08:24:31 +01:00
|
|
|
|
# PYTHON_ARGCOMPLETE_OK
|
|
|
|
|
import argcomplete, argparse
|
2019-10-04 00:45:33 +02:00
|
|
|
|
import math
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from pyEDSM.edsm.exception import ServerError, NotFoundError
|
|
|
|
|
from pyEDSM.edsm.models import System, Commander
|
|
|
|
|
|
|
|
|
|
# ===========================================================================
|
|
|
|
|
|
2020-01-08 08:49:09 +01:00
|
|
|
|
def getBodyCount(system):
|
|
|
|
|
return System(system).bodyCount
|
|
|
|
|
|
2020-01-09 21:37:42 +01:00
|
|
|
|
def distanceBetween(system1, system2, roundTo=2):
|
2020-01-08 13:45:31 +01:00
|
|
|
|
systems = System.getSystems(system1, system2)
|
2020-01-09 21:37:42 +01:00
|
|
|
|
distance = systems[0].distanceTo(systems[1], roundTo)
|
|
|
|
|
if roundTo == 0:
|
|
|
|
|
distance = int(distance)
|
|
|
|
|
return distance
|
2019-10-04 00:45:33 +02:00
|
|
|
|
|
2020-01-08 08:49:53 +01:00
|
|
|
|
def getCommanderPosition(name, apikey):
|
2020-01-08 09:33:55 +01:00
|
|
|
|
coords = Commander(name, apikey).currentPosition
|
|
|
|
|
ret = ""
|
|
|
|
|
for k in coords:
|
|
|
|
|
ret += "{}: {}, ".format(k, coords[k])
|
|
|
|
|
return ret[:-2]
|
2020-01-08 08:49:53 +01:00
|
|
|
|
|
2020-01-08 08:49:09 +01:00
|
|
|
|
def getCommanderProfileUrl(name, apikey):
|
2020-01-07 17:27:31 +01:00
|
|
|
|
return Commander(name, apikey).profileUrl
|
|
|
|
|
|
2020-01-08 08:49:09 +01:00
|
|
|
|
def getCommanderSystem(name, apikey):
|
|
|
|
|
return Commander(name, apikey).currentSystem
|
2019-10-04 05:09:33 +02:00
|
|
|
|
|
2020-01-08 14:54:39 +01:00
|
|
|
|
def getSystemList(name):
|
|
|
|
|
systems = System.getSystems(name)
|
2020-01-13 21:23:41 +01:00
|
|
|
|
ret = ""
|
2020-01-08 14:54:39 +01:00
|
|
|
|
for system in systems:
|
|
|
|
|
ret += "{}\n".format(system.name)
|
|
|
|
|
return ret[:-1]
|
|
|
|
|
|
2020-01-13 21:23:41 +01:00
|
|
|
|
def getSystemNear(name):
|
|
|
|
|
# Probably want to abort at _some_ point. I’m defining two full words left as
|
|
|
|
|
# the condition for that now.
|
|
|
|
|
if name.count(' ') < 2:
|
|
|
|
|
ret = "Aborting search at {}, require more than 2 words to limit the "
|
|
|
|
|
ret += "result set."
|
|
|
|
|
return ret.format(name)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
systems = System.getSystems(name)
|
|
|
|
|
except NotFoundError:
|
|
|
|
|
return getSystemNear(name[:-1])
|
|
|
|
|
else:
|
|
|
|
|
ret = ""
|
|
|
|
|
for system in systems:
|
|
|
|
|
ret += "{} ({}, {}, {})\n".format(system.name,
|
|
|
|
|
system.coords['x'], system.coords['y'], system.coords['z'])
|
|
|
|
|
return ret[:-1]
|
|
|
|
|
|
2019-10-04 00:45:33 +02:00
|
|
|
|
# ===========================================================================
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="A collection of tools useful for "
|
|
|
|
|
+ "exploration.")
|
|
|
|
|
subparsers = parser.add_subparsers(title="subcommands", help="sub-command help",
|
|
|
|
|
dest="subCommand")
|
|
|
|
|
|
|
|
|
|
parser_bodycount = subparsers.add_parser("bodycount",
|
|
|
|
|
help="Returns the number of bodies in a system. Will exit with code 1 on "
|
|
|
|
|
+ "server error and code 2 if the system could not be found in EDSM.")
|
2020-01-06 17:28:30 +01:00
|
|
|
|
parser_bodycount.add_argument("system", nargs=1, help="system to query")
|
2019-10-04 05:09:33 +02:00
|
|
|
|
|
|
|
|
|
parser_distance = subparsers.add_parser("distancebetween",
|
2019-10-22 01:33:35 +02:00
|
|
|
|
help="Calculates the distance between two systems. Will exit with code 1 "
|
2019-10-04 05:09:33 +02:00
|
|
|
|
+ "on server error and code 2 if (one of) the systems could not be found "
|
|
|
|
|
+ "on EDSM.")
|
2020-01-09 21:37:42 +01:00
|
|
|
|
parser_distance.add_argument("--roundto", nargs="?", type=int, default=2,
|
|
|
|
|
help="the number of digits to round to (default: 2)")
|
2020-01-06 17:28:30 +01:00
|
|
|
|
parser_distance.add_argument("system", nargs=2, help="the systems to measure")
|
2019-10-04 00:45:33 +02:00
|
|
|
|
|
2020-01-13 21:23:41 +01:00
|
|
|
|
parser_findCmdr = subparsers.add_parser("findcommander",
|
2020-01-05 19:20:20 +01:00
|
|
|
|
help="Attempts to find a CMDR’s last known position. Will exit with code 1 "
|
|
|
|
|
+ "on server error and code 2 if the CMDR could not be found on EDSM.")
|
2020-01-13 21:23:41 +01:00
|
|
|
|
group = parser_findCmdr.add_mutually_exclusive_group(required=False)
|
2020-01-07 17:27:31 +01:00
|
|
|
|
group.add_argument('--system', action='store_true',
|
|
|
|
|
help='output the commander’s last known system (default)')
|
2020-01-08 08:49:53 +01:00
|
|
|
|
group.add_argument('--coords', action='store_true',
|
|
|
|
|
help='output the commander’s last known position in {x,y,z} coordinates')
|
2020-01-07 17:27:31 +01:00
|
|
|
|
group.add_argument('--url', action='store_true',
|
|
|
|
|
help='output the commander’s profile URL')
|
2020-01-13 21:23:41 +01:00
|
|
|
|
parser_findCmdr.add_argument("name", help="the commander in question")
|
|
|
|
|
parser_findCmdr.add_argument("apikey", default="", nargs="?",
|
2020-01-06 17:24:57 +01:00
|
|
|
|
help="the commander’s EDSM API key. Can be empty for public profiles.")
|
2020-01-05 19:20:20 +01:00
|
|
|
|
|
2020-01-13 21:23:41 +01:00
|
|
|
|
parser_findSystem = subparsers.add_parser("findsystem",
|
|
|
|
|
help="Attempts to find a partially matching system that should then "
|
|
|
|
|
+ "hopefully be in the vicinity of the given system")
|
|
|
|
|
parser_findSystem.add_argument("system", help="the system in question")
|
|
|
|
|
|
2020-01-08 14:54:39 +01:00
|
|
|
|
parser_bodycount = subparsers.add_parser("systemlist",
|
|
|
|
|
help="Pulls all system names starting with the given string from EDSM")
|
|
|
|
|
parser_bodycount.add_argument("partialsystem", nargs=1,
|
|
|
|
|
help="the partial system name to query against")
|
|
|
|
|
|
2020-01-08 08:24:31 +01:00
|
|
|
|
argcomplete.autocomplete(parser)
|
2019-10-04 00:45:33 +02:00
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# ===========================================================================
|
|
|
|
|
|
2020-01-06 17:20:37 +01:00
|
|
|
|
try:
|
|
|
|
|
if args.subCommand == "bodycount":
|
|
|
|
|
out = getBodyCount(args.system[0])
|
|
|
|
|
elif args.subCommand == "distancebetween":
|
2020-01-09 21:37:42 +01:00
|
|
|
|
out = distanceBetween(args.system[0], args.system[1], args.roundto)
|
2020-01-06 17:20:37 +01:00
|
|
|
|
elif args.subCommand == "findcommander":
|
2020-01-08 08:49:53 +01:00
|
|
|
|
if args.coords:
|
2020-01-08 09:33:55 +01:00
|
|
|
|
out = getCommanderPosition(args.name, args.apikey)
|
2020-01-08 08:49:53 +01:00
|
|
|
|
elif args.url:
|
2020-01-08 08:49:09 +01:00
|
|
|
|
out = getCommanderProfileUrl(args.name, args.apikey)
|
2020-01-07 17:27:31 +01:00
|
|
|
|
else:
|
2020-01-08 08:49:09 +01:00
|
|
|
|
out = getCommanderSystem(args.name, args.apikey)
|
2020-01-13 21:23:41 +01:00
|
|
|
|
elif args.subCommand == "findsystem":
|
|
|
|
|
out = getSystemNear(args.system)
|
2020-01-08 14:54:39 +01:00
|
|
|
|
elif args.subCommand == "systemlist":
|
|
|
|
|
out = getSystemList(args.partialsystem)
|
2020-01-13 21:23:41 +01:00
|
|
|
|
else:
|
|
|
|
|
parser.print_usage()
|
|
|
|
|
sys.exit(1)
|
2020-01-06 17:20:37 +01:00
|
|
|
|
except ServerError as e:
|
|
|
|
|
print(e)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
except NotFoundError as e:
|
|
|
|
|
print(e)
|
|
|
|
|
sys.exit(2)
|
|
|
|
|
else:
|
|
|
|
|
print(out)
|
|
|
|
|
sys.exit(0)
|