def create_graph(): ''' create a vtuber relationd graph ''' PAGE_SIZE = 20 vgraph = networkx.Graph() client = Mongo(CONFIG["mongo"]["addr"], 'youtube') vtubers = client.loadWholeDoc('vtuber') video_num = client.loadWholeDoc('videosv2').count() for i in range(0, video_num, PAGE_SIZE): v_page = client.loadWholeDoc('videosv2').skip(i).limit(PAGE_SIZE) for v in v_page: owner = v['channelId'] try: desc = v['description'] except KeyError: # some times des is not exist in video continue extract_relation(owner, desc, vgraph) # preview # networkx.draw(vgraph, with_labels=True, font_weight='bold') # plt.show() # split the name from vtb list name_dict = {} vtb_dict = {} for v in vtubers: name = v['channel'] try: channel_id = v['channel_url'].split('/')[-1] except: continue name_dict[channel_id] = name vtb_dict[channel_id] = v # update the position and size of node in graph pos = networkx.random_layout(vgraph) # clean out all node who is not in vtuber list remove_list = [] for _n in vgraph.nodes(): if not _n in name_dict.keys(): remove_list.append(_n) vgraph.remove_nodes_from(remove_list) for _n in vgraph.nodes(): vgraph.node[_n]['viz']['position'] = { 'x': pos[_n][0] * (-100) * 1.5, 'y': pos[_n][1] * 100, 'z': 0 } # print(math.log2(vtb_dict[_n]['regsit'])) #vgraph.node[_n]['viz']['size'] = vgraph.degree[_n] vgraph.node[_n]['viz']['size'] = (math.log2(vtb_dict[_n]['regsit']) - 10) * 10 vgraph = networkx.relabel_nodes(vgraph, name_dict) # networkx.write_gexf(vgraph, '../data/vtb.gexf') return vgraph
def gen_mock_data(): ''' generate fake data for test ''' client = Mongo(CONFIG["mongo"]["addr"], 'youtube') data = client.loadWholeDoc('vtuber') graph = networkx.Graph() vtubers = [] for i in range(10): vtubers.append(data[i]['channel']) graph.add_node(data[i]['channel'], viz={}, mod=1, id=0) pos = networkx.random_layout(graph) counter = 0 for v in vtubers: graph.node[v]['id'] = counter graph.node[v]['viz']['size'] = 20 graph.node[v]['viz']['position'] = { 'x': pos[v][0] * (-100), 'y': pos[v][1] * 100, 'z': 0 } graph.node[v]["viz"]['color'] = {'r': 255, 'g': 192, 'b': 201, 'a': 1} print(graph.node[v]) counter = counter + 1 graph.add_weighted_edges_from([(vtubers[1], vtubers[5], 3), (vtubers[2], vtubers[4], 2), (vtubers[1], vtubers[3], 3), (vtubers[1], vtubers[7], 1), (vtubers[5], vtubers[9], 3), (vtubers[4], vtubers[8], 3), (vtubers[1], vtubers[8], 3), (vtubers[1], vtubers[2], 3), (vtubers[6], vtubers[7], 2)]) #graph = networkx.generate_gexf(graph) # regen id networkx.write_gexf(graph, '../data/mock.gexf')