#!/usr/bin/env python3

############################################################################
# Fetch matches edges
############################################################################

import json
import pickle
import gaffer

gaffer_url = "https://analytics.trustnetworks.com/gaffer-threat"

g = gaffer.Gaffer(gaffer_url)
g.use_cert()

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

matches = {}

for v in r:

    try:
        obj = json.loads(v)
    except:
        # Ignore lines which don't parse as JSON
        continue

    src = obj["source"]
    dest = obj["destination"]
    
    if not src in matches:
        matches[src] = []
    matches[src].append({
        "blacklist": dest,
        "latest": obj["properties"]["time"]["uk.gov.gchq.gaffer.time.RBMBackedTimestampSet"]["latest"]
    })

r.close()

pickle.dump(matches, open("cache.MATCH", "wb"))

