#!/usr/bin/env python

import gaffer
import json

g = gaffer.Gaffer("https://analytics.trustnetworks.com/gaffer-threat")
g.use_cert()

op = gaffer.OperationChain([
    gaffer.GetAllElements(entities=["ip"], edges=None),
    gaffer.GetWalks(
        operations=[
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("ipflow", exclude=["count", "time"])
                ], entities=None, include="OUTGOING")
            ]),
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("serves", exclude=["count", "time"])
                ], entities=None, include="OUTGOING")
            ]),
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("indomain", exclude=["count", "time"])
                ], entities=None, include="OUTGOING")
            ]),
            gaffer.OperationChain(operations=[
                gaffer.GetElements(edges=[
                    gaffer.ViewGroup("matches", exclude=["count", "time"])
                ], entities=None, include="OUTGOING")
            ])
        ],
        limit=1000000)
])

res = g.execute_chunked(op)

for ent in res:

    print ent

    try:
        ent = json.loads(ent)
        print ent
    except:
        continue

    src = ent["entities"][0].keys()[0]
    dest = ent["entities"][1].keys()[0]
    server = ent["entities"][2].keys()[0]
    domain = ent["entities"][3].keys()[0]
    blacklist = ent["entities"][4].keys()[0]

    print "%-15s %-15s %-20s %-20s %s" % (
        src, dest, server[:20], domain[:20], blacklist
    )

