# █ █ ▀ █▄▀ ▄▀█ █▀█ ▀ # █▀█ █ █ █ █▀█ █▀▄ █ # © Copyright 2022 # https://t.me/hikariatama # # 🔒 Licensed under the GNU AGPLv3 # 🌐 https://www.gnu.org/licenses/agpl-3.0.html # meta pic: https://static.dan.tatar/pollplot_icon.png # meta banner: https://mods.hikariatama.ru/badges/pollplot.jpg # requires: matplotlib # meta developer: @hikarimods # scope: hikka_only # scope: hikka_min 1.2.10 import io import matplotlib.pyplot as plt from telethon.tl.types import Message from .. import loader, utils @loader.tds class PollPlotMod(loader.Module): """Visualises polls as plots""" strings = { "name": "PollPlot", "no_reply": ( "🚫 Reply to a poll is" " required!" ), "no_answers": ( "😢 This poll has not" " answers yet." ), } strings_ru = { "no_reply": ( "🚫 Нужен ответ на" " опрос!" ), "no_answers": ( "😢 В этом опросе" " пока что" " нет участников." ), "_cmd_doc_plot": " - Создать визуализацию опроса", "_cls_doc": "Визуализирует опросы в виде графиков", } strings_de = { "no_reply": ( "🚫 Antwort auf eine" " Umfrage erforderlich!" ), "no_answers": ( "😢 Diese Umfrage hat" " noch" " keine Antworten." ), "_cmd_doc_plot": " - Erstelle eine Visualisierung von Umfragen", "_cls_doc": "Visualisiert Umfragen als Diagramme", } strings_hi = { "no_reply": ( "🚫 एक पोल पर जवाब आवश्यक" " है!" ), "no_answers": ( "😢 इस पोल में अभी तक कोई" " उत्तर नहीं है।" ), "_cmd_doc_plot": " - पोल को बनाने के लिए प्लॉट करें", "_cls_doc": "पोल को प्लॉट के रूप में दर्शाता है", } strings_uz = { "no_reply": ( "🚫 Javob berilgan savol" " kerak!" ), "no_answers": ( "😢 Ushbu savolda hali" " hech qanday javob yo'q." ), "_cmd_doc_plot": " - Savolni chizishga o'tkazish", "_cls_doc": "Savollarni chizishlar shaklida ko'rsatadi", } strings_tr = { "no_reply": ( "🚫 Bir anket yanıtı" " gerekli!" ), "no_answers": ( "😢 Bu anket henüz cevap" " yok." ), "_cmd_doc_plot": " - Bir anketi çizimden oluşturun", "_cls_doc": "Anketleri çizimler şeklinde gösterir", } async def plotcmd(self, message: Message): """ - Create plot from poll""" reply = await message.get_reply_message() if not reply or not getattr(reply, "poll", False): await utils.answer(message, self.strings("no_reply")) return sizes = [i.voters for i in reply.poll.results.results] if not sum(sizes): await utils.answer(message, self.strings("no_answers")) return labels = [ f"{a.text} [{sizes[i]}] ({round(sizes[i] / sum(sizes) * 100, 1)}%)" for i, a in enumerate(reply.poll.poll.answers) ] explode = [0.05] * len(sizes) fig1, ax1 = plt.subplots() ax1.pie( sizes, explode=explode, labels=labels, textprops={"color": "white", "size": "large"}, ) buf = io.BytesIO() fig1.patch.set_facecolor("#303841") fig1.savefig(buf) buf.seek(0) await self._client.send_file(message.peer_id, buf.getvalue(), reply_to=reply) if message.out: await message.delete()