rss.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from asyncio import get_event_loop, sleep
  2. from feedparser import parse
  3. from pyrogram import filters
  4. from pyrogram.errors import (
  5. ChannelInvalid, ChannelPrivate, InputUserDeactivated
  6. )
  7. from pyrogram.types import Message
  8. from wbb import RSS_DELAY, app, log
  9. from wbb.core.decorators.errors import capture_err
  10. from wbb.utils.dbfunctions import (
  11. add_rss_feed,
  12. get_rss_feeds,
  13. is_rss_active,
  14. remove_rss_feed,
  15. update_rss_feed,
  16. )
  17. from wbb.utils.functions import get_http_status_code, get_urls_from_text
  18. from wbb.utils.rss import Feed
  19. __MODULE__ = "RSS"
  20. __HELP__ = f"""
  21. /add_feed [URL] - Add a feed to chat
  22. /rm_feed - Remove feed from chat
  23. **Note:**
  24. - This will check for updates every {RSS_DELAY // 60} minutes.
  25. - You can only add one feed per chat.
  26. - Currently RSS and ATOM feeds are supported.
  27. """
  28. async def rss_worker():
  29. log.info("RSS Worker started")
  30. while True:
  31. feeds = await get_rss_feeds()
  32. if not feeds:
  33. await sleep(RSS_DELAY)
  34. continue
  35. loop = get_event_loop()
  36. for _feed in feeds:
  37. chat = _feed["chat_id"]
  38. try:
  39. url = _feed["url"]
  40. last_title = _feed.get("last_title")
  41. parsed = await loop.run_in_executor(None, parse, url)
  42. feed = Feed(parsed)
  43. if feed.title == last_title:
  44. continue
  45. await app.send_message(
  46. chat, feed.parsed(), disable_web_page_preview=True
  47. )
  48. await update_rss_feed(chat, feed.title)
  49. except (ChannelInvalid, ChannelPrivate, InputUserDeactivated):
  50. await remove_rss_feed(chat)
  51. log.info(f"Removed RSS Feed from {chat} (Invalid Chat)")
  52. except Exception as e:
  53. log.info(f"RSS in {chat}: {str(e)}")
  54. pass
  55. await sleep(RSS_DELAY)
  56. loop = get_event_loop()
  57. loop.create_task(rss_worker())
  58. @app.on_message(filters.command("add_feed"))
  59. @capture_err
  60. async def add_feed_func(_, m: Message):
  61. if len(m.command) != 2:
  62. return await m.reply("Read 'RSS' section in help menu.")
  63. url = m.text.split(None, 1)[1].strip()
  64. if not url:
  65. return await m.reply("[ERROR]: Invalid Argument")
  66. urls = get_urls_from_text(url)
  67. if not urls:
  68. return await m.reply("[ERROR]: Invalid URL")
  69. url = urls[0]
  70. status = await get_http_status_code(url)
  71. if status != 200:
  72. return await m.reply("[ERROR]: Invalid Url")
  73. ns = "[ERROR]: This feed isn't supported."
  74. try:
  75. loop = get_event_loop()
  76. parsed = await loop.run_in_executor(None, parse, url)
  77. feed = Feed(parsed)
  78. except Exception:
  79. return await m.reply(ns)
  80. if not feed:
  81. return await m.reply(ns)
  82. chat_id = m.chat.id
  83. if await is_rss_active(chat_id):
  84. return await m.reply("[ERROR]: You already have an RSS feed enabled.")
  85. try:
  86. await m.reply(feed.parsed(), disable_web_page_preview=True)
  87. except Exception:
  88. return await m.reply(ns)
  89. await add_rss_feed(chat_id, parsed.url, feed.title)
  90. @app.on_message(filters.command("rm_feed"))
  91. async def rm_feed_func(_, m: Message):
  92. if await is_rss_active(m.chat.id):
  93. await remove_rss_feed(m.chat.id)
  94. await m.reply("Removed RSS Feed")
  95. else:
  96. await m.reply("There are no active RSS Feeds in this chat.")