1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-08 05:20:03 +03:00

Get timestamp for cAPI-derived data from cAPI server

Fixes #318
This commit is contained in:
Jonathan Harris 2018-04-25 00:42:11 +01:00
parent 4d46bb5af5
commit 6c254f4bcd
8 changed files with 10 additions and 50 deletions

View File

@ -304,18 +304,6 @@ class AppWindow:
config.delete('password')
config.delete('logdir')
# Check system time
drift = abs(time() - self.eddn.time())
if drift > DRIFT_THRESHOLD:
tkMessageBox.showerror(applongname,
_('This app requires accurate timestamps.') + '\n' + # Error message shown if system time is wrong
(TZ_THRESHOLD < drift < CLOCK_THRESHOLD and
_("Check your system's Time Zone setting.") or # Error message shown if system time is wrong
_("Check your system's Date and Time settings.")), # Error message shown if system time is wrong
parent = self.w)
self.w.destroy()
return
self.postprefs(False) # Companion login happens in callback from monitor
if keyring.get_keyring().priority < 1:

View File

@ -52,12 +52,6 @@
/* Menu item. [EDMarketConnector.py] */
"Check for Updates..." = "Check for Updates...";
/* Error message shown if system time is wrong. [EDMarketConnector.py] */
"Check your system's Date and Time settings." = "Check your system's Date and Time settings.";
/* Error message shown if system time is wrong. [EDMarketConnector.py] */
"Check your system's Time Zone setting." = "Check your system's Time Zone setting.";
/* Federation rank. [stats.py] */
"Chief Petty Officer" = "Chief Petty Officer";
@ -493,9 +487,6 @@
/* Appearance setting. [prefs.py] */
"Theme" = "Theme";
/* Error message shown if system time is wrong. [EDMarketConnector.py] */
"This app requires accurate timestamps." = "This app requires accurate timestamps.";
/* Help text in settings. [prefs.py] */
"Tip: You can disable a plugin by{CR}adding '{EXT}' to its folder name" = "Tip: You can disable a plugin by{CR}adding '{EXT}' to its folder name";

View File

@ -23,7 +23,6 @@ def export(data, kind=COMMODITY_DEFAULT, filename=None):
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'])
@ -53,9 +52,9 @@ def export(data, kind=COMMODITY_DEFAULT, filename=None):
bracketmap[commodity['stockBracket']]
])
if kind==COMMODITY_DEFAULT:
line = sep.join([line, str(int(commodity['meanPrice'])), str(commodity['id']), timestamp+'\n'])
line = sep.join([line, str(int(commodity['meanPrice'])), str(commodity['id']), data['timestamp'] + '\n'])
else:
line = sep.join([line, timestamp, '\n'])
line = sep.join([line, data['timestamp'] + '\n'])
h.write(line.encode('utf-8'))
h.close()

View File

@ -244,6 +244,8 @@ class Session:
try:
data = r.json()
if 'timestamp' not in data:
data['timestamp'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.strptime(r.headers['Date'].split(',')[1].strip(), "%d %b %Y %H:%M:%S %Z"))
except:
self.dump(r)
raise ServerError()

22
eddn.py
View File

