hash: updated hash to use URL. also added DRY_RUN option

This commit is contained in:
A.M. Rowsell 2026-04-24 10:23:18 -04:00
commit d412c1a378
Signed by: amr
GPG key ID: E0879EDBDB0CA7B1

View file

@ -26,6 +26,7 @@ import re
class Discorss:
FEED_TIMEOUT_SECONDS = 15
DRY_RUN = True
def __init__(self):
self.config_dir = os.environ.get("XDG_CONFIG_HOME")
@ -66,7 +67,6 @@ class Discorss:
self.logger.debug("Parsing feed %s...", hook["name"])
feeds = await self._fetch_feed(hook)
latest_post = None
latest_post_time = None
prev_best = 0
bad_time = False
self.logger.debug("About to sort through entries for feed %s ...", hook["name"])
@ -79,7 +79,6 @@ class Discorss:
bad_time = True
if published_time > prev_best:
latest_post = feed
latest_post_time = published_time
prev_best = published_time
if latest_post is None:
@ -91,15 +90,16 @@ class Discorss:
"Feed %s doesn't supply a published time, using updated time instead",
hook["name"],
)
# Hash the title of the latest post and use that to determine if it's been posted
self.logger.debug("Feed url is %s", latest_post["url"])
# Hash the url of the latest post and use that to determine if it's been posted
# Yes, SHA3-512 is totally unnecessary for this purpose, but I love SHA3
self.logger.debug("About to hash %s ...", latest_post["title"])
self.logger.debug("About to hash %s ...", latest_post["url"])
try:
new_hash = hashlib.sha3_512(
bytes(latest_post["title"], "utf-8") # Removed time from hash
bytes(latest_post["url"], "utf-8") # Removed time from hash
).hexdigest()
except TypeError:
self.logger.error("Title of %s isn't hashing correctly", hook["name"])
self.logger.error("URL of %s isn't hashing correctly", hook["name"])
return None
if hook.get("lasthash") == new_hash:
@ -144,7 +144,13 @@ class Discorss:
webhook_string = json.dumps(webhook)
self.logger.debug("About to run POST for %s", hook["name"])
response = await self._post_webhook(hook, webhook_string, custom_header)
if not self.DRY_RUN:
response = await self._post_webhook(hook, webhook_string, custom_header)
else:
self.logger.debug(
"Dry run, not actually posting to webhook, faking return code 200"
)
response.status_code = 200
if response.status_code not in self.success_codes:
self.logger.error(
"Error %d while trying to post %s", response.status_code, hook["name"]
@ -267,9 +273,9 @@ class Discorss:
logging.basicConfig(
filename=str(self.log_dir + self.log_file_path),
encoding="utf-8",
level=logging.ERROR,
level=logging.DEBUG,
datefmt="%m/%d/%Y %H:%M:%S",
format="%(asctime)s: %(levelname)s: %(message)s",
format="%(asctime)s -> %(levelname)s: %(message)s",
)
return