new: [importer] minimal JSON importer

main
Alexandre Dulaunoy 2021-05-22 19:20:38 +02:00
parent 15092537b0
commit f87338d52e
No known key found for this signature in database
GPG Key ID: 09E2CD4944E6CBCD
1 changed files with 36 additions and 0 deletions

36
backend/bin/importer.py Normal file
View File

@ -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