rss.py 3.3 KB

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