-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomActions.py
executable file
·102 lines (96 loc) · 3.76 KB
/
customActions.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
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
import random
import time
import logging
import logging.handlers
import os
import sys
import requests.exceptions
from vk_api.longpoll import VkEventType
cwd = os.path.dirname(os.path.abspath(__file__))
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
stream=sys.stdout,
level=logging.WARNING
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(
os.path.join(cwd, 'log.txt'),
maxBytes=102400
)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
logger.info("Запуск...")
def tryAgainIfFailed(func, *args, delay=5, maxRetries=5, **kwargs):
c = maxRetries
while True:
try:
return func(*args, **kwargs)
except requests.exceptions.RequestException:
time.sleep(delay)
continue
except BaseException:
if maxRetries == 0:
logger.warning("После %s попыток %s(%s%s) завершился с ошибкой.", c, func.__name__, args, kwargs)
raise Warning
logger.warning("Перезапуск %s(%s%s) через %s секунд...", func.__name__, args, kwargs, delay)
time.sleep(delay)
if maxRetries > 0:
maxRetries -= 1
continue
class customActions:
def __init__(self, vk, conn, cursor):
self.vk = vk
self.conn = conn
self.cursor = cursor
def getPeerName(self, id):
if id > 2000000000:
self.cursor.execute("""SELECT * FROM chats_cache WHERE chat_id = ?""", (id,))
fetch = self.cursor.fetchone()
if fetch is None:
try:
name = tryAgainIfFailed(
self.vk.messages.getChat,
delay=0.5,
chat_id=id-2000000000
)["title"]
self.cursor.execute("""INSERT INTO chats_cache (chat_id,chat_name) VALUES (?,?)""", (id, name,))
self.conn.commit()
except Warning:
name = "Секретный чат, используйте токен другого приложения"
else:
name = fetch[1]
elif id < 0:
self.cursor.execute("""SELECT * FROM users_cache WHERE user_id = ?""", (id,))
fetch = self.cursor.fetchone()
if fetch is None:
name = tryAgainIfFailed(
self.vk.groups.getById,
delay=0.5,
group_id=-id
)[0]['name']
self.cursor.execute("""INSERT INTO users_cache (user_id,user_name) VALUES (?,?)""", (id, name,))
self.conn.commit()
else:
name = fetch[1]
else:
self.cursor.execute("""SELECT * FROM users_cache WHERE user_id = ?""", (id,))
fetch = self.cursor.fetchone()
if fetch is None:
name = tryAgainIfFailed(
self.vk.users.get,
delay=0.5,
user_id=id
)[0]
name = f"{name['first_name']} {name['last_name']}"
self.cursor.execute("""INSERT INTO users_cache (user_id,user_name) VALUES (?,?)""", (id, name,))
self.conn.commit()
else:
name = fetch[1]
return name
def act(self, event):
#Место для своего кода
#Пример:
#if event.type == VkEventType.MESSAGE_NEW and event.message.find("echo") != -1:
# self.vk.messages.send(peer_id=event.peer_id,message=event.message.strip("echo"),random_id=random.getrandbits(64))
pass