rss.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """
  2. MIT License
  3. Copyright (c) present TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import asyncio
  21. from feedparser import parse
  22. from pyrogram import filters
  23. from pyrogram.errors import (
  24. ChannelInvalid, ChannelPrivate, InputUserDeactivated,
  25. UserIsBlocked
  26. )
  27. from pyrogram.types import Message
  28. from wbb import RSS_DELAY, app, log
  29. from wbb.core.decorators.errors import capture_err
  30. from wbb.utils.dbfunctions import (
  31. add_rss_feed,
  32. get_rss_feeds,
  33. is_rss_active,
  34. remove_rss_feed,
  35. update_rss_feed,
  36. )
  37. from wbb.utils.functions import get_http_status_code, get_urls_from_text
  38. from wbb.utils.rss import Feed
  39. __MODULE__ = "RSS"
  40. __HELP__ = f"""
  41. /add_feed [URL] - Add a feed to chat
  42. /rm_feed - Remove feed from chat
  43. **Note:**
  44. - This will check for updates every {RSS_DELAY // 60} minutes.
  45. - You can only add one feed per chat.
  46. - Currently RSS and ATOM feeds are supported.
  47. """
  48. async def rss_worker():
  49. log.info("RSS Worker started")
  50. while True:
  51. feeds = await get_rss_feeds()
  52. if not feeds:
  53. await asyncio.sleep(RSS_DELAY)
  54. continue
  55. loop = asyncio.get_event_loop()
  56. for _feed in feeds:
  57. chat = _feed["chat_id"]
  58. try:
  59. url = _feed["url"]
  60. last_title = _feed.get("last_title")
  61. parsed = await loop.run_in_executor(None, parse, url)
  62. feed = Feed(parsed)
  63. if feed.title == last_title:
  64. continue
  65. await app.send_message(
  66. chat, feed.parsed(), disable_web_page_preview=True
  67. )
  68. await update_rss_feed(chat, feed.title)
  69. except (
  70. ChannelInvalid, ChannelPrivate, InputUserDeactivated,
  71. UserIsBlocked):
  72. await remove_rss_feed(chat)
  73. log.info(f"Removed RSS Feed from {chat} (Invalid Chat)")
  74. except Exception as e:
  75. log.info(f"RSS in {chat}: {str(e)}")
  76. await asyncio.sleep(RSS_DELAY)
  77. loop = asyncio.get_event_loop()
  78. loop.create_task(rss_worker())
  79. @app.on_message(
  80. filters.command("add_feed")
  81. )
  82. @capture_err
  83. async def add_feed_func(_, m: Message):
  84. if len(m.command) != 2:
  85. return await m.reply("Read 'RSS' section in help menu.")
  86. url = m.text.split(None, 1)[1].strip()
  87. if not url:
  88. return await m.reply("[ERROR]: Invalid Argument")
  89. urls = get_urls_from_text(url)
  90. if not urls:
  91. return await m.reply("[ERROR]: Invalid URL")
  92. url = urls[0]
  93. status = await get_http_status_code(url)
  94. if status != 200:
  95. return await m.reply("[ERROR]: Invalid Url")
  96. ns = "[ERROR]: This feed isn't supported."
  97. try:
  98. loop = asyncio.get_event_loop()
  99. parsed = await loop.run_in_executor(None, parse, url)
  100. feed = Feed(parsed)
  101. except Exception:
  102. return await m.reply(ns)
  103. if not feed:
  104. return await m.reply(ns)
  105. chat_id = m.chat.id
  106. if await is_rss_active(chat_id):
  107. return await m.reply("[ERROR]: You already have an RSS feed enabled.")
  108. try:
  109. await m.reply(feed.parsed(), disable_web_page_preview=True)
  110. except Exception:
  111. return await m.reply(ns)
  112. await add_rss_feed(chat_id, parsed.url, feed.title)
  113. @app.on_message(
  114. filters.command("rm_feed")
  115. )
  116. async def rm_feed_func(_, m: Message):
  117. if await is_rss_active(m.chat.id):
  118. await remove_rss_feed(m.chat.id)
  119. await m.reply("Removed RSS Feed")
  120. else:
  121. await m.reply("There are no active RSS Feeds in this chat.")