hash: updated hash to use URL. also added DRY_RUN option
This commit is contained in:
parent
02aa1aa11b
commit
d412c1a378
1 changed files with 15 additions and 9 deletions
24
discorss.py
24
discorss.py
|
|
@ -26,6 +26,7 @@ import re
|
||||||
|
|
||||||
class Discorss:
|
class Discorss:
|
||||||
FEED_TIMEOUT_SECONDS = 15
|
FEED_TIMEOUT_SECONDS = 15
|
||||||
|
DRY_RUN = True
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config_dir = os.environ.get("XDG_CONFIG_HOME")
|
self.config_dir = os.environ.get("XDG_CONFIG_HOME")
|
||||||
|
|
@ -66,7 +67,6 @@ class Discorss:
|
||||||
self.logger.debug("Parsing feed %s...", hook["name"])
|
self.logger.debug("Parsing feed %s...", hook["name"])
|
||||||
feeds = await self._fetch_feed(hook)
|
feeds = await self._fetch_feed(hook)
|
||||||
latest_post = None
|
latest_post = None
|
||||||
latest_post_time = None
|
|
||||||
prev_best = 0
|
prev_best = 0
|
||||||
bad_time = False
|
bad_time = False
|
||||||
self.logger.debug("About to sort through entries for feed %s ...", hook["name"])
|
self.logger.debug("About to sort through entries for feed %s ...", hook["name"])
|
||||||
|
|
@ -79,7 +79,6 @@ class Discorss:
|
||||||
bad_time = True
|
bad_time = True
|
||||||
if published_time > prev_best:
|
if published_time > prev_best:
|
||||||
latest_post = feed
|
latest_post = feed
|
||||||
latest_post_time = published_time
|
|
||||||
prev_best = published_time
|
prev_best = published_time
|
||||||
|
|
||||||
if latest_post is None:
|
if latest_post is None:
|
||||||
|
|
@ -91,15 +90,16 @@ class Discorss:
|
||||||
"Feed %s doesn't supply a published time, using updated time instead",
|
"Feed %s doesn't supply a published time, using updated time instead",
|
||||||
hook["name"],
|
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
|
# 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:
|
try:
|
||||||
new_hash = hashlib.sha3_512(
|
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()
|
).hexdigest()
|
||||||
except TypeError:
|
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
|
return None
|
||||||
|
|
||||||
if hook.get("lasthash") == new_hash:
|
if hook.get("lasthash") == new_hash:
|
||||||
|
|
@ -144,7 +144,13 @@ class Discorss:
|
||||||
webhook_string = json.dumps(webhook)
|
webhook_string = json.dumps(webhook)
|
||||||
|
|
||||||
self.logger.debug("About to run POST for %s", hook["name"])
|
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:
|
if response.status_code not in self.success_codes:
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
"Error %d while trying to post %s", response.status_code, hook["name"]
|
"Error %d while trying to post %s", response.status_code, hook["name"]
|
||||||
|
|
@ -267,9 +273,9 @@ class Discorss:
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename=str(self.log_dir + self.log_file_path),
|
filename=str(self.log_dir + self.log_file_path),
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
level=logging.ERROR,
|
level=logging.DEBUG,
|
||||||
datefmt="%m/%d/%Y %H:%M:%S",
|
datefmt="%m/%d/%Y %H:%M:%S",
|
||||||
format="%(asctime)s: %(levelname)s: %(message)s",
|
format="%(asctime)s -> %(levelname)s: %(message)s",
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue