From 4587df372a68085a17d47d36439596a17f6748f2 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 21 Dec 2019 11:47:56 +0100 Subject: [PATCH] Add support of Tomuss --- EDTuser.py | 11 +++++++++++ Languages/en.json | 5 ++++- bot.py | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/EDTuser.py b/EDTuser.py index 52bb339..fdca167 100644 --- a/EDTuser.py +++ b/EDTuser.py @@ -1,6 +1,7 @@ import datetime import requests from EDTcalendar import Calendar +from feedparser import parse KFET_URL = "http://kfet.bdeinfo.org/orders" @@ -20,6 +21,8 @@ class User: self.nt_last = get_now() self.kfet = None self.await_cmd = str() + self.tomuss_rss = str() + self.tomuss_last = str() def calendar(self, time: str = "", pass_week: bool = False): return Calendar(time, self.resources, pass_week=pass_week) @@ -45,3 +48,11 @@ class User: res = 3 self.kfet = None if res else self.kfet return res + + def get_tomuss(self): + entry = list() + if self.tomuss_rss: + entry = [e for e in parse(self.tomuss_rss).entries] + if not self.tomuss_last: + return entry + return entry[self.tomuss_last:] diff --git a/Languages/en.json b/Languages/en.json index 406194c..e6337bc 100644 --- a/Languages/en.json +++ b/Languages/en.json @@ -19,5 +19,8 @@ "err_num": "Invalid number ! \u274C", "notif_time_cooldown": "Notification `{}` set to `{}` ! \u2705", "notif_info": "_Notification_\n*State:* {}\n*Time:* {}\n*Cooldown:* {}", - "notif_await": "Please send the new value !" + "notif_await": "Please send the new value !", + "settomuss": "Tomuss set \u2705", + "settomuss_error": "Invalid RSS feed ! \u274C", + "settomuss_wait": "Send the RSS feed link of your Tomuss !" } \ No newline at end of file diff --git a/bot.py b/bot.py index e5df0d3..ce03e26 100644 --- a/bot.py +++ b/bot.py @@ -22,6 +22,7 @@ from ics.parse import ParseError from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema from pyzbar.pyzbar import decode from PIL import Image +from feedparser import parse if not isdir("logs"): @@ -82,16 +83,18 @@ async def notif(): with shelve.open("edt", writeback=True) as db: for u in db: nt = None - kg = None + kf = None + tm = None try: nt = db[u].get_notif() kf = db[u].get_kfet() + tm = db[u].get_tomuss() except: pass if nt: await bot.send_message(int(u), lang(db[u], "notif_event")+str(nt), parse_mode=ParseMode.MARKDOWN) - if kf is not None: + if kf: if kf == 1: kf = lang(db[u], "kfet") elif kf == 2: @@ -99,6 +102,17 @@ async def notif(): else: kf = lang(db[u], "kfet_err") await bot.send_message(int(u), kf, parse_mode=ParseMode.MARKDOWN) + if tm: + for i in tm: + msg = markdown.text( + markdown.bold(i.title), + markdown.code(i.summary.replace("
", "\n").replace("", "").replace("", "")), + sep="\n" + ) + await bot.send_message(int(u), msg, parse_mode=ParseMode.MARKDOWN) + db[u].tomuss_last = i + + await sleep(60) @@ -137,6 +151,7 @@ async def start(message: types.Message): key.add(reply_keyboard.KeyboardButton("Setkfet")) key.add(reply_keyboard.KeyboardButton("Setedt")) key.add(reply_keyboard.KeyboardButton("Notif")) + key.add(reply_keyboard.KeyboardButton("Settomuss")) await message.reply(lang(user, "welcome"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) @@ -209,6 +224,17 @@ async def edt_await(message: types.Message): await message.reply(lang(db[user_id], "setedt_wait"), parse_mode=ParseMode.MARKDOWN) +@dp.message_handler(lambda msg: msg.text.lower() == "settomuss") +async def edt_await(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 settomuss") + with dbL: + with shelve.open("edt", writeback=True) as db: + db[user_id].await_cmd = "settomuss" + await message.reply(lang(db[user_id], "settomuss_wait"), parse_mode=ParseMode.MARKDOWN) + + @dp.message_handler(commands="getedt") async def edt_geturl(message: types.Message): user_id = str(message.from_user.id) @@ -303,6 +329,13 @@ async def await_cmd(message: types.message): db[user_id].kfet = int(message.text) msg = lang(db[user_id], "kfet_set") + elif db[user_id].await_cmd == "settomuss": + if not len(parse(message.text).entries): + msg = lang(db[user_id], "settomuss_error") + else: + db[user_id].tomuss_rss = message.text + msg = lang(db[user_id], "settomuss") + elif db[user_id].await_cmd in ["time", "cooldown"]: try: value = int(message.text)