1
0
Fork 0

Support of QR code on setedt and url with regex

This commit is contained in:
Ethanell 2019-09-23 00:15:50 +02:00
parent 098a5b0210
commit ba8f17c77c
2 changed files with 24 additions and 4 deletions

View file

@ -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", "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_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`", "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", "setedt": "EDT set \u2705",
"getedt_err": "No EDT set ! \u274C", "getedt_err": "No EDT set ! \u274C",
"notif_event": "\uD83D\uDD14A event is coming !\n", "notif_event": "\uD83D\uDD14A event is coming !\n",

24
bot.py
View file

@ -3,6 +3,7 @@ import datetime
import hashlib import hashlib
import logging import logging
import shelve import shelve
import re
import requests import requests
from asyncio import sleep from asyncio import sleep
from os import mkdir from os import mkdir
@ -10,7 +11,7 @@ from os.path import isdir, isfile
from threading import RLock from threading import RLock
from aiogram import Bot, Dispatcher, executor, types 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 import markdown
from aiogram.utils.exceptions import MessageIsTooLong from aiogram.utils.exceptions import MessageIsTooLong
from EDTcalendar import Calendar from EDTcalendar import Calendar
@ -18,6 +19,8 @@ from EDTuser import User, KFET_URL
from lang import lang from lang import lang
from ics.parse import ParseError from ics.parse import ParseError
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
from pyzbar.pyzbar import decode
from PIL import Image
if not isdir("logs"): if not isdir("logs"):
@ -170,17 +173,34 @@ async def kfet_set(message: types.Message):
@dp.message_handler(commands="setedt") @dp.message_handler(commands="setedt")
@dp.message_handler(content_types=ContentType.PHOTO)
async def edt_set(message: types.Message): async def edt_set(message: types.Message):
user_id = str(message.from_user.id) user_id = str(message.from_user.id)
await message.chat.do(types.ChatActions.TYPING) await message.chat.do(types.ChatActions.TYPING)
logger.info(f"{message.from_user.username} do setedt command: {message.text}") logger.info(f"{message.from_user.username} do setedt command: {message.text}")
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:] resources = message.text[8:]
with dbL: with dbL:
with shelve.open("edt", writeback=True) as db: with shelve.open("edt", writeback=True) as db:
try: try:
Calendar("", int(resources)) Calendar("", int(resources))
except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError): except (ParseError, ConnectionError, InvalidSchema, MissingSchema, ValueError, UnboundLocalError):
msg = lang(db[user_id], "setedt_err_res") msg = lang(db[user_id], "setedt_err_res")
else: else:
db[user_id].resources = int(resources) db[user_id].resources = int(resources)