def nodeslinks(threshold):

    nodes = []
    links = []
    
    # lines look like "nodeA,nodeB,123"
    for line in sys.stdin:
        tokens = line.split(",")
        # use try to ignore header line
        try:
            if int(tokens[2]) >= threshold:
                if not tokens[0] in nodes:
                    nodes.append(tokens[0])
                if not tokens[1] in nodes:
                    nodes.append(tokens[1])
                links.append({"source": nodes.index(tokens[0]), 
                "target": nodes.index(tokens[1]), 
                "value": int(tokens[2])}) 
        except:
            continue
    
    nodelist = []
    for node in nodes:
        nodelist.append({"name": node})
    
    print json.dump({"nodes": nodelist, "links": links})
dataset/ETHPy150Open pbinkley/twarc-report/d3output.py/nodeslinks/original