#!/usr/bin/env python

# Displays ...

# Runs GetElements to perform a query, and then uses Limit to restrict the
# data set to 15.

import requests
import json
import sys
import gaffer
import threatgraph
from tabulate import tabulate

g = threatgraph.Gaffer()
g.use_cert()

op = gaffer.GetAllElements(edges=None, entities=["blacklist"])
res = g.execute_chunked(op)

prob = {}
src = {}

for v in res:

    try:
        o = json.loads(v)
    except:
        continue

    if o["group"] == "blacklist":
        prob[o["vertex"]] = o["properties"]["probability"]
        src[o["vertex"]] = o["properties"]["source"]

op = gaffer.GetAllElements(edges=["matches"], entities=None)
res = g.execute_chunked(op)

data = []        

for v in res:

    try:
        o = json.loads(v)
        print o
    except:
        continue

    if o["group"] == "matches":
        thing = o["source"]
        bl = o["destination"]
        p = prob.get(bl, 0.0)
        s = src.get(bl, "")
        d = o["properties"]["description"]
        print [thing, s, p, bl, d]

