edts.py: added script to pull from Alot’s hosted EDTS API

fixes #4
This commit is contained in:
alterNERDtive 2020-07-01 09:40:36 +02:00
parent 0179369fc1
commit 92eb2c9008
4 changed files with 121 additions and 1 deletions

View file

@ -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

View file

@ -44,6 +44,34 @@ optional arguments:
--text explicitly give text output
```
### edts.py ###
```
usage: edts.py [-h] {coords} ...
Script for interfacing with Alots 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.

77
edts.py Executable file
View file

@ -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 "
+ "Alots 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)

View file

@ -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 ###
\`\`\`