From 92eb2c9008118a27fc1491f8768cf2d122990352 Mon Sep 17 00:00:00 2001 From: alterNERDtive Date: Wed, 1 Jul 2020 09:40:36 +0200 Subject: [PATCH] =?UTF-8?q?edts.py:=20added=20script=20to=20pull=20from=20?= =?UTF-8?q?Alot=E2=80=99s=20hosted=20EDTS=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #4 --- Makefile | 1 + README.md | 30 ++++++++++++++++++- edts.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ generate_docs.sh | 14 +++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100755 edts.py diff --git a/Makefile b/Makefile index 9712d7f..2a9b25a 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ exe: pip install --user --upgrade -r requirements.txt pip install --user --upgrade -r pyEDSM\requirements.txt python -OO -m PyInstaller --clean -yF edsm-getnearest.py + python -OO -m PyInstaller --clean -yF edts.py python -OO -m PyInstaller --clean -yF explorationtools.py python -OO -m PyInstaller --clean -yF spansh.py diff --git a/README.md b/README.md index 9886c11..103cf2e 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,34 @@ optional arguments: --text explicitly give text output ``` +### edts.py ### + +``` +usage: edts.py [-h] {coords} ... + +Script for interfacing with Alot’s hosted EDTS API. + +optional arguments: + -h, --help show this help message and exit + +subcommands: + {coords} sub-command help + coords Searches for the approximate coordinates of a given procedurally + generated system name. +``` + +``` +usage: edts.py coords [-h] [--maxuncertainty [MAXUNCERTAINTY]] system + +positional arguments: + system the system name to get coordinates for + +optional arguments: + -h, --help show this help message and exit + --maxuncertainty [MAXUNCERTAINTY] + maximum accepted uncertainty, if any +``` + ### explorationtools.py ### ``` @@ -194,4 +222,4 @@ optional arguments: ## Need Help / Want to Contribute? ## Just [file an issue](https://github.com/alterNERDtive/elite-scripts/issues/new) -here or [hop into Discord](https://discord.gg/XHNX7jN) if that is your thing. +here or [hop into Discord](https://discord.gg/uUKFdW) if that is your thing. diff --git a/edts.py b/edts.py new file mode 100755 index 0000000..bd7f560 --- /dev/null +++ b/edts.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +# PYTHON_ARGCOMPLETE_OK +import argcomplete, argparse +import json as JSON +import requests +import urllib +import sys + +from pyEDSM.edsm.exception import ServerError, NotFoundError + +# =========================================================================== + +def querystations(url, params): + response = requests.post(url, params) + if response.status_code != 200: + raise ServerError(url, params) + json = response.json() + if json["count"] == 0: + raise NotFoundError() + return json + +# =========================================================================== + +def getCoords(system): + response = requests.get(APIURLS["coords"] + urllib.parse.quote(system)) + if response.status_code != 200: + raise ServerError(url, params) + + json = response.json()['result'] + + ret = json['position'] + ret['uncertainty'] = json['uncertainty'] + + return ret + +# =========================================================================== + +parser = argparse.ArgumentParser(description="Script for interfacing with " + + "Alot’s hosted EDTS API.") +subparsers = parser.add_subparsers(title="subcommands", help="sub-command help", + dest="subcommand", required=True) + +parser_coords = subparsers.add_parser("coords", + help="Searches for the approximate coordinates of a given procedurally " + + "generated system name.") +parser_coords.add_argument("system", + help="the system name to get coordinates for") +parser_coords.add_argument("--maxuncertainty", nargs="?", type=int, + help="maximum accepted uncertainty, if any") + +argcomplete.autocomplete(parser) +args = parser.parse_args() + +# =========================================================================== + +APIURLS = { + "coords": "http://edts.thargoid.space/api/v1/system_position/" + } + +try: + if args.subcommand == "coords": + coords = getCoords(args.system) + if args.maxuncertainty: + if args.maxuncertainty < coords['uncertainty']: + raise NotFoundError() + out = "{},{},{}|{}".format(int(coords['x']), int(coords['y']), + int(coords['z']), int(coords['uncertainty'])) +except ServerError as e: + print(e) + sys.exit(1) +except NotFoundError as e: + print("Maximum uncertainty exceeded: " + str(int(coords['uncertainty'])) + + " > " + str(args.maxuncertainty)) + sys.exit(3) +else: + print(out) + sys.exit(0) diff --git a/generate_docs.sh b/generate_docs.sh index c790f2b..dadf37d 100644 --- a/generate_docs.sh +++ b/generate_docs.sh @@ -34,6 +34,20 @@ EOF cat >> README.md << EOF \`\`\` +### edts.py ### + +\`\`\` +EOF +./edts.py -h >> README.md +cat >> README.md << EOF +\`\`\` + +\`\`\` +EOF +./edts.py coords -h >> README.md +cat >> README.md << EOF +\`\`\` + ### explorationtools.py ### \`\`\`