3D可视化--酷炫的opengraphiti应用

展示

先来看这么一段酷炫的视频

http://security.pku.edu.cn/cvedemo/v2/1.mp4

源自:https://www.youtube.com/watch?v=JsEm-CDj4qM&feature=youtu.be
网站: http://www.opengraphiti.com/
github: https://github.com/ThibaultReuille/graphiti

由于这个项目网上没有什么文档和参考资料,所以写下一点东西分享一下。

这是我做出的一个东西

http://security.pku.edu.cn/cvedemo/v2/2.mp4

环境配置

待续,参照http://www.opengraphiti.com/

流程

在这里插入图片描述

生成原始数据

获取,节点、边等之间的信息与关系

示例:爬取某一比特币地址的前后各一层交易与地址信息

在这里插入图片描述

# coding: utf-8
import requests
import json
# tx_info = {'tx': tx_hash,  'tx_info': {'tx_time': tx_time, 'address_in': address_in, 'address_in_value': address_in_value, 'address_in_value_all': address_in_value_all,
#                                            'address_out': address_out, 'address_out_value': address_out_value, 'address_out_value_all': 	address_out_value_all
#                                            }
#                }

#   addr -> tx    1
#   tx   -> addr -1


search_deep = 0  # end when up to deep or coinbase

dict_addr = {}


def process_single_tx(tx_info):
	global search_deep

 if search_deep < 2:

    tx = tx_info['tx']  # label
    # id
    # type
    # color
    # ...light
    address_in = tx_info['tx_info']['address_in']  # list
    address_out = tx_info['tx_info']['address_out']  # list
    # address_in_value = tx_info['tx_info']['address_in_value']
    # address_out_value = tx_info['tx_info']['address_out_value']

    for i in xrange(address_in):
        if address_in[i] not in dict_addr.keys():
            dict_addr[address_in[i]] = {}  # 如果地址字典中没有该地址,就创造一个字典并将其加入根字典
        dict_addr[address_in[i]][tx] = 1           # addr -> tx

        process_single_address(address_in[i])
    
    for i in xrange(address_out):
        if address_out[i] not in dict_addr.keys():
            dict_addr[address_out[i]] = {}  # 如果地址字典中没有该地址,就创造一个字典并将其加入根字典
        dict_addr[address_out[i]][tx] = -1           # addr -> tx

        process_single_address(address_out[i])

    search_deep += 1


def process_single_address(address):
	url = 'https://blockchain.info/rawaddr/' + address
	r = requests.get(url)
	json_origin = r.json()

tx = json_origin['txs']
for i in xrange(len(tx)):
    tx_inputs = tx[i]['inputs']
    tx_hash = tx[i]['hash']
    tx_out = tx[i]['out']
    tx_time = tx[i]['time']
    address_in = []
    # address_in_value = []
    # address_in_value_all = 0
    for j in xrange(len(tx_inputs)):
        if 'prev_out' not in tx_inputs[j].keys():
            continue
        addr = tx_inputs[j]['prev_out']['addr']
        address_in.append(addr)

        if addr not in dict_addr.keys():
            dict_addr[addr] = {}  # 如果地址字典中没有该地址,就创造一个字典并将其加入根字典
        dict_addr[addr][tx_hash] = 1           # addr -> tx

        # address_in_value.append(tx_inputs[i]['prev_out']['value'])
        # address_in_value_all += tx_inputs[i]['prev_out']['value']

    address_out = []
    # address_out_value = []
    # address_out_value_all = 0
    for j in xrange(len(tx_out)):
        if 'addr' not in tx_out[j].keys():
            continue
        addr = tx_out[j]['addr']

        if addr not in dict_addr.keys():
            dict_addr[addr] = {}  # 如果地址字典中没有该地址,就创造一个字典并将其加入根字典
        dict_addr[addr][tx_hash] = -1  # tx -> addr

        address_out.append(addr)
        # address_out_value.append(tx_out[i]['value'])
        # address_out_value_all += tx_out[i]['value']

    # tx_info = {'tx': tx_hash,  'tx_info': {'tx_time': tx_time, 'address_in': address_in, 'address_in_value': address_in_value, 'address_in_value_all': address_in_value_all,
    #                                        'address_out': address_out, 'address_out_value': address_out_value, 'address_out_value_all': address_out_value_all
    #                                        }
    #            }
    # return tx_info


