Skip to content

Commit

Permalink
Rss now supports multiple chats per subscribed feed along with sendin…
Browse files Browse the repository at this point in the history
…g to a specific topic

Topic format "{chat_id}:{topic_id}"
  • Loading branch information
Nubuki-all committed Dec 15, 2024
1 parent 4fb8cdc commit 2b1c175
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
6 changes: 2 additions & 4 deletions bot/startup/before.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@
os.mkdir("minfo/")


# shutil.copytree("qBittorrent", "qbit2/qBittorrent")

if conf.TEMP_USER:
for t in conf.TEMP_USER.split():
if t in conf.OWNER.split():
Expand Down Expand Up @@ -168,7 +166,7 @@ class EnTimer:
def __init__(self):
self.ind_pause = conf.LOCK_ON_STARTUP
self.time = 0
self.msg = None
self.msg = []

async def start(self):
asyncio.create_task(self.timer())
Expand All @@ -193,7 +191,7 @@ async def timer(self):
)
except Exception:
pass
self.msg = None
self.msg = []

def new_timer(self, new_time, lmsg=None):
if isinstance(new_time, int):
Expand Down
24 changes: 21 additions & 3 deletions bot/utils/msg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,28 @@ async def enpause(message):
await logger(Exception)


async def send_rss(msg: str, chat_id: int = None):
def get_expanded_chats(chat):
expanded_chat = []
for chat in chats:
(
expanded_chat.append(chat)
if chat
else expanded_chat.extend(conf.RSS_CHAT.split())
)
return expanded_chat


async def send_rss(msg: str, chat_ids: list = None):
try:
chat = chat_id or conf.RSS_CHAT
return await avoid_flood(tele.send_message, chat, msg)
chats = chat_ids or conf.RSS_CHAT
chats = [chats] if isinstance(chats, int) else chats #backward compatibility
for chat in get_expanded_chats(chats):
top_chat = chat.split(":")
chat, top_id = (
map(int, top_chat) if len(top_chat) > 1 else (int(top_chat[0]), None)
)
event = await avoid_flood(tele.send_message, chat, msg, reply_to=top_id)
return event
except Exception:
await logger(Exception)

Expand Down
71 changes: 52 additions & 19 deletions bot/workers/handlers/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ async def rss_editor(event, args, client):
-c (/command): command to prefix the rss link
--exf (what_to_exclude): keyword of words to fiter out*
--inf (what_to_include): keywords to include*
--chat (chat_id) chat to send rss overides RSS_CHAT pass 'default' to reset.
--chat (chat_id)* chat to send rss overides RSS_CHAT pass 'default' to reset.
-p () to pause the rss feed
-r () to resume the rss feed
--nodirect () disables rss direct
Expand All @@ -1075,6 +1075,7 @@ async def rss_editor(event, args, client):
where:
or - means either of both values
| - means and
or '.' for current chat
Returns:
success message on successfully editing the rss configuration
"""
Expand Down Expand Up @@ -1110,21 +1111,32 @@ async def rss_editor(event, args, client):
or arg.nodirect
):
return await event.reply("Please supply at least one additional arguement.")
if arg.chat and not (
arg.chat.lstrip("-").isdigit() or arg.chat.casefold() == "default"
):
return await avoid_flood(
event.reply,
f"Chat must be a Telegram chat id (with -100 if a group or channel)\nNot '{arg.chat}'",
)
if arg.chat:
for chat in arg.chat.split():
chat = chat.split(":")[0]
if not (chat.lstrip("-").isdigit() or chat.casefold() in ("default", ".")):
return await avoid_flood(
event.reply,
f"Chat must be a Telegram chat id (with -100 if a group or channel) or default\nNot '{chat}'",
)
if arg.c:
if not arg.c.startswith("/"):
return await event.reply("'-c': arguement must start with '/'")
data["command"] = arg.c
if arg.l:
data["link"] = arg.l
if arg.chat:
data["chat"] = int(arg.chat) if arg.chat.casefold() != "default" else None
_default = False
data["chat"] = []
for chat in arg.chat.split():
chat = str(event.chat.id) if chat == "." else chat
if chat.casefold() != "default":
data["chat"].append(chat)
else:
if _default:
continue
data["chat"].append(None)
_default = True
if arg.direct and arg.nodirect:
await avoid_flood(event.reply, "**Warning:** Ignoring '--direct'")
if arg.direct or arg.nodirect:
Expand Down Expand Up @@ -1190,11 +1202,13 @@ async def rss_sub(event, args, client):
Args:
-t (TITLE): New Title of the subscribed rss feed [Required]
-c (/command): command to prefix the rss link [Required]
[Optional]
--exf (what_to_exclude): keyword of words to fiter out*
--inf (what_to_include): keywords to include*
-p () to pause the rss feed
-r () to resume the rss feed
--chat (chat_id) chat to send feeds
--chat (chat_id)* chat to send feeds
--nodirect () to disable rss message getting passed directly* to bot
--direct () to enable the above
if not specified, defaults to RSS_DIRECT env.
Expand All @@ -1203,6 +1217,7 @@ async def rss_sub(event, args, client):
where:
or - means either of both values
| - means and
* --chat also accepts '.' for current chat and 'default' which is the same as not specifying; defaults to RSS_CHAT
*only leech and qbleech commands are passed
Returns:
success message on successfully editing the rss configuration
Expand All @@ -1228,23 +1243,40 @@ async def rss_sub(event, args, client):
title = arg.t
if not arg.c.startswith("/"):
return await event.reply("'-c': arguement must start with '/'")
if arg.chat and not arg.chat.lstrip("-").isdigit():
return await avoid_flood(
event.reply,
f"Chat must be a Telegram chat id (with -100 if a group or channel)\nNot '{arg.chat}'",
)
if arg.chat:
for chat in arg.chat.split():
chat = chat.split(":")[0]
if not (chat.lstrip("-").isdigit() or chat.casefold() in ("default", ".")):
return await avoid_flood(
event.reply,
f"Chat must be a Telegram chat id (with -100 if a group or channel) or default\nNot '{chat}'",
)
if arg.direct and arg.nodirect:
await avoid_flood(event.reply, "**Warning:** Ignoring '--direct'")
if _bot.rss_dict.get(title):
return await avoid_flood(
event.reply,
f"This title **{title}** has already been subscribed!. **Please choose another title!**",
)
chat = []
if arg.chat:
_current = False
_default = False
for chat_ in arg.chat.split():
if chat_ == ".":
if _current:
continue
chat_ = str(event.chat.id)
_current =True
if chat_.casefold() != "default":
chat.append(chat_)
else:
if _default:
continue
chat.append(None)
_default = True
inf_lists = []
exf_lists = []
msg = str()
if arg.chat:
arg.chat = int(arg.chat)
if arg.inf:
filters_list = arg.inf.split("|")
for x in filters_list:
Expand All @@ -1259,6 +1291,7 @@ async def rss_sub(event, args, client):
arg.direct = False
elif not arg.direct:
arg.direct = conf.RSS_DIRECT
msg = str()
try:
html = await get_html(feed_link)
rss_d = feedparse(html)
Expand All @@ -1282,7 +1315,7 @@ async def rss_sub(event, args, client):
"link": feed_link,
"last_feed": last_link,
"last_title": last_title,
"chat": arg.chat,
"chat": chat,
"command": arg.c,
"direct": arg.direct,
"inf": inf_lists,
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.3.2-beta.9
v2.4.0-beta.0

0 comments on commit 2b1c175

Please sign in to comment.