forked from mnemonic-no/act-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_uploader.py
executable file
·52 lines (40 loc) · 1.57 KB
/
generic_uploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""General ACT backend uploader. Reads facts as JSON
from the stdin, uploading accordingly"""
from logging import error
import traceback
import argparse
import json
import sys
import act
def parseargs():
""" Parse arguments """
parser = argparse.ArgumentParser(description='PDNS enrichment')
parser.add_argument('--userid', dest='user_id', required=True,
help="User ID")
parser.add_argument('--act-baseurl', dest='act_baseurl', required=True,
help='ACT API URI')
parser.add_argument("--logfile", dest="logfile",
help="Log to file (default = stdout)")
parser.add_argument("--loglevel", dest="loglevel", default="info",
help="Loglevel (default = info)")
return parser.parse_args()
def process(actapi):
"""Process stdin, parse each separat line as a JSON structure and
register a fact based on the structure. The form of input should
be the on the form accepted by the ACT Rest API fact API."""
for line in sys.stdin:
data = json.loads(line)
fact = actapi.fact(**data)
try:
fact.add()
except act.base.ResponseError as err:
error("ResponseError while storing objects: %s" % err)
if __name__ == '__main__':
ARGS = parseargs()
try:
actapi = act.Act(ARGS.act_baseurl, ARGS.user_id, ARGS.loglevel, ARGS.logfile, "generic-uploader")
process(actapi)
except Exception as e:
error("Unhandled exception: {}".format(traceback.format_exc()))
raise