process_single_address('1CGY85YnzsjtdMYgA52cq7z7gmBb94ha5W')
# print(json_origin)
jsonData = json.dumps(dict_addr)
fileObject = open('dict_addr.json', 'w')
fileObject.write(jsonData)
fileObject.close()
print('finished')

生成的部分json 如下:

{
"12vRWYQHkeHTsxszYcF8Py9NtMxUAom1PR": {
    "5fd67f4b165e1f92343830afdda889e46b50ebdc287eb13072e43064b47534f9": 1
},
"19W7TSDchk29NLgQ1krQqNL2soJ13sBfbJ": {
    "daecc0c04f6ef67f3db3bb67872d89eec984c86df2054ba0dda9ab2b06a7065d": 1
},
"36F2G8uH98XAkJkmNBk5GUw7dXfadegoUj": {
    "6cf91d164863d5d2bcb06a0f79126203ab5406d0d9b5cb777ddf0eb737f72629": -1
},
"1CryU5iTQGZ8Ho3hf49R8q7pnXVJbD7dCW": {
    "5e74da8ed3e813ede56798e698d57d745bebd48c641c489a1addc803e3fbaa91": 1
},
"16TqGJ9yVj6SYKbfXbdABEZtAYPZgssC2z": {
    "2e4331a30f107032002d9c7e0a11c8f959aa004b2fe55ec357f0676ebc61966a": 1
},
...
}

其中,key表示的是地址,value表示的是交易,1与-1代表的是方向

增加属性

属性有:

  1. 颜色(RGBA)
  2. 光圈大小(activity)
  3. 节点大小(size)
  4. 类型(type)
  5. 边宽度(width)
  6. 显示内容(label)
import json
with open('dict_addr.json', 'r') as f:
	l = f.read()
dict_addr = json.loads(l)
addr_node_list = []
tx_node_list = []
edge_list = []

dict_node_id = {}

edges = []
meta = {}
nodes = []

id = 0
edge_id = 0

tx_list = []

init_color = [1.0, 0.0, 0.0, 1.0]
addr_color = [0.4, 0.6, 0.6, 1.0  ]
addr_size = 1.0
addr_type = "1"
addr_activity = 0.0

tx_color = [1.0, 1.0, 0.0, 1.0]
tx_size = 2.0
tx_type = "2"
tx_activity = 1.1

edge_color = [0.4, 0.4, 0.7, 1.0]
edge_size = 1.0
edge_type = "3"
edge_width = 0.5
edge_activity = 0.2



def generate_address_node(key):
    global id
    if key not in addr_node_list:
        addr_node_list.append(key)
        label = key
        activity = addr_activity
        if key == '1CGY85YnzsjtdMYgA52cq7z7gmBb94ha5W':
                color = init_color
        else:
                color = addr_color
        size = addr_size
        type = addr_type
        #设定地址节点属性
        node_info = {"id":id,"label": label, "og:space:activity": activity,
                     "og:space:color": color, "og:space:size": size, "type": type}
        nodes.append(node_info)
        dict_node_id[key] = id
        id += 1

def generate_tx_node(tx):
    global id
    if tx not in tx_node_list:
        tx_node_list.append(tx)
        label = tx
        activity = tx_activity
        color = tx_color
        size = tx_size
        type = tx_type
        #设定交易节点属性
        node_info = {"id":id, "label": label, "og:space:activity": activity,
                     "og:space:color": color, "og:space:size": size, "type": type}
        nodes.append(node_info)
        dict_node_id[tx] = id
        id += 1

def generate_edge(key, tx, direction):
    global edge_id
    if direction > 0:
        src = dict_node_id[key]
        dst = dict_node_id[tx]
    else:
        src = dict_node_id[tx]
        dst = dict_node_id[key]
    activity = edge_activity
    color1 = edge_color
    color2 = edge_color
    width = edge_width
    type = edge_type
    #设定边属性
    edge_info = {"id":edge_id,"dst":dst,"src":src, "og:space:activity": activity,
                     "og:space:color1": color1,"og:space:color2": color2, "og:space:width": width, "type": type}
    edges.append(edge_info)
    edge_id += edge_id
    
for key, value in dict_addr.items():
    for tx, direction in value.items():
        generate_address_node(key)
        generate_tx_node(tx)
        generate_edge(key, tx, direction)



#组装成最后的json数据
test = {"edges":edges,"meta":meta,"nodes":nodes}

jsonData = json.dumps(test)
fileObject = open('test1.json', 'w')
fileObject.write(jsonData)
fileObject.close()
print('finished')

运行

运行选择最终生成的json文件,大功告成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值