From ba8f17c77c41dbd9533f264c1d931e960802d722 Mon Sep 17 00:00:00 2001 From: Ethanell Date: Mon, 23 Sep 2019 00:15:50 +0200 Subject: [PATCH] Support of QR code on setedt and url with regex --- Languages/en.json | 2 +- bot.py | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Languages/en.json b/Languages/en.json index d1b5a03..414eeb4 100644 --- a/Languages/en.json +++ b/Languages/en.json @@ -3,7 +3,7 @@ "help": "\u2139 *Commands help* \u2139\n\uD83D\uDCC5/edt, show your next events\n\uD83D\uDD14/notif, set your events notifications\n\u2699/setedt, set your calendar\n\uD83D\uDCE4\t/getedt to show your ressource", "edt_err_set": "Your EDT is not set ! \u274C\n\u2139Use /setedt to fix that", "edt_err_choice": "Invalid choice ! \u274C\n\u2139You can choose between: `day`, `next`, `week`, `next week`", - "setedt_err_res": "Invalid resources ! \u274C\n\u2139Put your resources number from a export url of your calendar", + "setedt_err_res": "Invalid resources ! \u274C\n\u2139Put your resources number or a export url/QR code of your calendar", "setedt": "EDT set \u2705", "getedt_err": "No EDT set ! \u274C", "notif_event": "\uD83D\uDD14A event is coming !\n", diff --git a/bot.py b/bot.py index a09f407..5824c6a 100644 --- a/bot.py +++ b/bot.py @@ -3,6 +3,7 @@ import datetime import hashlib import logging import shelve +import re import requests from asyncio import sleep from os import mkdir @@ -10,7 +11,7 @@ from os.path import isdir, isfile from threading import RLock from aiogram import Bot, Dispatcher, executor, types -from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle, ParseMode, reply_keyboard +from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle, ParseMode, reply_keyboard, ContentType from aiogram.utils import markdown from aiogram.utils.exceptions import MessageIsTooLong from EDTcalendar import Calendar @@ -18,6 +19,8 @@ from EDTuser import User, KFET_URL from lang import lang from ics.parse import ParseError from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema +from pyzbar.pyzbar import decode +from PIL import Image if not isdir("logs"): @@ -170,17 +173,34 @@ async def kfet_set(message: types.Message): @dp.message_handler(commands="setedt") +@dp.message_handler(content_types=ContentType.PHOTO) async def edt_set(message: types.Message): user_id = str(message.from_user.id) await message.chat.do(types.ChatActions.TYPING) logger.info(f"{message.from_user.username} do setedt command: {message.text}") - resources = message.text[8:] + + url = str() + if message.photo and message.caption == "/setedt": + file_path = await bot.get_file(message.photo[0].file_id) + file_url = f"https://api.telegram.org/file/bot{API_TOKEN}/{file_path['file_path']}" + qr = decode(Image.open(requests.get(file_url, stream=True).raw)) + if qr: + url = str(qr[0].data) + elif message.text: + msg_url = re.findall("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", message.text) + if msg_url: + url = msg_url[0] + + if url: + resources = url[url.find("resources")+10:][:4] + elif message.text: + resources = message.text[8:] with dbL: with shelve.open("edt", writeback=True) as db: try: Calendar("", int(resources)) - except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError): + except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError, UnboundLocalError): msg = lang(db[user_id], "setedt_err_res") else: db[user_id].resources = int(resources)