forked from EDCD/EDMarketConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
td.py
54 lines (43 loc) · 2.39 KB
/
td.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
# Export to Trade Dangerous
from os.path import join
from collections import defaultdict
import codecs
import numbers
from operator import itemgetter
from platform import system
from sys import platform
import time
from config import applongname, appversion, config
demandbracketmap = { 0: '?',
1: 'L',
2: 'M',
3: 'H', }
stockbracketmap = { 0: '-',
1: 'L',
2: 'M',
3: 'H', }
def export(data):
querytime = config.getint('querytime') or int(time.time())
filename = join(config.get('outdir'), '%s.%s.%s.prices' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip(), time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime))))
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(data['timestamp'], '%Y-%m-%dT%H:%M:%SZ'))
# Format described here: https://bitbucket.org/kfsone/tradedangerous/wiki/Price%20Data
h = open(filename, 'wt') # codecs can't automatically handle line endings, so encode manually where required
h.write(('#! trade.py import -\n# Created by %s %s on %s%s.\n#\n# <item name> <sellCR> <buyCR> <demand> <stock> <timestamp>\n\n@ %s/%s\n' % (applongname, appversion, platform=='darwin' and "Mac OS" or system(), not config.getint('anonymous') and ' for Cmdr '+data['commander']['name'].strip() or '', data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip())).encode('utf-8'))
# sort commodities by category
bycategory = defaultdict(list)
for commodity in data['lastStarport']['commodities']:
bycategory[commodity['categoryname']].append(commodity)
for category in sorted(bycategory):
h.write(' + %s\n' % category)
# corrections to commodity names can change the sort order
for commodity in sorted(bycategory[category], key=itemgetter('name')):
h.write(' %-23s %7d %7d %9s%c %8s%c %s\n' % (
commodity['name'],
int(commodity['sellPrice']),
int(commodity['buyPrice']),
int(commodity['demand']) if commodity['demandBracket'] else '',
demandbracketmap[commodity['demandBracket']],
int(commodity['stock']) if commodity['stockBracket'] else '',
stockbracketmap[commodity['stockBracket']],
timestamp))
h.close()