Skip to content

Commit

Permalink
Keep track of seen blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
raccube committed Aug 6, 2024
1 parent 3cdeca9 commit 46f7fda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/*
log.log
.env
.env
seen_posts.txt
25 changes: 15 additions & 10 deletions src/rss.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from typing import Optional
import os
from typing import List

import discord
import feedparser
Expand All @@ -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:
Expand Down

0 comments on commit 46f7fda

Please sign in to comment.