from hikkatl.types import Message from .. import loader, utils import aiohttp from ..inline.types import InlineCall import datetime import logging """ ███ ███ ██ ██ ██████ ██ ██ ██ ██████ ███████ ███████ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██████ ██ ██ ██ ██ ██ ███████ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ██████ ███████ ██████ ███████ ███████ CheckTime 📜 Licensed under the GNU AGPLv3 """ # meta banner: https://0x0.st/HYVT.jpg # meta desc: desc # meta developer: @BruhHikkaModules # requires: aiohttp logger = logging.getLogger(__name__) @loader.tds class CheckTime(loader.Module): """Check time in your city""" strings = { "name": "CheckTime", "right_setcity": "Is this correct? Timezone: {city}, If yes, press: ✅ Correct", "button_right_setcity": "✅ Correct", "button_wrong_setcity": "❌ Wrong", "city_set": "🌆 The default city is set!", "time": "Timezone: {}\nTime: {}", "error": "🚫 Something wrong!\nYou may have entered the wrong time zone, if you can't resolve this, contact @BruhHikkaModules in the chat room", "select_tz": "Select the time zone:", "select_info": "Select the information in the buttons:", "general_info": "🌐 General information:\n\nTime: {}\nDate: {}\nDay: {}\nTimezone: {}\nDay of the week: {}", "day_week": [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", ], "no_tz": "❌ There is no such time zone!", "widget": "Information about my time:\n\n{}", "wait_widget": "🕓 Wait for widget (1min, maybe more)", } strings_ru = { "right_setcity": "Всё верно? Часовой пояс: {city}, Если да, то нажмите: ✅ Верно", "button_right_setcity": "✅ Верно", "button_wrong_setcity": "❌ Неверно", "city_set": "🌆 Город по-улмочанию установлен!", "time": "Часовой пояс: {}\nВремя: {}", "error": "🚫 Что-то пошло не так!\nВозможно вы указали неверный часовой пояс, если вы не можете это решить, обратитесь в чат @BruhHikkaModules", "select_tz": "Выберите часовой пояс", "select_info": "Выберите информацию рассположеную в кнопках:", "general_info": "🌐 Общая информация:\n\nВремя: {}\nДата: {}\nДень: {}\nЧасовой пояс: {}\nДень недели: {}", "day_week": [ "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье", ], "no_tz": "❌ Нету такой часовой зоны!", "widget": "Информация о моём времени:\n\n{}", "wait_widget": "🕓 Подождите пока появится виджет (1min, maybe more)", "_cls_doc": "Проверьте время в вашем городе", } def __init__(self): self.config = loader.ModuleConfig( loader.ConfigValue( "city", "Europe/Moscow", lambda: "For default city", validator=loader.validators.String(), ), loader.ConfigValue("id", 0, lambda: "For widget"), ) @loader.command( ru_doc=" [Часовой пояс / Ничего] - Поставь свой город по-улмолчанию\nПример: .set_city Europe/Moscow", ) async def setcity(self, message: Message): """[Timezone / Nothing] - Set your city to default\nExample: .set_city Europe/Moscow""" args = utils.get_args_raw(message) if not args: await self.inline.form( text=self.strings["select_tz"], message=message, reply_markup=[ [ { "text": "America/Los Angeles", "callback": self._setcity, "args": ("America/Los_Angeles",), }, { "text": "Europe/Moscow", "callback": self._setcity, "args": ("Europe/Moscow",), }, { "text": "Europe/Kiyv", "callback": self._setcity, "args": ("Europe/Kiyv",), }, ] ], ) else: async with aiohttp.ClientSession() as session: async with session.get( f"http://worldtimeapi.org/api/timezone/{args}" ) as response: if response.status == 404: return await utils.answer(message, self.strings["no_tz"]) await self.inline.form( text=self.strings["right_setcity"].format(city=args), message=message, reply_markup=[ [ { "text": self.strings["button_right_setcity"], "callback": self._setcity, "args": (args,), }, { "text": self.strings["button_wrong_setcity"], "action": "close", }, ] ], ) @loader.command(ru_doc=" [Часовой пояс / Ничего] - Узнать время") async def showtime(self, message: Message): """[Timezone / Nothing] - Find out the time\nExample: .show_time Europe/Moscow""" args = utils.get_args_raw(message) default = self.config["city"] if not args: async with aiohttp.ClientSession() as session: async with session.get( f"http://worldtimeapi.org/api/timezone/{default}" ) as response: if response.status != 200: return await utils.answer(message, self.strings["error"]) data = await response.json() datetimecity = data["datetime"] abbreviation = data["abbreviation"] day_of_week = data["day_of_week"] day_of_year = data["day_of_year"] tz = data["timezone"] week_number = data["week_number"] else: async with aiohttp.ClientSession() as session: async with session.get( f"http://worldtimeapi.org/api/timezone/{args}" ) as response: if response.status != 200: return await utils.answer(message, self.strings["error"]) data = await response.json() datetimecity = data["datetime"] abbreviation = data["abbreviation"] day_of_week = data["day_of_week"] day_of_year = data["day_of_year"] tz = data["timezone"] week_number = data["week_number"] await self.inline.form( text=self.strings["select_info"], message=message, reply_markup=[ [ { "text": "🌐 General", "callback": self._general, "args": ( [ datetimecity, abbreviation, day_of_week, day_of_year, tz, week_number, ], ), }, {"text": "❌ Close", "action": "close"}, ] ], ) @loader.loop(autostart=True, interval=60) async def updwidget(self): if self.config["id"] != 0: chat_id = self.config["id"][1] message_id = self.config["id"][0] async with aiohttp.ClientSession() as session: async with session.get( f'http://worldtimeapi.org/api/timezone/{self.config["city"]}' ) as response: if response.status != 200: return await self.client.edit_message( chat_id, message_id, self.strings["error"] ) data = await response.json() datetimecity = data["datetime"] day_of_week = data["day_of_week"] day_of_year = data["day_of_year"] tz = data["timezone"] datem = datetime.datetime.fromisoformat(datetimecity) await self.client.edit_message( self.config["id"][1], self.config["id"][0], self.strings["widget"].format( self.strings["general_info"].format( datem.strftime("%H:%M"), datem.strftime("%d.%m.%Y"), day_of_year, tz, self.strings["day_week"][day_of_week - 1], ) ), ) @loader.command(ru_doc=" [Часовой пояс / Ничего] - Отправить виджет") async def send_widget(self, message: Message): """- Send widget""" self.config["id"] = [message.id, message.chat_id] await utils.answer(message, self.strings["wait_widget"]) async def _setcity(self, call: InlineCall, city: str): self.config["city"] = city await call.edit(self.strings["city_set"]) async def _nothing(self, call: InlineCall): await call.answer("It's nothing, have a nice day :)") async def _general(self, call: InlineCall, data: list): datem = datetime.datetime.fromisoformat(data[0]) await call.edit( reply_markup=[ [{"text": "⏪ Back", "callback": self._showtime_menu, "args": (data,)}] ], text=self.strings["general_info"].format( datem.strftime("%H:%M"), datem.strftime("%d.%m.%Y"), data[3], data[4], self.strings["day_week"][data[2] - 1], ), ) async def _showtime_menu(self, call: InlineCall, data: list): await call.edit( text=self.strings["select_info"], reply_markup=[ [ {"text": "🌐 General", "callback": self._general, "args": (data,)}, {"text": "❌ Close", "action": "close"}, ] ], )