-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvk_class.py
More file actions
83 lines (64 loc) · 2.23 KB
/
vk_class.py
File metadata and controls
83 lines (64 loc) · 2.23 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
import sys
import time
from random import randrange
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
class Kinder:
def __init__(self, token=None, c_token=None):
self.v = None
self.vk = None
self.token = token
self.c_token = c_token
if self.token and self.c_token:
self.vk = vk_api.VkApi(token=self.token).get_api()
self.c_vk = vk_api.VkApi(token=self.c_token)
self.c_lp = VkLongPoll(vk_api.VkApi(token=self.c_token))
try:
self.users_get()
except vk_api.exceptions.ApiError as error_msg:
print(error_msg)
sys.exit()
def users_get(self, id_=None):
try:
some_user = self.vk.users.get(user_ids=id_, fields='sex, bdate, city, relation')
time.sleep(0.3)
except vk_api.exceptions.ApiError as error_msg:
print(error_msg)
return
return some_user
def get_prof_photos(self, id_):
try:
profile = self.vk.photos.get(owner_id=id_, album_id='profile', extended=1)
time.sleep(0.3)
except vk_api.exceptions.ApiError as error_msg:
print(error_msg)
return
return profile
def search(self, params):
tool = vk_api.tools.VkTools(self.vk)
try:
res = tool.get_all_iter('users.search', 1000, values=params)
except vk_api.exceptions.ApiError as error_msg:
print(error_msg)
return
return res
def write_msg(self, user_id, message):
self.c_vk.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': randrange(10 ** 7), })
def read_msg(self):
for event in self.c_lp.listen():
if event.type == VkEventType.MESSAGE_NEW:
if event.to_me:
return event
class Talk:
def __init__(self, k, id_):
self.k = k
self.id_ = id_
def write(self, message):
self.k.write_msg(self.id_, message)
def read(self):
text = False
while not text:
event = self.k.read_msg()
if event.user_id == self.id_:
text = event.text
return text