-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvk_api.py
53 lines (40 loc) · 2.28 KB
/
vk_api.py
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
""" Набор функций для работы с VK API """
import requests
GET_FRIENDS_METHOD_URL = 'https://api.vk.com/method/friends.get'
API_VERSION = '5.131'
def get_user_friends_list(access_token: str, user_id: int, fields: list,
api_version: str = API_VERSION,
verbose: bool = False) -> list:
""" Функция возвращает список друзей пользователя используя метод VK API friends.get
Метод VK API: https://vk.com/dev/friends.get
"""
user_friends_list = []
offset_count = 5000
result_count = offset_count
request_num = 0
# При вызове метода friends.get с аргументом fields возможно получить не более 5000 друзей,
# поэтому в цикле используем friends.get с аргументом offset = offset_count * request_num.
while result_count >= offset_count:
if verbose:
print('Trying to get the user\'s friends...')
offset = offset_count * request_num
get_friends_response = requests.get(GET_FRIENDS_METHOD_URL,
{'access_token': access_token,
'user_id': user_id,
'fields': ','.join(fields),
'offset': offset,
'v': api_version})
get_friends_response_json = get_friends_response.json()
error_response = get_friends_response_json.get('error', {})
if error_response:
error_code = error_response.get('error_code', 'Unknown error')
error_msg = error_response.get('error_msg', '-1')
raise ValueError(f'{error_msg} [Error code: {error_code}]')
get_friends_response = get_friends_response_json.get('response', {})
friends_list = get_friends_response.get('items', [])
result_count = len(friends_list)
user_friends_list += friends_list
request_num += 1
if verbose:
print(f'\tReceived {result_count} friend{"s" if result_count > 1 else ""}')
return user_friends_list