File size: 3,353 Bytes
80e5c7b
323553e
 
 
 
 
 
 
 
80e5c7b
 
323553e
 
 
 
 
57cb69a
323553e
 
 
57cb69a
323553e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e1e13f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import streamlit as st
import requests
import plotly.graph_objects as go
import networkx as nx
import time
import matplotlib.pyplot as plt

access_token = '24001214633baa923e4caf1fba8da62847aa8df94b3b041d40b54186eabd6a16a81962c9bbbd02f52082a'


x = st.slider('Select a value')
st.write(x, 'squared is', x * x)


api_version = '5.131'
my_id = '167196653'
read_id = '0'  # input("Type interested vk id or write \"0\" to use default: ")
if read_id != '0':
    my_id = read_id
    print("my_id changed", my_id)
friend_num = 15  # int(input("Type how many friends you want to show or write \"0\" show all: "))


params_for_users_get = {'user_ids': my_id,
                        'access_token': access_token,
                        'v': api_version}

myFirstAndLastName = requests.get('https://api.vk.com/method/users.get', params=params_for_users_get)
#print(myFirstAndLastName.text)

params_for_friends_get = {'user_id': my_id,
                          'order': 'hints',
                          'fields': 'sex',
                          'access_token': access_token,
                          'v': api_version}

r = requests.get('https://api.vk.com/method/friends.get', params=params_for_friends_get)
if 'error' in r.json() and r.json()['error']['error_code'] == 30:
    print('This is private profile, sorry')
    exit()
print(r.text)
AllMyFriends = r.json()['response']['items']
#print(AllMyFriends[0]['id'])
#print(AllMyFriends[0]['is_closed'])
#print(AllMyFriends)
AllMyFriendsWithoutClosed = [i for i in AllMyFriends if 'is_closed' in i and i['is_closed'] == False]
if friend_num == 0:
    friend_num = len(AllMyFriendsWithoutClosed)

G = nx.Graph()


def GetFriendListById(friend_id):
    while True:
        params_for_another_friends_get = {'user_id': friend_id,
                                          'order': 'random',
                                          'access_token': access_token,
                                          'v': api_version}
        res = requests.get('https://api.vk.com/method/friends.get', params=params_for_another_friends_get)
        print(res.text)
        res_json = res.json();
        if 'error' in res_json:
            if res_json['error']['error_code'] == 6:
                time.sleep(0.8)
                continue
            if res_json['error']['error_code'] == 30:
                return [], False
            return [], False
        if 'response' in res_json:
            return res_json['response']['items'], True
        return [], False


MyFriends = AllMyFriendsWithoutClosed[:friend_num-1]
#MyFriends.append(myFirstAndLastName.json()['response'][0])
MyFriendsSet = set([i['id'] for i in MyFriends])
MyFriendsDict = dict([(i['id'], i) for i in MyFriends])
MyFriendsDict[int(my_id)] = myFirstAndLastName.json()['response'][0]
#print(MyFriends)
#print(G.nodes())
for friend_id in MyFriends:
    friendFriendList, Flag = GetFriendListById(friend_id['id'])
    if not Flag:
        continue
    G.add_edge(friend_id['id'], int(my_id))
    for friendFriend_id in friendFriendList:
        if friendFriend_id in MyFriendsSet:
            G.add_edge(friend_id['id'], friendFriend_id)

G_nodes_list = list(G.nodes)
#print(G_nodes_list)

pos = nx.spring_layout(G)
#print(pos)

nx.draw_networkx_nodes(G, pos, node_size=50)
nx.draw_networkx_edges(G, pos, edge_color='b')
# plt.show()

st.pyplot(plt)