diff --git a/.gitignore b/.gitignore index 15cbfe1..7686201 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/* log.log -.env \ No newline at end of file +.env +seen_posts.txt diff --git a/src/rss.py b/src/rss.py index 710bb8d..d181482 100644 --- a/src/rss.py +++ b/src/rss.py @@ -1,5 +1,6 @@ import asyncio -from typing import Optional +import os +from typing import List import discord import feedparser @@ -16,23 +17,27 @@ def get_feed_channel(bot: BotClient) -> discord.TextChannel: return channel -async def get_last_blog_post(channel: discord.TextChannel) -> str | None: - # TODO: This doesn't work when the bot is restarted, store the URL instead - last_message: Optional[discord.Message] = channel.last_message - if last_message is not None and len(last_message.embeds) > 0: - return last_message.embeds[0].url +def get_seen_posts() -> List[str]: + if os.path.exists('seen_posts.txt'): + with open('seen_posts.txt', 'r') as f: + return f.readlines() - return None + return [] + + +def add_seen_post(post_id: str) -> None: + with open('seen_posts.txt', 'a') as f: + f.write(post_id + '\n') async def check_posts(bot: BotClient): feed = feedparser.parse(FEED_URL) channel = get_feed_channel(bot) post = feed.entries[0] - newest_post_url = post.link - last_message_url = await get_last_blog_post(channel) - if newest_post_url != last_message_url: + + if post.id + "\n" not in get_seen_posts(): await channel.send(embed=create_embed(post)) + add_seen_post(post.id) def create_embed(post: FeedParserDict) -> discord.Embed: