Skip to content

Instantly share code, notes, and snippets.

@DarkMentat
Last active August 29, 2015 14:25
Show Gist options
  • Save DarkMentat/6b999fbbc7e1daf085fc to your computer and use it in GitHub Desktop.
Save DarkMentat/6b999fbbc7e1daf085fc to your computer and use it in GitHub Desktop.
VK retrieve friends of friends
var r = API.friends.get({"user_id": Args.user_id, "count" : "24", "offset": Args.offset, "fields": "domain"});
var friends = [];
var i = 0;
while(i < r.items.length){
var ff = API.friends.get({"user_id": r.items[i].id});
friends.push({
"id": r.items[i].id,
"first_name": r.items[i].first_name,
"last_name": r.items[i].last_name,
"domain": r.items[i].domain,
"friends_count": ff.count,
"friends": ff.items
});
i = i + 1;
}
return {
"count": r.count,
"friends": friends
};
import json
import urllib.request
APP_ID = "4998098"
PERMISSIONS = "offline"
REDIRECT_URI = "https://oauth.vk.com/blank.html"
DISPLAY = "page"
API_VERSION = "5.34"
def get_auth_url():
return ("https://oauth.vk.com/authorize?"
"client_id=" + APP_ID + "&"
"scope=" + PERMISSIONS + "&"
"redirect_uri=" + REDIRECT_URI + "&"
"display=" + DISPLAY + "&"
"v=" + API_VERSION + "&"
"response_type=token")
"""
This class uses https://vk.com/dev/execute feature of vk api.
VK execute script: execute.friendsOfFriends(user_id, offset)
"""
class Vk:
def __init__(self, access_token):
self.access_token = access_token
def get_friends_of_friends(self, user_id):
url = "https://api.vk.com/method/execute.friendsOfFriends?"
url += "user_id=" + str(user_id) + "&"
url += "offset=" + "{offset}" + "&"
url += "v=" + API_VERSION + "&"
url += "access_token=" + self.access_token
friends = []
offset = 0
while True:
response = urllib.request.urlopen(url.format(offset=str(offset)))
if response.status != 200:
return None
response = response.read().decode(response.headers.get_content_charset())
response = json.loads(response).get("response")
if response is None:
return None
count = response.get("count", -1)
if count < 0:
continue
added = response.get("friends")
friends += added
offset += len(added)
if count == len(friends):
break
return friends
if __name__ == "__main__":
print(get_auth_url())
token = input("Enter access token: ")
vk = Vk(token)
print(vk.get_friends_of_friends(42153047))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment