-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvk_profile.py
More file actions
129 lines (114 loc) · 5.1 KB
/
vk_profile.py
File metadata and controls
129 lines (114 loc) · 5.1 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import requests
import json
import sys
with open('ACCESS_TOKEN.txt', 'r') as file:
ACCESS_TOKEN = file.read()
def help():
print('ввод:\npy vk_profile.py [ID пользователя] (парамметры)')
print('параметры:\n-subscriptions публичные страницы пользователся\n-friends друзья\n-albums альбомы')
print('Пример ввода:\npy vk_profile.py 0 -friends')
class VkApi:
ALL_API_METHODS = ['-subscriptions', '-friends', '-albums']
def __init__(self, username=0):
r = requests.post(
f'https://api.vk.com/method/users.get?user_ids={username}&fields=city,domain&access_token={ACCESS_TOKEN}&v=5.103')
if r.status_code == 200:
self.user_baseinfo = json.loads(r.content)['response'][0]
self.user_id = self.user_baseinfo['id']
else:
print('не удалось получить страницу пользователя')
def get_userinfo(self, parameters=[]):
if '-all' in parameters:
parameters = VkApi.ALL_API_METHODS
self.get_user_base_info()
if '-subscriptions' in parameters:
VkApi.get_user_subscriptions_info(
self.get_request('users.getSubscriptions', 'user_id',
'extended=1')['items']),
if '-friends' in parameters:
VkApi.get_friends_info(
self.get_request('friends.get', 'user_id',
'fields=city,domain')[
'items']),
if '-albums' in parameters:
VkApi.get_album_info(
self.get_request('photos.getAlbums', 'owner_id')['items'])
def get_request(self, method, id_field_name, parameters=''):
r = requests.post(
f'https://api.vk.com/method/{method}?{id_field_name}={self.user_id}&{parameters}&access_token={ACCESS_TOKEN}&v=5.103')
if r.status_code == 200:
return json.loads(r.content)['response']
return None
def get_user_base_info(self):
print('%12s%20d' % ('id:', self.user_id))
print('%12s%20s' % ('first name:', self.user_baseinfo.get('first_name')))
print('%12s%20s' % ('last name:', self.user_baseinfo.get('last_name')))
print('%12s%20s' % ('city:', VkApi.try_get_city(self.user_baseinfo)))
@staticmethod
def get_user_subscriptions_info(subscriptions_list):
print('subscriptions list:\n%10s%40s%20s' % ('id', 'name', 'type'))
if subscriptions_list:
for subscriptions in subscriptions_list:
id = subscriptions.get("id")
name = subscriptions.get("name")
type = subscriptions.get("type")
print(
'%10s%40s%20s' % (id, name, type))
return True
else:
print('не удалось получить список публичных страниц')
return False
@staticmethod
def get_friends_info(friend_list):
print('friendList:\n%10s%20s%20s%20s' % (
'id', 'first name', 'last name', 'city'))
if friend_list:
for friend in friend_list:
friend_id = friend.get("id")
first_name = friend.get("first_name")
last_name = friend.get("last_name")
city = VkApi.try_get_city(friend)
print(
'%10s%20s%20s%20s' % (
friend_id, first_name, last_name, city))
else:
print('не удалось получить список друзей')
@staticmethod
def try_get_city(container: dict):
city_info = container.get("city")
if city_info:
return city_info.get('title')
return city_info
@staticmethod
def get_album_info(album_list):
print('Album list:\n%10s%10s%50s' % (
'name', 'size', 'description'))
if album_list:
for album in album_list:
title = album.get("title")
size = album.get("size")
description = album.get("description")
formatted_description = ''
if description:
for i in range(len(description)):
if i % 50 != 0 or i == 0:
formatted_description += description[i]
else:
formatted_description += f'{description[i]}\n\t\t\t\t\t\t'
print('%10s%10d%50s' % (title, size, formatted_description))
return True
else:
print('не удалось получить список альбомов')
return False
if __name__ == '__main__':
if len(sys.argv) < 2:
help()
username = sys.argv[1]
if username in ['-help', '-h']:
help()
else:
vk_api = VkApi(username)
if len(sys.argv) > 2:
vk_api.get_userinfo(sys.argv)
else:
vk_api.get_userinfo()