VKGraph / app.py
Ivan Kudryakov
commit 1
1e1e13f
raw
history blame
3.35 kB
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)