From 274c438c264674dbc2373dd24440d867d238d07a Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 21 Aug 2022 01:51:12 +0100 Subject: [PATCH] Extra checks on JSON files downloaded --- hblink.py | 83 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 27 deletions(-) diff --git a/hblink.py b/hblink.py index a846bd4..0b1a66c 100755 --- a/hblink.py +++ b/hblink.py @@ -1389,12 +1389,16 @@ def try_download(_path, _file, _url, _stale,): except IOError: result = 'ID ALIAS MAPPER: \'{}\' could not be downloaded due to an IOError'.format(_file) else: - try: - with open(_path+_file, 'wb') as outfile: - outfile.write(data) - outfile.close() - except IOError: - result = 'ID ALIAS mapper \'{}\' file could not be written due to an IOError'.format(_file) + if data and (data != b'{}'): + try: + with open(_path+_file, 'wb') as outfile: + outfile.write(data) + outfile.close() + except IOError: + result = 'ID ALIAS mapper \'{}\' file could not be written due to an IOError'.format(_file) + else: + result = 'ID ALIAS mapper \'{}\' file not written because downloaded data is empty for some reason'.format(_file) + else: result = 'ID ALIAS MAPPER: \'{}\' is current, not downloaded'.format(_file) @@ -1417,6 +1421,11 @@ def mk_server_dict(path,filename): # ID ALIAS CREATION # Download def mk_aliases(_config): + peer_ids = {} + subscriber_ids = {} + local_subscriber_ids = {} + talkgroup_ids = {} + server_ids = {} if _config['ALIASES']['TRY_DOWNLOAD'] == True: # Try updating peer aliases file result = try_download(_config['ALIASES']['PATH'], _config['ALIASES']['PEER_FILE'], _config['ALIASES']['PEER_URL'], _config['ALIASES']['STALE_TIME']) @@ -1431,30 +1440,50 @@ def mk_aliases(_config): result = try_download(_config['ALIASES']['PATH'], _config['ALIASES']['SERVER_ID_FILE'], _config['ALIASES']['SERVER_ID_URL'], _config['ALIASES']['STALE_TIME']) logger.info('(ALIAS) %s', result) - - # Make Dictionaries - peer_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['PEER_FILE']) - if peer_ids: - logger.info('(ALIAS) ID ALIAS MAPPER: peer_ids dictionary is available') - - subscriber_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['SUBSCRIBER_FILE']) - #Add special IDs to DB - subscriber_ids[900999] = 'D-APRS' - subscriber_ids[4294967295] = 'SC' + try: + _peer_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['PEER_FILE']) + except Exception as e: + logger.error('(ALIAS) ID ALIAS MAPPER: problem with data in peer_ids dictionary, not updating: %s',e) + else: + peer_ids = _peer_ids + if peer_ids: + logger.info('(ALIAS) ID ALIAS MAPPER: peer_ids dictionary is available') - if subscriber_ids: - logger.info('(ALIAS) ID ALIAS MAPPER: subscriber_ids dictionary is available') + try: + _subscriber_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['SUBSCRIBER_FILE']) + except Exception as e: + logger.info('(ALIAS) ID ALIAS MAPPER: problem with data in subscriber_ids dictionary, not updating: %s',e) + else: + subscriber_ids = _subscriber_ids + #Add special IDs to DB + subscriber_ids[900999] = 'D-APRS' + subscriber_ids[4294967295] = 'SC' - talkgroup_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['TGID_FILE']) - if talkgroup_ids: - logger.info('(ALIAS) ID ALIAS MAPPER: talkgroup_ids dictionary is available') - - local_subscriber_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['LOCAL_SUBSCRIBER_FILE']) - if subscriber_ids: - logger.info('(ALIAS) ID ALIAS MAPPER: local_subscriber_ids dictionary is available') - - server_ids = mk_server_dict(_config['ALIASES']['PATH'], _config['ALIASES']['SERVER_ID_FILE']) + if subscriber_ids: + logger.info('(ALIAS) ID ALIAS MAPPER: subscriber_ids dictionary is available') + try: + _talkgroup_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['TGID_FILE']) + except Exception as e: + logger.info('(ALIAS) ID ALIAS MAPPER: problem with data in talkgroup_ids dictionary, not updating: %s',e) + else: + talkgroup_ids = _talkgroup_ids + if talkgroup_ids: + logger.info('(ALIAS) ID ALIAS MAPPER: talkgroup_ids dictionary is available') + try: + _local_subscriber_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['LOCAL_SUBSCRIBER_FILE']) + except Exception as e: + logger.info('(ALIAS) ID ALIAS MAPPER: problem with data in local_subscriber_ids dictionary, not updating: %s',e) + else: + local_subscriber_ids = _local_subscriber_ids + if subscriber_ids: + logger.info('(ALIAS) ID ALIAS MAPPER: local_subscriber_ids dictionary is available') + try: + _server_ids = mk_server_dict(_config['ALIASES']['PATH'], _config['ALIASES']['SERVER_ID_FILE']) + except Exception as e: + logger.info('(ALIAS) ID ALIAS MAPPER: problem with data in server_ids dictionary, not updating: %s',e) + else: + server_ids = _server_ids if server_ids: logger.info('(ALIAS) ID ALIAS MAPPER: server_ids dictionary is available')