class DatabaseManager: cur: Connection = asyncio.get_event_loop().run_until_complete( create_pool()) async def query(self, arg, values=None): try: if values == None: await self.cur.execute(arg) else: await self.cur.execute(arg, *values) except UniqueViolationError: pass async def fetchone(self, arg, values=None): try: if values == None: row = await self.cur.fetchrow(arg) else: row = await self.cur.fetchrow(arg, *values) return row except UniqueViolationError: pass async def fetchall(self, arg, values=None): try: if values == None: row = await self.cur.fetch(arg) else: row = await self.cur.fetch(arg, *values) return row except UniqueViolationError: pass
def main(): storage = MemoryStorage() bot = Bot(token=BOT_TOKEN, parse_mode="HTML", proxy=TELEGRAM_PROXY) dp = Dispatcher(bot, storage=storage) pool = dp.loop.run_until_complete(create_pool()) db = DBCommands(pool) async def on_shutdown(dp): await bot.close() async def on_startup(dp): await asyncio.sleep(10) await create_db() await bot.send_message(ADMIN_ID, "Я запущен!") reg_handlers(dp, bot, db) executor.start_polling(dp, on_shutdown=on_shutdown, on_startup=on_startup)
env = Environment( loader = FileSystemLoader(path), autoescape = select_autoescape(['html', 'xml']) ) env.filters['datetimeformat'] = datetimeformat def datetimeformat(value, format='%H:%M / %d-%m-%Y'): date = datetime.fromtimestamp(value) return date.strftime(format) def add_static(app): PROJECT_PATH = pathlib.Path(__file__).parent app.router.add_static('/static/',PROJECT_PATH / 'static') async def init(): app = web.Application(middlewares = [logger_factory,auth_factory,response_factory]) init_jinja2() handlerframe.add_routes(app,'handlers') add_static(app) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner,'127.0.0.1',9000) await site.start() logging.info('server start at http://127.0.0.1:9000...') from config import configs loop = asyncio.get_event_loop() tasks = [init(),sql.create_pool(host = configs['db']['host'],user = configs['db']['user'],password = configs['db']['password'],db = configs['db']['database'])] loop.run_until_complete(asyncio.wait(tasks)) loop.run_forever()
import asyncio import logging from aiogram import Bot from aiogram import Dispatcher from aiogram.contrib.fsm_storage.memory import MemoryStorage from config import TOKEN from sql import create_pool # from aiogram.contrib.fsm_storage.redis import RedisStorage2 logging.basicConfig( format= u'%(filename)s [LINE:%(lineno)d] #%(levelname)-8s [%(asctime)s] %(message)s', level=logging.INFO) loop = asyncio.get_event_loop() # Set up storage (either in Redis or Memory) storage = MemoryStorage() # storage = RedisStorage2() bot = Bot(token=TOKEN, parse_mode="HTML") dp = Dispatcher(bot, storage=storage) bot.db = loop.run_until_complete(create_pool())
import asyncio import logging from aiogram import Bot from aiogram import Dispatcher from aiogram.contrib.fsm_storage.memory import MemoryStorage from config import TOKEN from sql import create_pool, create_db # from aiogram.contrib.fsm_storage.redis import RedisStorage2 logging.basicConfig( format= u'%(filename)s [LINE:%(lineno)d] #%(levelname)-8s [%(asctime)s] %(message)s', level=logging.INFO) # loop = asyncio.get_event_loop() # Поток нам не нужен, т.к. он и так создается в диспатчере. # Set up storage (either in Redis or Memory) storage = MemoryStorage() # storage = RedisStorage2() bot = Bot(token=TOKEN, parse_mode="HTML") dp = Dispatcher(bot, storage=storage) db = dp.loop.run_until_complete(create_pool())