From 3def57a9334f03c9fb8877c3b11d7337c95cc12e Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Tue, 25 Feb 2025 18:15:10 -0500 Subject: [PATCH] Quick and dirty hack to check for latest post in a feed This is needed in case the feed is not in reverse chronological order, like most feeds. This needs testing still. --- discorss.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/discorss.py b/discorss.py index f0ee41f..c36b49a 100755 --- a/discorss.py +++ b/discorss.py @@ -100,16 +100,25 @@ def main(): for i, hook in enumerate(app_config["feeds"]): # Get the feed print("Parsing feed {}...".format(hook["name"])) - feed = feedparser.parse(hook["url"]) - try: - published_time = time.mktime(feed.entries[0]["published_parsed"]) - published_time = published_time + hook["offset"] - except KeyError: - published_time = now - 10 # Not sure what a sensible default here is + feeds = feedparser.parse(hook["url"]) + latest_post = [] + prev_best = 0 + for feed in feeds: + try: + published_time = time.mktime(feed["published_parsed"]) + published_time = published_time + hook["offset"] + except KeyError: + published_time = feed["published"] + print(published_time) + sys.exit(254) + if published_time > prev_best: + latest_post = feed + prev_best = published_time + else: + continue + # Hash the title of the latest post and use that to determine if it's been posted - new_hash = hashlib.sha3_512( - bytes(feed.entries[0]["title"], "utf-8") - ).hexdigest() + new_hash = hashlib.sha3_512(bytes(latest_post["title"], "utf-8")).hexdigest() try: if hook["lasthash"] != new_hash: app_config["feeds"][i]["lasthash"] = new_hash @@ -121,8 +130,8 @@ def main(): webhook = { "embeds": [ { - "title": str(feed.entries[0]["title"]), - "url": str(feed.entries[0]["link"]), + "title": str(latest_post["title"]), + "url": str(latest_post["link"]), "color": 216128, "footer": { "name": "DiscoRSS", @@ -135,7 +144,7 @@ def main(): "fields": [ { "name": "Excerpt from post:", - "value": get_description(feed), + "value": get_description(latest_post), } ], }