From 74ae9ecb57e57c149886d544dbef3a8d51d3c343 Mon Sep 17 00:00:00 2001 From: Hikari Date: Fri, 15 Apr 2022 09:30:34 +0000 Subject: [PATCH] Add automatic database backup (`HikkaBackup`) --- hikka/modules/hikka_backup.py | 150 ++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 hikka/modules/hikka_backup.py diff --git a/hikka/modules/hikka_backup.py b/hikka/modules/hikka_backup.py new file mode 100644 index 0000000..f930798 --- /dev/null +++ b/hikka/modules/hikka_backup.py @@ -0,0 +1,150 @@ +# █ █ ▀ █▄▀ ▄▀█ █▀█ ▀ ▄▀█ ▀█▀ ▄▀█ █▀▄▀█ ▄▀█ +# █▀█ █ █ █ █▀█ █▀▄ █ ▄ █▀█ █ █▀█ █ ▀ █ █▀█ +# +# © Copyright 2022 +# +# https://t.me/hikariatama +# +# 🔒 Licensed under the GNU GPLv3 +# 🌐 https://www.gnu.org/licenses/agpl-3.0.html + +# meta developer: @hikariatama + +from .. import loader, utils +from telethon.tl.types import Message +import logging +from aiogram.types import CallbackQuery +import time +import asyncio +import io +import json +import datetime +from telethon.tl.functions.channels import ( + EditPhotoRequest +) + +import requests + +logger = logging.getLogger(__name__) + + +@loader.tds +class HikkaBackupMod(loader.Module): + """Automatic database backup""" + + strings = { + "name": "HikkaBackup", + "period": "⌚️ Hewwo! I'm Asuna - your personal backup manager. Please, select the periodicity of automatic database backups", + "saved": "✅ Backup period saved. You can re-configure it later with .set_backup_period", + "invalid_args": "🚫 Specify correct backup period in hours", + } + + async def on_unload(self) -> None: + self._task.cancel() + + async def client_ready(self, client, db) -> None: + self._db = db + self._client = client + self._me = (await client.get_me()).id + if not self.get("period"): + await self.inline.bot.send_photo( + self._me, + photo="https://i.pinimg.com/originals/94/e5/9c/94e59c1fbecd7b842d7feeecb58f8fd6.jpg", + caption=self.strings("period"), + reply_markup=self.inline._generate_markup( + utils.chunks( + [ + {"text": f"🕰 {i} h", "data": f"backup_period/{i}"} + for i in {1, 2, 4, 6, 8, 12, 24, 48, 168} + ], + 3, + ) + ), + parse_mode="HTML", + ) + + self._backup_channel, is_new = await utils.asset_channel( + self._client, + "hikka-backups", + "📼 Your database backups will appear there", + silent=True, + archive=True, + ) + + self._task = asyncio.ensure_future(self.handler()) + + if not is_new: + return + + try: + f = ( + await utils.run_sync( + requests.get, + "https://i.imgur.com/0wa2kEu.jpeg", + ) + ).content + + await self._client( + EditPhotoRequest( + channel=self._backup_channel, + photo=await self._client.upload_file(f, file_name="photo.jpg"), + ) + ) + except Exception: + pass + + async def backup_period_callback_handler(self, call: CallbackQuery) -> None: + if not call.data.startswith("backup_period"): + return + + period = int(call.data.split("/")[1]) * 60 * 60 + + self.set("period", period) + self.set("last_backup", round(time.time())) + + await call.answer(self.strings("saved"), show_alert=True) + + await self.inline.bot.delete_message( + call.message.chat.id, + call.message.message_id, + ) + + async def set_backup_periodcmd(self, message: Message) -> None: + """