import argparse import json import redis import os parser = argparse.ArgumentParser(description='JSON importer for CyCAT backend') parser.add_argument('-f', '--file', help='JSON file to import') parser.add_argument('-t', '--type', help='CyCAT backend type', default=1) parser.add_argument('-p', '--parent', help='Parent reference to add for the JSON imported', default=None) args = parser.parse_args() r = redis.Redis(host='127.0.0.1', port='3033') if not args.file: parser.print_usage() os.sys.exit(1) with open(args.file, 'r') as f: toimport = f.read() record = json.loads(toimport) if int(args.type) == 1: uuid = record['cycat-oid'] r.set("u:{}".format(uuid), args.type) d = {"{}".format(uuid): 1} k = "t:{}".format(args.type) r.zadd(k, d, nx=False) print(uuid) r.hmset("{}:{}".format(args.type, uuid), record) print(record) elif int(args.type) == 2: uuid = record['cycat-oid'] r.set("u:{}".format(uuid), args.type) d = {"{}".format(uuid): 1} k = "t:{}".format(args.type) r.zadd(k, d, nx=False) r.hmset("{}:{}".format(args.type, uuid), record) if args.parent is not None: r.sadd("parent:{}".format(uuid), args.parent) r.sadd("child:{}".format(args.parent), uuid) else: pass