1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-08 19:32:15 +03:00
This commit is contained in:
Jonathan Harris 2017-12-17 19:21:56 +00:00
parent b28355d182
commit 105f44e6ca

View File

@ -25,19 +25,19 @@ import plug
if __debug__: if __debug__:
from traceback import print_exc from traceback import print_exc
EDSM_POLL = 0.1 EDSM_POLL = 0.1
_TIMEOUT = 20 _TIMEOUT = 20
FAKE = ['CQC', 'Training', 'Destination'] # Fake systems that shouldn't be sent to EDSM FAKE = ['CQC', 'Training', 'Destination'] # Fake systems that shouldn't be sent to EDSM
this = sys.modules[__name__] # For holding module globals this = sys.modules[__name__] # For holding module globals
this.session = requests.Session() this.session = requests.Session()
this.queue = Queue() # Items to be sent to EDSM by worker thread this.queue = Queue() # Items to be sent to EDSM by worker thread
this.discardedEvents = [] # List discarded events from EDSM this.discardedEvents = [] # List discarded events from EDSM
this.lastlookup = False # whether the last lookup succeeded this.lastlookup = False # whether the last lookup succeeded
# Game state # Game state
this.multicrew = False # don't send captain's ship info to EDSM while on a crew this.multicrew = False # don't send captain's ship info to EDSM while on a crew
def plugin_start(): def plugin_start():
# Can't be earlier since can only call PhotoImage after window is created # Can't be earlier since can only call PhotoImage after window is created
@ -66,9 +66,8 @@ def plugin_start():
this.thread = Thread(target = worker, name = 'EDSM worker') this.thread = Thread(target = worker, name = 'EDSM worker')
this.thread.daemon = True this.thread.daemon = True
this.thread.start() this.thread.start()
# Get the last Discarded events from EDSM # Get the last Discarded events from EDSM
if __debug__: print('Calling Discarded Events API endpoint')
this.queue.put(('https://www.edsm.net/api-journal-v1/discard', {}, discarded_callback)) this.queue.put(('https://www.edsm.net/api-journal-v1/discard', {}, discarded_callback))
return 'EDSM' return 'EDSM'
@ -196,7 +195,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
this.system.update_idletasks() this.system.update_idletasks()
this.multicrew = bool(state['Role']) this.multicrew = bool(state['Role'])
# If Discarded events still emtpy, retry # If Discarded events still emtpy, retry
if not this.discardedEvents: if not this.discardedEvents:
this.queue.put(('https://www.edsm.net/api-journal-v1/discard', {}, discarded_callback)) this.queue.put(('https://www.edsm.net/api-journal-v1/discard', {}, discarded_callback))
@ -242,11 +241,10 @@ def worker():
retrying = 0 retrying = 0
while retrying < 3: while retrying < 3:
try: try:
#if __debug__: print(data)
r = this.session.post(url, data=data, timeout=_TIMEOUT) r = this.session.post(url, data=data, timeout=_TIMEOUT)
r.raise_for_status() r.raise_for_status()
reply = r.json() reply = r.json()
if callback: if callback:
callback(reply) callback(reply)
else: else:
@ -267,14 +265,12 @@ def worker():
# Queue a call to the EDSM journal API with args (which should be quoted) # Queue a call to the EDSM journal API with args (which should be quoted)
def call(cmdr, args, callback=None): def call(cmdr, args, callback=None):
(username, apikey) = credentials(cmdr) (username, apikey) = credentials(cmdr)
args = dict(args)
args = dict(args) args['commanderName'] = username
args['commanderName'] = username args['apiKey'] = apikey
args['apiKey'] = apikey args['fromSoftware'] = applongname
args['fromSoftware'] = applongname args['fromSoftwareVersion'] = appversion
args['fromSoftwareVersion'] = appversion
this.queue.put(('https://www.edsm.net/api-journal-v1', args, callback)) this.queue.put(('https://www.edsm.net/api-journal-v1', args, callback))
@ -282,28 +278,27 @@ def call(cmdr, args, callback=None):
def sendEntry(cmdr, system, station, entry, state): def sendEntry(cmdr, system, station, entry, state):
if entry['event'] in this.discardedEvents: if entry['event'] in this.discardedEvents:
return return
if entry['event'] in ['Location', 'FSDJump'] and entry['StarSystem'] in FAKE: if entry['event'] in ['Location', 'FSDJump'] and entry['StarSystem'] in FAKE:
return return
# Update callback on needed events # Update callback on needed events
if entry['event'] in ['Location', 'FSDJump']: if entry['event'] in ['Location', 'FSDJump']:
eventCallback = writelog_callback eventCallback = writelog_callback
else: else:
eventCallback = null_callback eventCallback = null_callback
# Introduce transient states into the event # Introduce transient states into the event
entry['_systemName'] = system; entry['_systemName'] = system
#entry['_systemCoordinates'] = gameStatus.coordinates; #TODO: Track system coordinates #entry['_systemCoordinates'] = gameStatus.coordinates #TODO: Track system coordinates
entry['_stationName'] = station; entry['_stationName'] = station
entry['_shipId'] = state['ShipID']; entry['_shipId'] = state['ShipID']
# Make the API call # Make the API call
call(cmdr, { 'message': json.dumps(entry) }, eventCallback) call(cmdr, { 'message': json.dumps(entry) }, eventCallback)
def writelog_callback(reply): def writelog_callback(reply):
#if __debug__: print(reply)
this.lastlookup = reply['events'][0] # Get first response while we send events one by one this.lastlookup = reply['events'][0] # Get first response while we send events one by one
this.system.event_generate('<<EDSMStatus>>', when="tail") # calls update_status in main thread this.system.event_generate('<<EDSMStatus>>', when="tail") # calls update_status in main thread
@ -325,11 +320,11 @@ def update_status(event=None):
# When we don't care about return msgnum from EDSM # When we don't care about return msgnum from EDSM
def null_callback(reply): def null_callback(reply):
if not reply: if not reply:
plug.show_error(_("Error: Can't connect to EDSM"))# When we don't care about return msgnum from EDSM plug.show_error(_("Error: Can't connect to EDSM"))
# Grab the discarded list # Grab the discarded list
def discarded_callback(reply): def discarded_callback(reply):
if not reply: if not reply:
plug.show_error(_("Error: Can't get EDSM DIscarded Events")) plug.show_error(_("Error: Can't connect to EDSM"))
else: else:
this.discardedEvents = reply this.discardedEvents = reply