@ -10,7 +10,6 @@ import re
import requests
from sys import platform
import time
from calendar import timegm
import uuid
if platform != 'win32':
@ -33,7 +32,6 @@ class EDDN:
### SERVER = 'http://localhost:8081' # testing
SERVER = 'https://eddn.edcd.io:4430'
UPLOAD = '%s/upload/' % SERVER
HEALTH = '%s/health_check/' % SERVER
REPLAYPERIOD = 400 # Roughly two messages per second, accounting for send delays [ms]
REPLAYFLUSH = 20 # Update log on disk roughly every 10 seconds
@ -79,17 +77,6 @@ class EDDN:
replayfile.close()
replayfile = None
def time(self):
# Returns the EDDN gateway's idea of time-of-day.
# Assumes that the gateway returns a strictly compliant Date - https://tools.ietf.org/html/rfc7231#section-7.1.1.1
try:
r = self.session.get(self.HEALTH, timeout=timeout)
return timegm(time.strptime(r.headers['Date'].split(',')[1].strip(), "%d %b %Y %H:%M:%S GMT"))
except:
# On any error assume that we're good
if __debug__: print_exc()
return time.time()
def send(self, cmdr, msg):
if config.getint('anonymous'):
uploaderID = config.get('uploaderID')
@ -179,8 +166,7 @@ class EDDN:
# Don't send empty commodities list - schema won't allow it
if commodities:
message = OrderedDict([
('timestamp', time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(config.getint('querytime') or int(time.time())))),
('timestamp', data['timestamp']),
('systemName', data['lastSystem']['name']),
('stationName', data['lastStarport']['name']),
('marketId', data['lastStarport']['id']),
@ -201,8 +187,7 @@ class EDDN:
self.send(data['commander']['name'], {
'$schemaRef' : 'https://eddn.edcd.io/schemas/outfitting/2' + (is_beta and '/test' or ''),
'message' : OrderedDict([
('timestamp', time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(config.getint('querytime') or int(time.time())))),
('timestamp', data['timestamp']),
('systemName', data['lastSystem']['name']),
('stationName', data['lastStarport']['name']),
('marketId', data['lastStarport']['id']),
@ -216,8 +201,7 @@ class EDDN:
self.send(data['commander']['name'], {
'$schemaRef' : 'https://eddn.edcd.io/schemas/shipyard/2' + (is_beta and '/test' or ''),
'message' : OrderedDict([
('timestamp', time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(config.getint('querytime') or int(time.time())))),
('timestamp', data['timestamp']),
('systemName', data['lastSystem']['name']),
('stationName', data['lastStarport']['name']),
('marketId', data['lastStarport']['id']),

View File

@ -456,7 +456,6 @@ def export(data, filename):
assert data['lastSystem'].get('name')
assert data['lastStarport'].get('name')
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime))
header = 'System,Station,Category,Name,Mount,Guidance,Ship,Class,Rating,FDevID,Date\n'
rowheader = '%s,%s' % (data['lastSystem']['name'], data['lastStarport']['name'])
@ -466,7 +465,7 @@ def export(data, filename):
try:
m = lookup(v, companion.ship_map)
if m:
h.write('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n' % (rowheader, m['category'], m['name'], m.get('mount',''), m.get('guidance',''), m.get('ship',''), m['class'], m['rating'], m['id'], timestamp))
h.write('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n' % (rowheader, m['category'], m['name'], m.get('mount',''), m.get('guidance',''), m.get('ship',''), m['class'], m['rating'], m['id'], data['timestamp']))
except AssertionError as e:
if __debug__: print 'Outfitting: %s' % e # Silently skip unrecognized modules
except:

View File

@ -14,12 +14,11 @@ def export(data, filename):
assert data['lastStarport'].get('name')
assert data['lastStarport'].get('ships')
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime))
header = 'System,Station,Ship,FDevID,Date\n'
rowheader = '%s,%s' % (data['lastSystem']['name'], data['lastStarport']['name'])
h = open(filename, 'wt')
h.write(header)
for (name,fdevid) in [(ship_map.get(ship['name'].lower(), ship['name']), ship['id']) for ship in (data['lastStarport']['ships'].get('shipyard_list') or {}).values() + data['lastStarport']['ships'].get('unavailable_list')]:
h.write('%s,%s,%s,%s\n' % (rowheader, name, fdevid, timestamp))
h.write('%s,%s,%s,%s\n' % (rowheader, name, fdevid, data['timestamp']))
h.close()

4
td.py
View File

@ -26,8 +26,6 @@ def export(data):
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.gmtime(querytime))
# 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'))
@ -49,6 +47,6 @@ def export(data):
demandbracketmap[commodity['demandBracket']],
int(commodity['stock']) if commodity['stockBracket'] else '',
stockbracketmap[commodity['stockBracket']],
timestamp))
data['timestamp']))
h.close()