forked from EDCD/EDMarketConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDMC.py
executable file
·167 lines (143 loc) · 6.67 KB
/
EDMC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
#
# Command-line interface. Requires prior setup through the GUI.
#
import argparse
import json
import requests
import sys
from os.path import getmtime
from time import time, sleep
from xml.etree import ElementTree
import l10n
l10n.Translations().install_dummy()
import collate
import companion
import commodity
from commodity import COMMODITY_DEFAULT
import outfitting
import loadout
import coriolis
import shipyard
import eddb
import stats
import prefs
from config import appcmdname, appversion, update_feed, config
EDDB = eddb.EDDB()
SERVER_RETRY = 5 # retry pause for Companion servers [s]
EXIT_SUCCESS, EXIT_SERVER, EXIT_CREDENTIALS, EXIT_VERIFICATION, EXIT_NOT_DOCKED, EXIT_SYS_ERR = range(6)
# quick and dirty version comparison assuming "strict" numeric only version numbers
def versioncmp(versionstring):
return map(int, versionstring.split('.'))
try:
# arg parsing
parser = argparse.ArgumentParser(prog=appcmdname, description='Prints the current system and station (if docked) to stdout and optionally writes player status, ship locations, ship loadout and/or station data to file. Requires prior setup through the accompanying GUI app.')
parser.add_argument('-v', '--version', help='print program version and exit', action='store_const', const=True)
parser.add_argument('-c', metavar='FILE', help='write ship loadout to FILE in Coriolis json format')
parser.add_argument('-e', metavar='FILE', help='write ship loadout to FILE in E:D Shipyard format')
parser.add_argument('-l', metavar='FILE', help='write ship locations to FILE in CSV format')
parser.add_argument('-m', metavar='FILE', help='write station commodity market data to FILE in CSV format')
parser.add_argument('-o', metavar='FILE', help='write station outfitting data to FILE in CSV format')
parser.add_argument('-s', metavar='FILE', help='write station shipyard data to FILE in CSV format')
parser.add_argument('-t', metavar='FILE', help='write player status to FILE in CSV format')
parser.add_argument('-d', metavar='FILE', help='write raw JSON data to FILE')
parser.add_argument('-j', help=argparse.SUPPRESS) # Import JSON dump
args = parser.parse_args()
if args.version:
latest = ''
try:
# Copied from update.py - probably should refactor
r = requests.get(update_feed, timeout = 10)
feed = ElementTree.fromstring(r.text)
items = dict([(item.find('enclosure').attrib.get('{http://www.andymatuschak.org/xml-namespaces/sparkle}version'),
item.find('title').text) for item in feed.findall('channel/item')])
lastversion = sorted(items, key=versioncmp)[-1]
if versioncmp(lastversion) > versioncmp(appversion):
latest = ' (%s is available)' % items[lastversion]
except:
pass # Quietly suppress timeouts etc.
print '%.2f%s' % (float(''.join(appversion.split('.')[:3])) / 100, latest) # just first three digits
sys.exit(EXIT_SUCCESS)
session = companion.Session()
if args.j:
# Import and collate from JSON dump
data = json.load(open(args.j))
config.set('querytime', getmtime(args.j))
else:
session.login(config.get('username'), config.get('password'))
querytime = int(time())
data = session.query()
config.set('querytime', querytime)
# Validation
if not data.get('commander') or not data['commander'].get('name','').strip():
sys.stderr.write('Who are you?!\n')
sys.exit(EXIT_SERVER)
elif not data.get('lastSystem') or not data['lastSystem'].get('name','').strip() or not data.get('lastStarport') or not data['lastStarport'].get('name','').strip():
sys.stderr.write('Where are you?!\n') # Shouldn't happen
sys.exit(EXIT_SERVER)
elif not data.get('ship') or not data['ship'].get('modules') or not data['ship'].get('name','').strip():
sys.stderr.write('What are you flying?!\n') # Shouldn't happen
sys.exit(EXIT_SERVER)
if data['lastStarport'].get('commodities'):
# Fixup anomalies in the commodity data
session.fixup(data['lastStarport']['commodities'])
# stuff we can do when not docked
if args.d:
with open(args.d, 'wt') as h:
h.write(json.dumps(data, ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': ')).encode('utf-8'))
if args.c:
coriolis.export(data, args.c)
if args.e:
loadout.export(data, args.e)
if args.l:
stats.export_ships(data, args.l)
if args.t:
stats.export_status(data, args.t)
if not data['commander'].get('docked'):
print data['lastSystem']['name']
if (args.m or args.o or args.s):
sys.stderr.write("You're not docked at a station!\n")
sys.exit(EXIT_NOT_DOCKED)
else:
sys.exit(EXIT_SUCCESS)
# Finally - the data looks sane and we're docked at a station
print '%s,%s' % (data['lastSystem']['name'], data['lastStarport']['name'])
(station_id, has_market, has_outfitting, has_shipyard) = EDDB.station(data['lastSystem']['name'], data['lastStarport']['name'])
if (args.m or args.o or args.s) and not (data['lastStarport'].get('commodities') or data['lastStarport'].get('modules')): # Ignore possibly missing shipyard info
sys.stderr.write("Station doesn't have anything!\n")
sys.exit(EXIT_SUCCESS)
if args.j:
# Collate from JSON dump
collate.addcommodities(data)
collate.addmodules(data)
collate.addships(data)
if args.m:
if data['lastStarport'].get('commodities'):
commodity.export(data, COMMODITY_DEFAULT, args.m)
else:
sys.stderr.write("Station doesn't have a market\n")
if args.o:
if data['lastStarport'].get('modules'):
outfitting.export(data, args.o)
else:
sys.stderr.write("Station doesn't supply outfitting\n")
if args.s:
if has_shipyard and not data['lastStarport'].get('ships') and not args.j:
sleep(SERVER_RETRY)
data = session.query()
if data['lastStarport'].get('ships'):
shipyard.export(data, args.s)
elif has_shipyard:
sys.stderr.write("Couldn't retrieve shipyard info\n")
else:
sys.stderr.write("Station doesn't have a shipyard\n")
sys.exit(EXIT_SUCCESS)
except companion.ServerError as e:
sys.stderr.write('Server is down\n')
sys.exit(EXIT_SERVER_DOWN)
except companion.CredentialsError as e:
sys.stderr.write('Invalid Credentials\n')
sys.exit(EXIT_CREDENTIALS)
except companion.VerificationRequired:
sys.stderr.write('Verification Required\n')
sys.exit(EXIT_VERIFICATION)