rss.py 3.5 KB

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