-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtradedangerous_listener.py
1214 lines (1048 loc) · 49.8 KB
/
tradedangerous_listener.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from __future__ import generators
import os
import json
import time
import zlib
import zmq
import threading
from datetime import datetime, timedelta
import sqlite3
import csv
import codecs
import sys
# Make things easier for Tromador.
# import ssl
#
# ssl._create_default_https_context = ssl._create_unverified_context
try:
import cache
import trade
import tradedb
import tradeenv
import transfers
import plugins.spansh_plug
except:
from tradedangerous import cli as trade, cache, tradedb, tradeenv, transfers, plugins, commands
from tradedangerous.plugins import spansh_plug
from urllib import request
from calendar import timegm
from pathlib import Path
from collections import defaultdict, namedtuple, deque, OrderedDict
from distutils.version import LooseVersion
_minute = 60
_hour = 3600
# Copyright (C) Oliver 'kfsone' Smith <[email protected]> 2015
#
# Conditional permission to copy, modify, refactor or use this
# code is granted so long as attribution to the original author
# is included.
class MarketPrice(namedtuple('MarketPrice', [
'system',
'station',
'market_id',
'commodities',
'timestamp',
'uploader',
'software',
'version',
])):
pass
class Listener(object):
"""
Provides an object that will listen to the Elite Dangerous Data Network
firehose and capture messages for later consumption.
Rather than individual upates, prices are captured across a window of
between minBatchTime and maxBatchTime. When a new update is received,
Rather than returning individual messages, messages are captured across
a window of potentially several seconds and returned to the caller in
batches.
Attributes:
zmqContext Context this object is associated with,
minBatchTime Allow at least this long for a batch (ms),
maxBatchTime Don't allow a batch to run longer than this (ms),
reconnectTimeout Reconnect the socket after this long with no data,
burstLimit Read a maximum of this many messages between
timer checks
subscriber ZMQ socket we're using
lastRecv time of the last receive (or 0)
"""
uri = 'tcp://eddn.edcd.io:9500'
supportedSchema = 'https://eddn.edcd.io/schemas/commodity/3'
def __init__(
self,
zmqContext = None,
minBatchTime = 36., # seconds
maxBatchTime = 60., # seconds
reconnectTimeout = 30., # seconds
burstLimit = 500,
):
assert burstLimit > 0
if not zmqContext:
zmqContext = zmq.Context()
self.zmqContext = zmqContext
self.subscriber = None
self.minBatchTime = minBatchTime
self.maxBatchTime = maxBatchTime
self.reconnectTimeout = reconnectTimeout
self.burstLimit = burstLimit
self.connect()
def connect(self):
"""
Start a connection
"""
# tear up the new connection first
if self.subscriber:
self.subscriber.close()
del self.subscriber
self.subscriber = newsub = self.zmqContext.socket(zmq.SUB)
newsub.setsockopt(zmq.SUBSCRIBE, b"")
newsub.connect(self.uri)
self.lastRecv = time.time()
self.lastJsData = None
def disconnect(self):
del self.subscriber
def wait_for_data(self, softCutoff, hardCutoff):
"""
Waits for data until maxBatchTime ms has elapsed
or cutoff (absolute time) has been reached.
"""
now = time.time()
cutoff = min(softCutoff, hardCutoff)
if self.lastRecv < now - self.reconnectTimeout:
self.connect()
now = time.time()
nextCutoff = min(now + self.minBatchTime, cutoff)
if now > nextCutoff:
return False
timeout = (nextCutoff - now) * 1000 # milliseconds
# Wait for an event
events = self.subscriber.poll(timeout = timeout)
if events == 0:
return False
return True
def get_batch(self, queue):
"""
Greedily collect deduped prices from the firehose over a
period of between minBatchTime and maxBatchTime, with
built-in auto-reconnection if there is nothing from the
firehose for a period of time.
As json data is decoded, it is stored in self.lastJsData.
Validated market list messages are added to the queue.
"""
while go:
now = time.time()
hardCutoff = now + self.maxBatchTime
softCutoff = now + self.minBatchTime
# hoists
supportedSchema = self.supportedSchema
sub = self.subscriber
# Prices are stored as a dictionary of
# (sys,stn,item) => [MarketPrice]
# The list thing is a trick to save us having to do
# the dictionary lookup twice.
batch = defaultdict(list)
if self.wait_for_data(softCutoff, hardCutoff):
# When wait_for_data returns True, there is some data waiting,
# possibly multiple messages. At this point we can afford to
# suck down whatever is waiting in "nonblocking" mode until
# we reach the burst limit or we get EAGAIN.
bursts = 0
for _ in range(self.burstLimit):
self.lastJsData = None
try:
zdata = sub.recv(flags = zmq.NOBLOCK, copy = False)
except zmq.error.Again:
continue
except zmq.error.ZMQError:
pass
self.lastRecv = time.time()
bursts += 1
try:
jsdata = zlib.decompress(zdata)
except Exception:
continue
bdata = jsdata.decode()
try:
data = json.loads(bdata)
except ValueError:
continue
self.lastJsData = jsdata
try:
schema = data["$schemaRef"]
except KeyError:
continue
if schema != supportedSchema:
continue
try:
header = data["header"]
message = data["message"]
system = message["systemName"].upper()
station = message["stationName"].upper()
market_id = message["marketId"]
commodities = message["commodities"]
timestamp = message["timestamp"]
uploader = header["uploaderID"]
software = header["softwareName"]
swVersion = header["softwareVersion"]
except (KeyError, ValueError):
continue
whitelist_match = list(filter(lambda x: x.get('software').lower() == software.lower(), config['whitelist']))
# Upload software not on whitelist is ignored.
if len(whitelist_match) == 0:
if config['debug'] or config['verbose']:
print(system + "/" + station + " update rejected from client not on whitelist: " + software + " v" + swVersion + "\n")
continue
# Upload software with version less than the defined minimum is ignored.
if whitelist_match[0].get("minversion"):
if LooseVersion(swVersion) < LooseVersion(whitelist_match[0].get("minversion")):
if config['debug']:
print(system + "/" + station + " rejected with:" + software + swVersion + "\n")
continue
# We've received real data.
# Normalize timestamps
timestamp = timestamp.replace("T", " ").replace("+00:00", "")
#Find the culprit!
if '.' in timestamp and config['debug']:
print("Client " + software + ", version " + swVersion + ", uses microseconds.")
for key in header:
if "timestamp" in key:
print(str(key) + ": " + str(header[key]))
for key in message:
if "timestamp" in key:
print(str(key) + ": " + str(message[key]))
# We'll get either an empty list or a list containing
# a MarketPrice. This saves us having to do the expensive
# index operation twice.
oldEntryList = batch[(system, station)]
if oldEntryList:
if oldEntryList[0].timestamp > timestamp:
continue
else:
# Add a blank entry to make the list size > 0
oldEntryList.append(None)
# Here we're replacing the contents of the list.
# This simple array lookup is several hundred times less
# expensive than looking up a potentially large dictionary
# by STATION/SYSTEM:ITEM...
oldEntryList[0] = MarketPrice(
system, station, market_id, commodities,
timestamp,
uploader, software, swVersion,
)
# For the edge-case where we wait 4.999 seconds and then
# get a burst of data: stick around a little longer.
if bursts >= self.burstLimit:
softCutoff = min(softCutoff, time.time() + 0.5)
for entry in batch.values():
queue.append(entry[0])
self.disconnect()
print("Listener reporting shutdown.")
# End of 'kfsone' code.
def db_execute(db, sql_cmd, args = None):
cur = db.cursor()
success = False
result = None
while go and not success:
try:
if args:
result = cur.execute(sql_cmd, args)
else:
result = cur.execute(sql_cmd)
success = True
except sqlite3.OperationalError as e:
if "locked" not in str(e):
success = True
raise sqlite3.OperationalError(e)
else:
print("Database is locked, waiting for access.", end = "\n")
print(f'Error message: {e}')
time.sleep(1)
return result
# We do this because the Listener object must be in the same thread that's running get_batch().
def get_messages():
listener = Listener()
listener.get_batch(q)
def check_update():
"""
Checks for updates to the spansh dump.
"""
global update_busy, dump_busy, process_ack, live_ack, db_name, item_ids, system_ids, station_ids
# Convert the number from the "check_update_every_x_min" setting, which is in minutes,
# into easily readable hours and minutes.
h, m = divmod(config['check_update_every_x_min'], 60)
next_check = ""
if h > 0:
next_check = str(h) + " hour"
if h > 1:
next_check += "s"
if m > 0:
next_check += ", "
if m > 0:
next_check += str(m) + " minute"
if m > 1:
next_check += "s"
now = round(time.time(), 0) - config['check_update_every_x_min']
dumpModded = 0
localModded = 0
# # The following values only need to be assigned once, no need to be in the while loop.
# BASE_URL = plugins.eddblink_plug.BASE_URL
# LISTINGS = "listings.csv"
# listings_path = Path(LISTINGS)
Months = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12}
SOURCE_URL = 'https://downloads.spansh.co.uk/galaxy_stations.json'
# request.urlopen(BASE_URL + LISTINGS)
# url = BASE_URL + LISTINGS
startup = True
while go:
# Trigger daily EDDB update if the dumps have updated since last run.
# Otherwise, go to sleep for {config['check_update_every_x_min']} minutes before checking again.
if time.time() >= now + (config['check_update_every_x_min'] * _minute) or startup:
startup = False
response = 0
tryLeft = 10
while tryLeft != 0:
try:
response = request.urlopen(SOURCE_URL)
tryLeft = 0
except:
tryLeft -= 1
if not response:
print("Error attempting to check for update, no response from server.")
continue
url_time = request.urlopen(SOURCE_URL).getheader("Last-Modified")
# dumpModded = datetime.strptime(url_time, "%a, %d %b %Y %H:%M:%S %Z").timestamp()
# # Now that we have the Unix epoch time of the dump file, get the same from the local file.
# if Path.exists(eddbPath / listings_path):
# localModded = (eddbPath / listings_path).stat().st_mtime
#
#
# if localModded < dumpModded:
last_modified = datetime.strptime(url_time, "%a, %d %b %Y %H:%M:%S %Z").timestamp()
if not config['last_update'] or config['last_update'] < last_modified:
maxage=((datetime.now() - datetime.fromtimestamp(config["last_update"])) + timedelta(hours=1))/timedelta(1)
options = '-'
if config['debug']:
options += 'w'
if config['verbose']:
options += 'vvv'
if options == '-':
options = ''
# TD will fail with an error if the database is in use while it's trying
# to do its thing, so we need to make sure that neither of the database
# editing methods are doing anything before running.
update_busy = True
print("Spansh update available, waiting for busy signal acknowledgement before proceeding.")
while not (process_ack and live_ack):
rep = 0
if config['debug']:
print("Still waiting for acknowledgment. (" + str(rep) + ")", end = '\r')
rep = rep + 1
time.sleep(1)
print("Busy signal acknowledged, performing update.")
try:
trade.main(('trade.py', 'import', '-P', 'spansh', '-O', f'url={SOURCE_URL},maxage={maxage}', options))
trade.main(('trade.py', 'export', '--path', f'{config["export_path"]}'))
if config['side'] == 'server':
trade.main(('trade.py', 'import', '-P', 'eddblink', '-O', 'prices'))
if config['debug']:
print("Updating dictionaries...")
# Since there's been an update, we need to redo all this.
db_name, item_ids, system_ids, station_ids = update_dicts()
config['last_update'] = last_modified
with open("tradedangerous-listener-config.json", "w") as config_file:
json.dump(config, config_file, indent = 4)
now = round(time.time(), 0)
except Exception as e:
print("Error when running update:")
print(e)
update_busy = False
continue
print("Update complete, turning off busy signal.")
if config['side'] == 'server':
dump_busy = True
update_busy = False
if config['side'] == 'server':
export_dump()
else:
print("No update, checking again in " + next_check + ".")
now = round(time.time(), 0)
if config['debug'] and ((round(time.time(), 0) - now) % 3600 == 0):
print("Update checker is sleeping: " + str(int(now + (config['check_update_every_x_min'] * _minute) - round(time.time(), 0)) / 60) + " minutes remain until next check.")
time.sleep(1)
#If not go:
print("Update checker reporting shutdown.")
def load_config():
"""
Loads the settings from 'tradedangerous-listener-configuration.json'.
If the config_file does not exist or is missing any settings,
the default will be used for any missing setting,
and the config_file will be updated to include all settings,
preserving the existing (if any) settings' current values.
"""
write_config = False
# Initialize config with default settings.
# NOTE: Whitespace added for readability.
config = OrderedDict([ \
('side', 'client'), \
('verbose', True), \
('debug', False), \
('last_update', 0), \
('check_update_every_x_min', 60), \
('export_live_every_x_min', 5), \
('export_dump_every_x_hour', 24), \
('db_maint_every_x_hour', 12), \
('export_path', './data/eddb'), \
('whitelist', \
[ \
OrderedDict([ ('software', 'E:D Market Connector [Windows]') ]),\
OrderedDict([ ('software', 'E:D Market Connector [Mac OS]') ]),\
OrderedDict([ ('software', 'E:D Market Connector [Linux]') ]),\
OrderedDict([ ('software', 'EDDiscovery') ]) \
] \
) \
])
# Load the settings from the configuration file if it exists.
if Path.exists(Path("tradedangerous-listener-config.json")):
with open("tradedangerous-listener-config.json", "r") as fh:
try:
temp = json.load(fh, object_pairs_hook = OrderedDict)
# For each setting in config,
# if file setting exists and isn't the default,
# overwrite config setting with file setting.
for setting in config:
if setting in temp:
if config[setting] != temp[setting]:
config[setting] = temp[setting]
else:
# If any settings don't exist in the config_file, need to update the file.
write_config = True
except:
# If, for some reason, there's an error trying to load
# the config_file, treat it as if it doesn't exist.
write_config = True
else:
# If the config_file doesn't exist, need to make it.
write_config = True
# Write the current configuration to the file, if needed.
if write_config:
with open("tradedangerous-listener-config.json", "w") as config_file:
json.dump(config, config_file, indent = 4)
# We now have a config that has valid values for all the settings, and a
# matching config_file so the settings are preserved for the next run.
return config
def validate_config():
"""
Checks to make sure the loaded config contains valid values.
If it finds any invalid, it marks that as such in the config_file
so the default value is used on reload, and then reloads the config.
"""
global config
valid = True
with open("tradedangerous-listener-config.json", "r") as fh:
config_file = fh.read()
# For each of these settings, if the value is invalid, mark the key.
# 'side' == 'client' || 'server'
config['side'] = config['side'].lower()
if config['side'] != 'server' and config['side'] != 'client':
valid = False
config_file = config_file.replace('"side"', '"side_invalid"')
# 'verbose' == True || False
if not isinstance(config["verbose"], bool):
valid = False
config_file = config_file.replace('"verbose"', '"verbose_invalid"')
# 'debug' == True || False
if not isinstance(config["debug"], bool):
valid = False
config_file = config_file.replace('"debug"', '"debug_invalid"')
#The spansh import plugin doesn't need any options specified.
if config.get('plugin_options'):
valid = False
config_file = config_file.replace('"plugin_options"', '"plugin_options_invalid"')
# # 'plugin_options': For this one, rather than completely replace invalid
# # values with the default, check to see if any of the values are valid
# # and keep those, prepending the default values to the setting if they
# # aren't already in the setting.
#
# if isinstance(config['plugin_options'], str):
# options = config['plugin_options'].split(',')
# valid_options = ""
# cmdenv = commands.CommandIndex().parse
# plugin_options = plugins.load(cmdenv(['trade', 'import', '--plug', 'eddblink', '-O', 'help']).plug, "ImportPlugin").pluginOptions.keys()
#
# for option in options:
# if option in plugin_options:
# if valid_options != "":
# valid_options += ","
# valid_options += option
# else:
# valid = False
#
# if not valid:
# if valid_options.find("force") == -1:
# valid_options = "force," + valid_options
# if valid_options.find("skipvend") == -1:
# valid_options = "skipvend," + valid_options
# if valid_options.find("all") == -1:
# valid_options = "all," + valid_options
# config_file = config_file.replace(config['plugin_options'], valid_options)
# else:
# valid = False
# config_file = config_file.replace('"plugin_options"', '"plugin_options_invalid"')
# 'check_update_every_x_min' >= 1 && <= 1440 (1 day)
if isinstance(config['check_update_every_x_min'], int):
if config['check_update_every_x_min'] < 1 or config['check_update_every_x_min'] > 1440:
valid = False
config_file = config_file.replace('"check_update_every_x_min"', '"check_update_every_x_min_invalid"')
else:
valid = False
config_file = config_file.replace('"check_update_every_x_min"', '"check_update_every_x_min_invalid"')
# 'export_dump_every_x_hour' >= 1 && <= 24 (1 day)
if isinstance(config['export_dump_every_x_hour'], int):
if config['export_dump_every_x_hour'] < 1 or config['export_dump_every_x_hour'] > 24:
valid = False
config_file = config_file.replace('"export_dump_every_x_hour"', '"export_dump_every_x_hour_invalid"')
else:
valid = False
config_file = config_file.replace('"export_dump_every_x_hour"', '"export_dump_every_x_hour_invalid"')
# 'export_live_every_x_min' >= 1 && <= 720 (12 hours)
if isinstance(config['export_live_every_x_min'], int):
if config['export_live_every_x_min'] < 1 or config['export_live_every_x_min'] > 720:
valid = False
config_file = config_file.replace('"export_live_every_x_min"', '"export_live_every_x_min_invalid"')
else:
valid = False
config_file = config_file.replace('"export_live_every_x_min"', '"export_live_every_x_min_invalid"')
# 'db_maint_every_x_hour' >=1 && <= 240 (10 days)
if isinstance(config['db_maint_every_x_hour'], (int, float)):
if config['db_maint_every_x_hour'] < 1 or config['db_maint_every_x_hour'] > 240:
valid = False
config_file = config_file.replace('"db_maint_every_x_hour"', '"db_maint_every_x_hour_invalid"')
else:
valid = False
config_file = config_file.replace('"db_maint_every_x_hour"', '"db_maint_every_x_hour_invalid"')
# 'export_path': location (absolute or relative) of folder to save the exported listings files
# (Listings export only performed when 'side' == 'server')
if not Path.exists(Path(config['export_path'])):
valid = False
config_file = config_file.replace('"export_path"', '"export_path_invalid"')
if not valid:
# Before we reload the config to set the invalid values back to default,
# we need to write the changes we made to the file.
with open("tradedangerous-listener-config.json", "w") as fh:
fh.write(config_file)
config = load_config()
def process_messages():
global process_ack, update_busy, dump_busy, live_busy, db_name, item_ids, system_ids, station_ids
tdb = tradedb.TradeDB(load = False)
db = tdb.getDB()
# Place the database into autocommit mode to avoid issues with
# sqlite3 doing automatic transactions.
db.isolation_level = None
curs = db.cursor()
# same SQL every time
deleteStationItemEntry = "DELETE FROM StationItem WHERE station_id = ?"
insertStationItemEntry = (
"INSERT OR IGNORE INTO StationItem("
" station_id, item_id, modified,"
" demand_price, demand_units, demand_level,"
" supply_price, supply_units, supply_level, from_live)"
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)"
)
updateItemAveragePrice = "UPDATE Item SET avg_price = ? WHERE item_id = ?"
getOldStationInfo = (
"SELECT name, ls_from_star,blackmarket, max_pad_size, "
"market, shipyard, outfitting, rearm, refuel, repair, "
"planetary, type_id from Station WHERE station_id = ?"
)
insertNewStation = (
"INSERT OR IGNORE INTO Station("
" station_id, name, system_id, ls_from_star,"
" blackmarket, max_pad_size, market, shipyard,"
" modified, outfitting, rearm, refuel, repair,"
" planetary, type_id)"
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
)
removeOldStation = "DELETE FROM Station WHERE station_id = ?"
moveStationToNewSystem = "UPDATE Station SET system_id = ? WHERE station_id = ?"
# We want to perform some automatic DB maintenance when running for long periods.
maintenance_time = time.time() + (config['db_maint_every_x_hour'] * _hour)
while go:
# We don't want the threads interfering with each other,
# so pause this one if either the update checker or
# listings exporter report that they're active.
if update_busy or dump_busy or live_busy:
print("Message processor acknowledging busy signal.")
process_ack = True
while (update_busy or dump_busy or live_busy) and go:
time.sleep(1)
# Just in case we caught the shutdown command while waiting.
if not go:
break
process_ack = False
print("Busy signal off, message processor resuming.")
if time.time() >= maintenance_time:
print("Performing server maintenance tasks." + str(datetime.now()))
try:
db_execute(db, "VACUUM")
db_execute(db, "PRAGMA optimize")
print("Server maintenance tasks completed. " + str(datetime.now()))
except Exception as e:
print("Error performing maintenance:")
print("-----------------------------")
print(e)
print("-----------------------------")
maintenance_time = time.time() + (config['db_maint_every_x_hour'] * _hour)
# Either get the first message in the queue,
# or go to sleep and wait if there aren't any.
try:
entry = q.popleft()
except IndexError:
time.sleep(1)
continue
# Get the station_id using the system and station names.
system = entry.system.upper()
station = entry.station.upper()
market_id = entry.market_id
modified = entry.timestamp.replace('T', ' ').replace('Z', '')
commodities = entry.commodities
#All the stations should be stored using the market_id.
success = False
while not success:
try:
exists = curs.execute("SELECT station_id FROM Station WHERE station_id = ?", (market_id,)).fetchone()
success = True
except sqlite3.OperationalError:
print("Database is locked, waiting for access.", end = "\n")
time.sleep(1)
if not exists:
station_id = station_ids.get(system + "/" + station)
system_id = system_ids.get(system)
if not station_id:
# Mobile stations are stored in the dict a bit differently.
station_id = station_ids.get("MEGASHIP/" + station)
if station_id and system_id:
if config['verbose']:
print("Megaship station, updating system to " + system)
# Update the system the station is in, in case it has changed.
success = False
while not success:
try:
curs.execute("BEGIN IMMEDIATE")
curs.execute(moveStationToNewSystem, (system_id, station_id))
db.commit()
success = True
except sqlite3.IntegrityError:
if config['verbose']:
print("ERROR: Not found in Systems: " + system + "/" + station)
continue
except sqlite3.OperationalError:
print("Database is locked, waiting for access.", end = "\n")
time.sleep(1)
else:
# If we can't find it by any of these means, it must be a 'new' station.
if config['verbose']:
print("Not found in Stations: " + system + "/" + station + ", inserting into DB.")
# Add the new Station with '?' for all unknowns.
success = False
while not success:
try:
curs.execute("BEGIN IMMEDIATE")
curs.execute(insertNewStation, (market_id, station, system_id, 1,
'?', '?', 'Y', '?', modified, '?',
'?', '?', '?', '?', 0))
db.commit()
success = True
except sqlite3.IntegrityError as e:
if config['verbose']:
print(e)
continue
except sqlite3.OperationalError:
print("Database is locked, waiting for access.", end = "\n")
time.sleep(1)
continue
station_ids[system + "/" + station] = market_id
if station_id and (station_id != market_id):
success = False
while not success:
try:
curs.execute("BEGIN IMMEDIATE")
result = curs.execute(getOldStationInfo, (station_id,))
nm, ls, bm, mps, mk, sy, of, ra, rf, rp, pl, ti = result.fetchone()
curs.execute(insertNewStation, (market_id, nm, system_id, ls, bm,
mps, mk, sy, modified,
of, ra, rf, rp, pl, ti))
curs.execute(removeOldStation, (station_id,))
db.commit()
success = True
except sqlite3.IntegrityError as e:
if config['verbose']:
print(e)
continue
except sqlite3.OperationalError as e:
print("Database is locked, waiting for access.", end = "\n")
print(e)
time.sleep(1)
station_id = market_id
start_update = datetime.now()
itemList = []
avgList = []
for commodity in commodities:
if commodity['sellPrice'] == 0 and commodity['buyPrice'] == 0:
# Skip blank entries
continue
# Get fdev_id using commodity name from message.
item_edid = db_name.get(commodity['name'].lower())
if not item_edid:
if config['verbose']:
print("Ignoring item: " + commodity['name'])
continue
# Some items, mostly recently added items, are found in db_name but not in item_ids
# (This is entirely EDDB.io's fault.)
item_id = item_ids.get(item_edid)
if not item_id:
item_id = int(item_edid)
itemList.append((
station_id, item_id, modified,
commodity['sellPrice'], commodity['demand'],
commodity['demandBracket'] if commodity['demandBracket'] != '' else -1,
commodity['buyPrice'], commodity['stock'],
commodity['stockBracket'] if commodity['stockBracket'] != '' else -1,
))
# We only "need" to update the avg_price for the few items not included in
# EDDB.io's API, but might as well do it for all of them.
avgList.append((commodity['meanPrice'], item_id))
success = False
while not success:
try:
curs.execute("BEGIN IMMEDIATE")
success = True
except sqlite3.OperationalError:
print("Database is locked, waiting for access.", end = "\n")
time.sleep(1)
curs.execute(deleteStationItemEntry, (station_id,))
for item in itemList:
try:
curs.execute(insertStationItemEntry, item)
except Exception as e:
if config['debug']:
print("Error '" + str(e) + "' when inserting item:\n\t(Not in DB's Item table?) fdev_id: " + str(item[1]))
for avg in avgList:
try:
curs.execute(updateItemAveragePrice, avg)
except Exception as e:
if config['debug']:
print("Error '" + str(e) + "' when inserting average:\n" + str(avg))
success = False
while not success:
try:
db.commit()
success = True
except sqlite3.OperationalError:
print("Database is locked, waiting for access.", end = "\n")
time.sleep(1)
if config['verbose']:
print("Updated " + system + "/" + station + ", station_id:'" + str(station_id) + "', from "+ entry.software + " v" + entry.version)
else:
print("Updated " + system + "/" + station)
print("Message processor reporting shutdown.")
def fetchIter(cursor, arraysize = 1000):
"""
An iterator that uses fetchmany to keep memory usage down
and speed up the time to retrieve the results dramatically.
"""
while True:
results = cursor.fetchmany(arraysize)
if not results:
break
for result in results:
yield result
def export_live():
"""
Creates a "listings-live.csv" file in "export_path" every X seconds,
as defined in the configuration file.
Only runs when program configured as server.
"""
global live_ack, live_busy, process_ack, dump_busy, update_busy
tdb = tradedb.TradeDB(load = False)
db = tdb.getDB()
listings_file = (Path(config['export_path']).resolve() / Path("listings-live.csv"))
listings_tmp = listings_file.with_suffix(".tmp")
print("Live listings will be exported to: \n\t" + str(listings_file))
now = time.time()
while go:
# Wait until the time specified in the "export_live_every_x_min" config
# before doing an export, watch for busy signal or shutdown signal
# while waiting.
while time.time() < now + (config['export_live_every_x_min'] * _minute):
if not go:
break
if dump_busy or update_busy:
print("Live listings exporter acknowledging busy signal.")
live_ack = True
while (dump_busy or update_busy) and go:
time.sleep(1)
# Just in case we caught the shutdown command while waiting.
if not go:
break
live_ack = False
print("Busy signal off, live listings exporter resuming.")
now = time.time()
time.sleep(1)
# We may be here because we broke out of the waiting loop,
# so we need to see if we lost go and quit the main loop if so.
if not go:
break
start = datetime.now()
print("Live listings exporter sending busy signal. " + str(start))
live_busy = True
# We don't need to wait for acknowledgement from the dump exporter,
# because it waits for one from this, and this won't acknowledge
# until it's finished exporting.
while not (process_ack):
if not go:
break
print("Busy signal acknowledged, getting live listings for export.")
try:
cursor = fetchIter(db_execute(db, "SELECT * FROM StationItem WHERE from_live = 1 ORDER BY station_id, item_id"))
# results = list(cursor)
except sqlite3.DatabaseError as e:
print(e)
live_busy = False
continue
except AttributeError as e:
print("Got Attribute error trying to fetch StationItems: " + str(e))
print(cursor)
live_busy = False
continue
print("Exporting 'listings-live.csv'. (Got listings in " + str(datetime.now() - start) + ")")
with open(str(listings_tmp), "w") as f:
f.write("id,station_id,commodity_id,supply,supply_bracket,buy_price,sell_price,demand,demand_bracket,collected_at\n")
lineNo = 1
for result in cursor:
# If we lose go during export, we need to abort.
if not go:
break
station_id = str(result[0])
commodity_id = str(result[1])
sell_price = str(result[2])
demand = str(result[3])
demand_bracket = str(result[4])
buy_price = str(result[5])
supply = str(result[6])
supply_bracket = str(result[7])
collected_at = str(timegm(datetime.strptime(result[8].split('.')[0], '%Y-%m-%d %H:%M:%S').timetuple()))
listing = station_id + "," + commodity_id + "," \
+supply + "," + supply_bracket + "," + buy_price + "," \
+sell_price + "," + demand + "," + demand_bracket + "," \
+collected_at
f.write(str(lineNo) + "," + listing + "\n")
lineNo += 1
# del results
if config['verbose']:
print('Live listings exporter finished with database, releasing lock.')
live_busy = False
# If we aborted the export because we lost go, listings_tmp is broken and useless, so delete it.
if not go:
listings_tmp.unlink()
print("Export aborted, received shutdown signal.")
break
while listings_file.exists():
try:
listings_file.unlink()
except:
time.sleep(1)
listings_tmp.rename(listings_file)
print("Export completed in " + str(datetime.now() - start))
now = time.time()
print("Live listings exporter reporting shutdown.")
def export_dump():
"""
Creates a "listings.csv" file in "export_path" every X seconds,
as defined in the configuration file.
Only runs when program configured as server.
"""
global dump_busy, process_ack, live_ack