YnM AI - Szekelyhon.py Sopel plugin


from sopel import plugin, tools
import feedparser
import time
from datetime import datetime, timedelta

# Globally store the timestamp of the latest article we've posted
latest_article_timestamp = None

@plugin.interval(120*60)  # Check every 2 hours
def check_and_post_news(bot):
    global latest_article_timestamp

    now = datetime.now()
    current_hour = now.hour

    # Only check and post news between 7:00 and 20:00
    if 7 <= current_hour < 22:

        rss_feed = 'https://szekelyhon.ro/rss/szekelyhon_hirek.xml'
        feed = feedparser.parse(rss_feed)

        if feed.entries:
            new_entries = []

            for entry in feed.entries:
                # Extract the published date and time
                published = datetime(*entry.published_parsed[:6])
                if latest_article_timestamp is None or published > latest_article_timestamp:
                    new_entries.append(entry)

            if new_entries:
                # Update the latest_article_timestamp to the most recent article's timestamp
                latest_article_timestamp = max(datetime(*entry.published_parsed[:6]) for entry in new_entries)

                # Only show the latest entry
                entry = new_entries[0]
                title = entry.title
                link = entry.link
                published = entry.published

                response = f"📰: {title} - Link: {link} - Közzétéve: {published}"
                bot.say(response, "#magyar")