rss.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from asyncio import get_event_loop, sleep
  2. from time import time
  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,
  8. is_rss_active, remove_rss_feed,
  9. update_rss_feed)
  10. from wbb.utils.functions import (get_http_status_code,
  11. get_urls_from_text)
  12. from wbb.utils.rss import Feed
  13. __MODULE__ = "RSS"
  14. __HELP__ = f"""
  15. /add_feed [URL] - Add a feed to chat
  16. /rm_feed - Remove feed from chat
  17. **Note:**
  18. - This will check for updates every {RSS_DELAY//60} minutes.
  19. - You can only add one feed per chat.
  20. - Currently RSS and ATOM feeds are supported.
  21. """
  22. async def rss_worker():
  23. print("[INFO]: RSS WORKER STARTED")
  24. while True:
  25. t1 = time()
  26. feeds = await get_rss_feeds()
  27. if not feeds:
  28. await sleep(RSS_DELAY)
  29. continue
  30. for _feed in feeds:
  31. try:
  32. chat = _feed["chat_id"]
  33. url = _feed["url"]
  34. last_title = _feed.get("last_title")
  35. feed = Feed(url)
  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 Exception as e:
  43. print(str(e), f"RSS {chat}")
  44. pass
  45. t2 = time()
  46. if (t2 - t1) >= RSS_DELAY:
  47. continue
  48. await sleep(RSS_DELAY - (t2 - t1))
  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. feed = Feed(url)
  69. except Exception:
  70. return await m.reply(ns)
  71. if not feed:
  72. return await m.reply(ns)
  73. chat_id = m.chat.id
  74. if await is_rss_active(chat_id):
  75. return await m.reply(
  76. "[ERROR]: You already have an RSS feed enabled."
  77. )
  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, feed.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.")