Skip to content

Instantly share code, notes, and snippets.

@orsinium
Created May 4, 2017 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orsinium/288ac5f67efa0e196fda3b386cb7c5c3 to your computer and use it in GitHub Desktop.
Save orsinium/288ac5f67efa0e196fda3b386cb7c5c3 to your computer and use it in GitHub Desktop.
Поиск общих друзей в vk между заданными пользователями
import vk
from time import sleep
from re import compile, sub
#http://vk.com/dev
token = input('ID приложения: ')
print('https://oauth.vk.com/authorize?client_id='+token+'&scope=wall,offline&display=page&response_type=token')
token = input('Токен: ')
vkapi = vk.API(access_token=token)
rex = compile(r'[^0-9]+')
cache_friends = dict()
def get_friends(uid):
if uid in cache_friends:
return cache_friends[uid]
else:
sleep(1)
while 1:
try:
fr = vk.friends.get(user_id=uid)
except Exception as e:
if str(e).find('Access denied: user deactivated.')>-1:
return list()
else:
print(e)
sleep(5)
else:
fr = set(fr['items'])
cache_friends[uid] = fr
return fr
def get_cfriends(uid1, uid2):
sleep(1)
while 1:
try:
fr = vk.friends.getMutual(source_uid=uid1, target_uid=uid2)
except Exception as e:
print(e)
sleep(5)
else:
return set(fr)
names = dict()
def get_name(uid):
if uid in names:
return names[uid]
else:
sleep(1)
while 1:
try:
u = vk.users.get(user_ids=uid)
except Exception as e:
print(e)
sleep(5)
else:
name = u[0]['first_name']+' '+u[0]['last_name']+' ('+str(uid)+')'
names[uid] = name
return name
while 1:
ids = input('Введите через пробел ссылки на профили или id пользователей:\n')
ids = rex.sub(' ', ids)
ids = [int(i) for i in ids.split(' ') if len(i)>0]
friends = []
cfriends = []
print()
for uid in ids:
friends.append(get_friends(uid))
print(get_name(uid))
print()
for i in range(len(ids)):
cfriends.append([0]*len(ids))
for j in range(i+1,len(ids)):
#общие друзья
cfriends[i][j] = get_cfriends(ids[i], ids[j])
if ids[i] in friends[j]:
print(get_name(ids[i]),'дружит с',get_name(ids[j]))
print()
total = set(friends[0])
for fr in friends:
total = total & fr
if len(total)>0:
print('Общие друзья ('+str(len(total))+'):')
for i in list(total):
print(get_name(i))
print()
if len(ids)>2:
for i in range(len(ids)):
for j in range(i+1,len(ids)):
if len(cfriends[i][j])>0:
print('Общие друзья между',get_name(ids[i]),'и',get_name(ids[j])+':')
for k in cfriends[i][j]:
print(' ',get_name(k))
print()
print('3 рукопожатия:')
for i in range(len(ids)):
for j in range(i+1,len(ids)):
print('Между',get_name(ids[i]),'и',get_name(ids[j])+':')
for k in friends[i]:
for l in friends[j]:
if k not in friends[j] and l not in friends[i] and len({k, l} & {ids[i], ids[j]})==0 and l in get_friends(k):
print(' ',get_name(k),'и',get_name(l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment