mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-14 16:27:13 +03:00
Add average prices to command-line output
This commit is contained in:
parent
b04386ad8d
commit
ee28daacc9
5
EDMC.py
5
EDMC.py
@ -13,7 +13,8 @@ import l10n
|
||||
l10n.Translations().install_dummy()
|
||||
|
||||
import companion
|
||||
import bpc
|
||||
import commodity
|
||||
from commodity import COMMODITY_DEFAULT
|
||||
import outfitting
|
||||
import loadout
|
||||
import coriolis
|
||||
@ -115,7 +116,7 @@ try:
|
||||
if data['lastStarport'].get('commodities'):
|
||||
# Fixup anomalies in the commodity data
|
||||
session.fixup(data['lastStarport']['commodities'])
|
||||
bpc.export(data, True, args.m)
|
||||
commodity.export(data, COMMODITY_DEFAULT, args.m)
|
||||
elif has_market:
|
||||
sys.stderr.write("Error: Can't get market data!\n")
|
||||
else:
|
||||
|
@ -29,7 +29,8 @@ from l10n import Translations
|
||||
Translations().install(config.get('language') or None)
|
||||
|
||||
import companion
|
||||
import bpc
|
||||
import commodity
|
||||
from commodity import COMMODITY_BPC, COMMODITY_CSV
|
||||
import td
|
||||
import eddn
|
||||
import edsm
|
||||
@ -433,11 +434,11 @@ class AppWindow:
|
||||
else:
|
||||
if data['lastStarport'].get('commodities'):
|
||||
if config.getint('output') & config.OUT_CSV:
|
||||
bpc.export(data, True)
|
||||
commodity.export(data, COMMODITY_CSV)
|
||||
if config.getint('output') & config.OUT_TD:
|
||||
td.export(data)
|
||||
if config.getint('output') & config.OUT_BPC:
|
||||
bpc.export(data, False)
|
||||
commodity.export(data, COMMODITY_BPC)
|
||||
|
||||
elif has_market and (config.getint('output') & (config.OUT_CSV|config.OUT_TD|config.OUT_BPC|config.OUT_EDDN)):
|
||||
# Overwrite any previous error message
|
||||
|
47
bpc.py
47
bpc.py
@ -1,47 +0,0 @@
|
||||
# Export in Slopey's BPC format
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from os.path import join
|
||||
import hashlib
|
||||
import codecs
|
||||
import numbers
|
||||
import time
|
||||
|
||||
from config import config
|
||||
|
||||
bracketmap = { 0: '',
|
||||
1: 'Low',
|
||||
2: 'Med',
|
||||
3: 'High', }
|
||||
|
||||
def export(data, csv=False, filename=None):
|
||||
|
||||
querytime = config.getint('querytime') or int(time.time())
|
||||
|
||||
if not filename:
|
||||
filename = join(config.get('outdir'), '%s.%s.%s.%s' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip(), time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)), csv and 'csv' or 'bpc'))
|
||||
|
||||
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime))
|
||||
header = 'System;Station;Commodity;Sell;Buy;Demand;;Supply;;Date;\n'
|
||||
rowheader = '%s;%s' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip())
|
||||
if not csv: # bpc
|
||||
header = 'userID;' + header
|
||||
cmdr = data['commander']['name'].strip()
|
||||
rowheader = '%s;%s' % (config.getint('anonymous') and hashlib.md5(cmdr.encode('utf-8')).hexdigest() or cmdr.replace(';',':'), rowheader)
|
||||
|
||||
h = open(filename, 'wt') # codecs can't automatically handle line endings, so encode manually where required
|
||||
h.write(header)
|
||||
|
||||
for commodity in data['lastStarport']['commodities']:
|
||||
h.write(('%s;%s;%s;%s;%s;%s;%s;%s;%s;\n' % (
|
||||
rowheader,
|
||||
commodity['name'],
|
||||
commodity['sellPrice'] or '',
|
||||
commodity['buyPrice'] or '',
|
||||
int(commodity['demand']) if commodity['demandBracket'] else '',
|
||||
bracketmap[commodity['demandBracket']],
|
||||
int(commodity['stock']) if commodity['stockBracket'] else '',
|
||||
bracketmap[commodity['stockBracket']],
|
||||
timestamp)).encode('utf-8'))
|
||||
|
||||
h.close()
|
61
commodity.py
Normal file
61
commodity.py
Normal file
@ -0,0 +1,61 @@
|
||||
# Export various CSV formats
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from os.path import join
|
||||
import hashlib
|
||||
import codecs
|
||||
import numbers
|
||||
import time
|
||||
|
||||
from config import config
|
||||
|
||||
bracketmap = { 0: '',
|
||||
1: 'Low',
|
||||
2: 'Med',
|
||||
3: 'High', }
|
||||
|
||||
(COMMODITY_DEFAULT, COMMODITY_BPC, COMMODITY_CSV) = range(3)
|
||||
|
||||
def export(data, kind=COMMODITY_DEFAULT, filename=None):
|
||||
|
||||
querytime = config.getint('querytime') or int(time.time())
|
||||
|
||||
if not filename:
|
||||
filename = join(config.get('outdir'), '%s.%s.%s.%s' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip(), time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)), kind==COMMODITY_BPC and 'bpc' or 'csv'))
|
||||
|
||||
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime))
|
||||
if kind == COMMODITY_CSV:
|
||||
sep = ';'
|
||||
header = sep.join(['System','Station','Commodity','Sell','Buy','Demand','','Supply','','Date','\n'])
|
||||
rowheader = sep.join([data['lastSystem']['name'], data['lastStarport']['name']])
|
||||
elif kind == COMMODITY_BPC:
|
||||
sep = ';'
|
||||
header = sep.join(['userID', 'System','Station','Commodity','Sell','Buy','Demand','','Supply','','Average','Date\n'])
|
||||
cmdr = data['commander']['name'].strip()
|
||||
rowheader = sep.join([(config.getint('anonymous') and hashlib.md5(cmdr.encode('utf-8')).hexdigest()) or (sep in cmdr and '"%s"' % cmdr) or cmdr, data['lastSystem']['name'], data['lastStarport']['name']])
|
||||
else:
|
||||
sep = ','
|
||||
header = sep.join(['System','Station','Commodity','Sell','Buy','Demand','','Supply','','Average','Date\n'])
|
||||
rowheader = sep.join([data['lastSystem']['name'], data['lastStarport']['name']])
|
||||
|
||||
h = open(filename, 'wt') # codecs can't automatically handle line endings, so encode manually where required
|
||||
h.write(header)
|
||||
|
||||
for commodity in data['lastStarport']['commodities']:
|
||||
line = sep.join([
|
||||
rowheader,
|
||||
commodity['name'],
|
||||
commodity['sellPrice'] and str(commodity['sellPrice']) or '',
|
||||
commodity['buyPrice'] and str(commodity['buyPrice']) or '',
|
||||
str(int(commodity['demand'])) if commodity['demandBracket'] else '',
|
||||
bracketmap[commodity['demandBracket']],
|
||||
str(int(commodity['stock'])) if commodity['stockBracket'] else '',
|
||||
bracketmap[commodity['stockBracket']]
|
||||
])
|
||||
if kind==COMMODITY_DEFAULT:
|
||||
line = sep.join([line, str(int(commodity['meanPrice'])), timestamp+'\n'])
|
||||
else:
|
||||
line = sep.join([line, timestamp, '\n'])
|
||||
h.write(line.encode('utf-8'))
|
||||
|
||||
h.close()
|
Loading…
x
Reference in New Issue
Block a user