From f87338d52e26f20d73f6d186ccdbf0a3e3b4c77b Mon Sep 17 00:00:00 2001 From: Alexandre Dulaunoy Date: Sat, 22 May 2021 19:20:38 +0200 Subject: [PATCH] new: [importer] minimal JSON importer --- backend/bin/importer.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 backend/bin/importer.py diff --git a/backend/bin/importer.py b/backend/bin/importer.py new file mode 100644 index 0000000..cd26d26 --- /dev/null +++ b/backend/bin/importer.py @@ -0,0 +1,36 @@ +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) +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) +else: + pass