-
Notifications
You must be signed in to change notification settings - Fork 31
/
bot.py
116 lines (91 loc) · 3.55 KB
/
bot.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import io
import logging
import re
import tempfile
import ujson
import aiohttp
import django.conf
import wget
from PIL import Image
from aiogram import Bot
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode, MediaGroup, InlineKeyboardMarkup, InlineKeyboardButton, ChatActions
from aiogram.utils.exceptions import *
from aiogram.utils.parts import safe_split_text, split_text, MAX_MESSAGE_LENGTH
from aiogram.utils import context
from aiovk import TokenSession, API
from aiovk.drivers import HttpDriver
from aiovk.exceptions import *
from aiovk.mixins import LimitRateDriverMixin
from config import *
django.conf.ENVIRONMENT_VARIABLE = SETTINGS_VAR
os.environ.setdefault(SETTINGS_VAR, "settings")
# Ensure settings are read
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from data.models import *
class VkSession(TokenSession):
API_VERSION = API_VERSION
class RateLimitedDriver(LimitRateDriverMixin, HttpDriver):
requests_per_period = 1
period = 0.4
DRIVERS = {}
async def get_driver(vk_token=None):
if vk_token:
if vk_token in DRIVERS:
return DRIVERS[vk_token]
else:
new_driver = RateLimitedDriver()
DRIVERS[vk_token] = new_driver
return new_driver
else:
return RateLimitedDriver()
async def get_vk_chat(cid):
return VkChat.objects.get_or_create(cid=cid)
def get_max_photo(obj, keyword='photo'):
maxarr = []
max_photo_re = re.compile(f'{keyword}_([0-9]*)')
for k, v in obj.items():
m = max_photo_re.match(k)
if m:
maxarr.append(int(m.group(1)))
if maxarr:
return keyword + '_' + str(max(maxarr))
async def get_content(url, docname='tgvkbot.document', chrome_headers=True, rewrite_name=False,
custom_ext=''):
try:
with aiohttp.ClientSession(headers=CHROME_HEADERS if chrome_headers else {}) as session:
r = await session.request('GET', url)
direct_url = str(r.url)
tempdir = tempfile.gettempdir()
filename_options = {'out': docname} if rewrite_name else {'default': docname}
if direct_url != url:
r.release()
c = await session.request('GET', direct_url)
file = wget.detect_filename(direct_url, headers=dict(c.headers), **filename_options)
temppath = os.path.join(tempdir, file + custom_ext)
with open(temppath, 'wb') as f:
f.write(await c.read())
else:
file = wget.detect_filename(direct_url, headers=dict(r.headers), **filename_options)
temppath = os.path.join(tempdir, file + custom_ext)
with open(temppath, 'wb') as f:
f.write(await r.read())
content = open(temppath, 'rb')
return {'content': content, 'file_name': file, 'custom_ext': custom_ext, 'temp_path': tempdir}
except Exception:
return {'url': url, 'docname': docname}
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot)
dp.loop.set_task_factory(context.task_factory)
import traceback
@dp.errors_handler()
async def all_errors_handler(dp, update, e):
if 'message' in dir(update) and update.message:
user = update.message.from_user.full_name
user_id = update.message.from_user.id
else:
user = update.callback_query.from_user.full_name
user_id = update.callback_query.from_user.id
logging.exception(f'The update was: {ujson.dumps(update.to_python(), indent=4)}', exc_info=True)
return True