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