yanic/contrib/yanic-import-timestamp

83 lines
2.6 KiB
Plaintext
Raw Normal View History

2017-03-26 08:53:21 +02:00
#!/usr/bin/env python3
import argparse
import json
2017-03-26 08:53:21 +02:00
import os
import sys
from datetime import datetime
2017-03-26 08:53:21 +02:00
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--nodesjson', action='store', required=True,
help='old nodes.json file you want to read firstseen from')
2017-03-26 08:53:21 +02:00
parser.add_argument('-s', '--state', action='store', required=True,
help='state.json you want to store')
2017-03-26 08:53:21 +02:00
args = parser.parse_args()
with open(os.path.realpath(args.nodesjson), encoding='UTF-8') as handle:
legacy_db = json.load(handle)
# w/o a version tag we cannot decide how to walk and interpret the nodes.json
assert ('version' in legacy_db)
yanic_db_path = os.path.realpath(args.state)
with open(yanic_db_path, encoding='UTF-8') as handle:
yanic_db = json.load(handle)
total = 0
updated = 0
yanic_date_format = '%Y-%m-%dT%H:%M:%S+0000'
v1_date_format = '%Y-%m-%dT%H:%M:%S.%f' # 2017-05-31T18:30:19.759610
v2_date_format = '%Y-%m-%dT%H:%M:%S.%fZ' # 2015-08-22T16:05:02.000Z
version = legacy_db['version']
print('nodes.json is in v{} format'.format(version))
fallback_date_format = None
if version == 1:
legacy_date_format = v1_date_format # ffmap-backend
elif version == 2:
legacy_date_format = v2_date_format # hopglass
fallback_date_format = v1_date_format # ffmap-backend
else:
print('unhandled nodes.json version number!', file=sys.stderr)
sys.exit(1)
for nodeid, node in yanic_db.get('nodes', {}).items():
legacy_node = None
if version == 1:
# v1 nodes.json is a dict, so lookups are cheap
legacy_node = legacy_db.get('nodes', {}).get(nodeid, None)
elif version == 2:
# v2 nodes.json however carries nodes as a list of dicts …
legacy_node = next((
candidate for candidate in legacy_db.get('nodes', [])
if candidate['nodeinfo']['node_id'] == nodeid), None)
if legacy_node is not None:
try:
dt = datetime.strptime(legacy_node['firstseen'], legacy_date_format)
except ValueError as ex:
# time format mismatch, try fallback format
if fallback_date_format is not None:
dt = datetime.strptime(legacy_node['firstseen'],
fallback_date_format)
else:
# or if none is set, re-raise the original exception
raise ex
dts = dt.strftime(yanic_date_format)
if node['firstseen'] != dts:
node['firstseen'] = dts
updated += 1
total += 1
with open(yanic_db_path, 'w') as f:
json.dump(yanic_db, f)
print('found {} nodes, {} changed.'.format(total, updated))