rss.py 3.1 KB

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