From 6c254f4bcdd80c117371294812f5eaf4eee6753e Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Wed, 25 Apr 2018 00:42:11 +0100 Subject: [PATCH] Get timestamp for cAPI-derived data from cAPI server Fixes #318 --- EDMarketConnector.py | 12 ------------ L10n/en.template | 9 --------- commodity.py | 5 ++--- companion.py | 2 ++ eddn.py | 22 +++------------------- outfitting.py | 3 +-- shipyard.py | 3 +-- td.py | 4 +--- 8 files changed, 10 insertions(+), 50 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 307887de..18e328d6 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -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: diff --git a/L10n/en.template b/L10n/en.template index 945793b7..4a8e05b5 100644 --- a/L10n/en.template +++ b/L10n/en.template @@ -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"; diff --git a/commodity.py b/commodity.py index 87e4701c..8d4e0094 100644 --- a/commodity.py +++ b/commodity.py @@ -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() diff --git a/companion.py b/companion.py index e7a65cfb..675c5dd9 100644 --- a/companion.py +++ b/companion.py @@ -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() diff --git a/eddn.py b/eddn.py index 538b0c18..2f4eb66a 100644 --- a/eddn.py +++ b/eddn.py @@ -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']), diff --git a/outfitting.py b/outfitting.py index 7a9d7502..df6082cc 100644 --- a/outfitting.py +++ b/outfitting.py @@ -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: diff --git a/shipyard.py b/shipyard.py index fb6b2bf7..108a9b89 100644 --- a/shipyard.py +++ b/shipyard.py @@ -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() diff --git a/td.py b/td.py index 0c3ff3dc..1addc0e0 100644 --- a/td.py +++ b/td.py @@ -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# \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()