rss.py 3.4 KB

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