Spaces:
Configuration error
Configuration error
Ivan Kudryakov
commited on
Commit
·
e751151
1
Parent(s):
5dfa4c1
inital commit
Browse files- .DS_Store +0 -0
- PersonalData.py +4 -0
- README.md +7 -31
- images/Add app.png +0 -0
- images/Dima Pavlyuchenkov.png +0 -0
- images/Ivan Kudryakov.png +0 -0
- images/Lycoris Radiata.png +0 -0
- main.py +169 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
PersonalData.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
access_token = '24001214633baa923e4caf1fba8da62847aa8df94b3b041d40b54186eabd6a16a81962c9bbbd02f52082a'
|
2 |
+
|
3 |
+
# to get your access token
|
4 |
+
# https://oauth.vk.com/authorize?client_id=8034168&scope=2&redirect_uri=https://oauth.vk.com/blank.html&display=page&response_type=token&revoke=1
|
README.md
CHANGED
@@ -1,37 +1,13 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 🔥
|
4 |
-
colorFrom: red
|
5 |
-
colorTo: purple
|
6 |
-
sdk: streamlit
|
7 |
-
app_file: app.py
|
8 |
-
pinned: false
|
9 |
-
---
|
10 |
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
Display title for the Space
|
15 |
|
16 |
-
|
17 |
-
Space emoji (emoji-only character allowed)
|
18 |
|
19 |
-
`colorFrom`: _string_
|
20 |
-
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
21 |
|
22 |
-
`colorTo`: _string_
|
23 |
-
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
24 |
|
25 |
-
`sdk`: _string_
|
26 |
-
Can be either `gradio` or `streamlit`
|
27 |
-
|
28 |
-
`sdk_version` : _string_
|
29 |
-
Only applicable for `streamlit` SDK.
|
30 |
-
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
31 |
-
|
32 |
-
`app_file`: _string_
|
33 |
-
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
34 |
-
Path is relative to the root of the repository.
|
35 |
-
|
36 |
-
`pinned`: _boolean_
|
37 |
-
Whether the Space stays on top of your list.
|
|
|
1 |
+
# SocialGraph
|
2 |
+
Social Graph printing using VK API (access token reqired)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
## Guide
|
5 |
+
Firstly you should visit [VK API guide](https://vk.com/dev/first_guide?f=2.%20%D0%A0%D0%B5%D0%B3%D0%B8%D1%81%D1%82%D1%80%D0%B0%D1%86%D0%B8%D1%8F%20%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F)
|
6 |
+
and register your own Standalone App
|
7 |
|
8 |
+
(Instead you can just go to [there](https://vk.com/apps?act=manage)) and click "add app"
|
|
|
9 |
|
10 |
+

|
|
|
11 |
|
|
|
|
|
12 |
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
images/Add app.png
ADDED
![]() |
images/Dima Pavlyuchenkov.png
ADDED
![]() |
images/Ivan Kudryakov.png
ADDED
![]() |
images/Lycoris Radiata.png
ADDED
![]() |
main.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import plotly.graph_objects as go
|
3 |
+
import networkx as nx
|
4 |
+
import time
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from PersonalData import access_token
|
7 |
+
|
8 |
+
api_version = '5.131'
|
9 |
+
my_id = '167196653'
|
10 |
+
read_id = input("Type interested vk id or write \"0\" to use default: ")
|
11 |
+
if read_id != '0':
|
12 |
+
my_id = read_id
|
13 |
+
print("my_id changed", my_id)
|
14 |
+
friend_num = int(input("Type how many friends you want to show or write \"0\" show all: "))
|
15 |
+
|
16 |
+
|
17 |
+
params_for_users_get = {'user_ids': my_id,
|
18 |
+
'access_token': access_token,
|
19 |
+
'v': api_version}
|
20 |
+
|
21 |
+
myFirstAndLastName = requests.get('https://api.vk.com/method/users.get', params=params_for_users_get)
|
22 |
+
#print(myFirstAndLastName.text)
|
23 |
+
|
24 |
+
params_for_friends_get = {'user_id': my_id,
|
25 |
+
'order': 'hints',
|
26 |
+
'fields': 'sex',
|
27 |
+
'access_token': access_token,
|
28 |
+
'v': api_version}
|
29 |
+
|
30 |
+
r = requests.get('https://api.vk.com/method/friends.get', params=params_for_friends_get)
|
31 |
+
if 'error' in r.json() and r.json()['error']['error_code'] == 30:
|
32 |
+
print('This is private profile, sorry')
|
33 |
+
exit()
|
34 |
+
print(r.text)
|
35 |
+
AllMyFriends = r.json()['response']['items']
|
36 |
+
#print(AllMyFriends[0]['id'])
|
37 |
+
#print(AllMyFriends[0]['is_closed'])
|
38 |
+
#print(AllMyFriends)
|
39 |
+
AllMyFriendsWithoutClosed = [i for i in AllMyFriends if 'is_closed' in i and i['is_closed'] == False]
|
40 |
+
if friend_num == 0:
|
41 |
+
friend_num = len(AllMyFriendsWithoutClosed)
|
42 |
+
|
43 |
+
G = nx.Graph()
|
44 |
+
|
45 |
+
|
46 |
+
def GetFriendListById(friend_id):
|
47 |
+
while True:
|
48 |
+
params_for_another_friends_get = {'user_id': friend_id,
|
49 |
+
'order': 'random',
|
50 |
+
'access_token': access_token,
|
51 |
+
'v': api_version}
|
52 |
+
res = requests.get('https://api.vk.com/method/friends.get', params=params_for_another_friends_get)
|
53 |
+
print(res.text)
|
54 |
+
res_json = res.json();
|
55 |
+
if 'error' in res_json:
|
56 |
+
if res_json['error']['error_code'] == 6:
|
57 |
+
time.sleep(0.8)
|
58 |
+
continue
|
59 |
+
if res_json['error']['error_code'] == 30:
|
60 |
+
return [], False
|
61 |
+
return [], False
|
62 |
+
if 'response' in res_json:
|
63 |
+
return res_json['response']['items'], True
|
64 |
+
return [], False
|
65 |
+
|
66 |
+
|
67 |
+
MyFriends = AllMyFriendsWithoutClosed[:friend_num-1]
|
68 |
+
#MyFriends.append(myFirstAndLastName.json()['response'][0])
|
69 |
+
MyFriendsSet = set([i['id'] for i in MyFriends])
|
70 |
+
MyFriendsDict = dict([(i['id'], i) for i in MyFriends])
|
71 |
+
MyFriendsDict[int(my_id)] = myFirstAndLastName.json()['response'][0]
|
72 |
+
#print(MyFriends)
|
73 |
+
#print(G.nodes())
|
74 |
+
for friend_id in MyFriends:
|
75 |
+
friendFriendList, Flag = GetFriendListById(friend_id['id'])
|
76 |
+
if not Flag:
|
77 |
+
continue
|
78 |
+
G.add_edge(friend_id['id'], int(my_id))
|
79 |
+
for friendFriend_id in friendFriendList:
|
80 |
+
if friendFriend_id in MyFriendsSet:
|
81 |
+
G.add_edge(friend_id['id'], friendFriend_id)
|
82 |
+
|
83 |
+
G_nodes_list = list(G.nodes)
|
84 |
+
#print(G_nodes_list)
|
85 |
+
|
86 |
+
pos = nx.spring_layout(G)
|
87 |
+
#print(pos)
|
88 |
+
|
89 |
+
nx.draw_networkx_nodes(G, pos, node_size=50)
|
90 |
+
nx.draw_networkx_edges(G, pos, edge_color='b')
|
91 |
+
plt.show()
|
92 |
+
|
93 |
+
edge_x = []
|
94 |
+
edge_y = []
|
95 |
+
for edge in G.edges():
|
96 |
+
x0, y0 = pos.get(edge[0]) # G.nodes[edge[0]]['pos']
|
97 |
+
x1, y1 = pos.get(edge[1]) # G.nodes[edge[1]]['pos']
|
98 |
+
edge_x.append(x0)
|
99 |
+
edge_x.append(x1)
|
100 |
+
edge_x.append(None)
|
101 |
+
edge_y.append(y0)
|
102 |
+
edge_y.append(y1)
|
103 |
+
edge_y.append(None)
|
104 |
+
|
105 |
+
edge_trace = go.Scatter(
|
106 |
+
x=edge_x, y=edge_y,
|
107 |
+
line=dict(width=0.5, color='#888'),
|
108 |
+
hoverinfo='none',
|
109 |
+
mode='lines')
|
110 |
+
|
111 |
+
node_x = []
|
112 |
+
node_y = []
|
113 |
+
for node in G.nodes():
|
114 |
+
# print(node)
|
115 |
+
x, y = pos.get(node) # G.nodes[node]['pos']
|
116 |
+
node_x.append(x)
|
117 |
+
node_y.append(y)
|
118 |
+
|
119 |
+
node_trace = go.Scatter(
|
120 |
+
x=node_x, y=node_y,
|
121 |
+
mode='markers',
|
122 |
+
hoverinfo='text',
|
123 |
+
marker=dict(
|
124 |
+
showscale=True,
|
125 |
+
# colorscale options
|
126 |
+
# 'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
|
127 |
+
# 'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
|
128 |
+
# 'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
|
129 |
+
colorscale='YlGnBu',
|
130 |
+
reversescale=True,
|
131 |
+
color=[],
|
132 |
+
size=10,
|
133 |
+
colorbar=dict(
|
134 |
+
thickness=15,
|
135 |
+
title='Node Edges',
|
136 |
+
xanchor='left',
|
137 |
+
titleside='right'
|
138 |
+
),
|
139 |
+
line_width=2))
|
140 |
+
|
141 |
+
node_adjacencies = []
|
142 |
+
node_text = []
|
143 |
+
#print(MyFriendsDict)
|
144 |
+
# print(G_nodes_list[1])
|
145 |
+
# print(MyFriendsDict[G_nodes_list[1]]['first_name'])
|
146 |
+
for node, adjacencies in enumerate(G.adjacency()):
|
147 |
+
node_adjacencies.append(len(adjacencies[1]))
|
148 |
+
node_text.append(MyFriendsDict[G_nodes_list[node]]['first_name'] + ' ' + MyFriendsDict[G_nodes_list[node]]['last_name'] + ' ' + str(len(adjacencies[1])) + ' edges')
|
149 |
+
# print(node)
|
150 |
+
|
151 |
+
node_trace.marker.color = node_adjacencies
|
152 |
+
node_trace.text = node_text
|
153 |
+
|
154 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
155 |
+
layout=go.Layout(
|
156 |
+
title='<br>Social graph to ' + myFirstAndLastName.json()['response'][0]['first_name'] + ' ' + myFirstAndLastName.json()['response'][0]['last_name'],
|
157 |
+
titlefont_size=16,
|
158 |
+
showlegend=False,
|
159 |
+
hovermode='closest',
|
160 |
+
margin=dict(b=20, l=5, r=5, t=40),
|
161 |
+
annotations=[dict(
|
162 |
+
text="Python code: <a href='https://github.com/Akuva2001/SocialGraph'> https://github.com/Akuva2001/SocialGraph</a>",
|
163 |
+
showarrow=False,
|
164 |
+
xref="paper", yref="paper",
|
165 |
+
x=0.005, y=-0.005)],
|
166 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
167 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
168 |
+
)
|
169 |
+
fig.show()
|