Compare commits
No commits in common. "63300e6012983e271f7006f3aa761eb6ced4ea14" and "02aa1aa11b369387b70ff3e46cd9cfae1b608a91" have entirely different histories.
63300e6012
...
02aa1aa11b
1 changed files with 23 additions and 82 deletions
105
discorss.py
105
discorss.py
|
|
@ -22,34 +22,21 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
from types import SimpleNamespace
|
|
||||||
|
|
||||||
|
|
||||||
class Discorss:
|
class Discorss:
|
||||||
FEED_TIMEOUT_SECONDS = 15
|
FEED_TIMEOUT_SECONDS = 15
|
||||||
HASH_HISTORY_LIMIT = 10
|
|
||||||
|
|
||||||
def __init__(self, args=None):
|
def __init__(self):
|
||||||
if args is None:
|
|
||||||
args = SimpleNamespace(
|
|
||||||
dry_run=False,
|
|
||||||
config_file=None,
|
|
||||||
log_file=None,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.DRY_RUN = args.dry_run
|
|
||||||
self.config_dir = os.environ.get("XDG_CONFIG_HOME")
|
self.config_dir = os.environ.get("XDG_CONFIG_HOME")
|
||||||
home_dir = Path.home()
|
home_dir = Path.home()
|
||||||
if self.config_dir is None:
|
if self.config_dir is None:
|
||||||
default_config_file_path = str(home_dir) + "/.config/discorss/discorss.conf"
|
self.config_file_path = str(home_dir) + "/.config/discorss/discorss.conf"
|
||||||
|
self.config_dir = str(home_dir) + "/.config/discorss"
|
||||||
else:
|
else:
|
||||||
default_config_file_path = self.config_dir + r"/discorss/discorss.conf"
|
self.config_file_path = self.config_dir + r"/discorss/discorss.conf"
|
||||||
self.config_file_path = args.config_file or default_config_file_path
|
self.log_dir = r"/var/log/discorss"
|
||||||
self.config_dir = str(Path(self.config_file_path).parent)
|
self.log_file_path = r"/app.log"
|
||||||
|
|
||||||
default_log_file_path = "/var/log/discorss/app.log"
|
|
||||||
self.log_file_path = args.log_file or default_log_file_path
|
|
||||||
self.log_dir = str(Path(self.log_file_path).parent)
|
|
||||||
# Yes, I know you "can't parse HTML with regex", but
|
# Yes, I know you "can't parse HTML with regex", but
|
||||||
# just watch me.
|
# just watch me.
|
||||||
self.html_filter = re.compile(r"\<\/?([A-Za-z0-9 \:\.\-\/\"\=])*\>")
|
self.html_filter = re.compile(r"\<\/?([A-Za-z0-9 \:\.\-\/\"\=])*\>")
|
||||||
|
|
@ -75,24 +62,11 @@ class Discorss:
|
||||||
timeout=self.FEED_TIMEOUT_SECONDS,
|
timeout=self.FEED_TIMEOUT_SECONDS,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_hash_history(self, hook):
|
|
||||||
# now we store a list of hashes 10 long
|
|
||||||
# this function checks if it's the old format and updates it if needed
|
|
||||||
existing_hashes = hook.get("lasthash", [])
|
|
||||||
if isinstance(existing_hashes, str):
|
|
||||||
return [existing_hashes]
|
|
||||||
if isinstance(existing_hashes, list):
|
|
||||||
return [
|
|
||||||
saved_hash
|
|
||||||
for saved_hash in existing_hashes
|
|
||||||
if isinstance(saved_hash, str)
|
|
||||||
]
|
|
||||||
return []
|
|
||||||
|
|
||||||
async def _process_feed(self, hook, last_check):
|
async def _process_feed(self, hook, last_check):
|
||||||
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"])
|
||||||
|
|
@ -105,6 +79,7 @@ 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:
|
||||||
|
|
@ -116,18 +91,18 @@ 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 url of the latest post and use that to determine if it's been posted
|
# Hash the title 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["link"])
|
self.logger.debug("About to hash %s ...", latest_post["title"])
|
||||||
try:
|
try:
|
||||||
new_hash = hashlib.sha3_512(
|
new_hash = hashlib.sha3_512(
|
||||||
bytes(latest_post["link"], "utf-8") # Removed time from hash
|
bytes(latest_post["title"], "utf-8") # Removed time from hash
|
||||||
).hexdigest()
|
).hexdigest()
|
||||||
except TypeError:
|
except TypeError:
|
||||||
self.logger.error("URL %s isn't hashing correctly", hook["link"])
|
self.logger.error("Title of %s isn't hashing correctly", hook["name"])
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if new_hash in self._get_hash_history(hook):
|
if hook.get("lasthash") == new_hash:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Generate the webhook
|
# Generate the webhook
|
||||||
|
|
@ -169,13 +144,7 @@ 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"])
|
||||||
if not self.DRY_RUN:
|
response = await self._post_webhook(hook, webhook_string, custom_header)
|
||||||
response = await self._post_webhook(hook, webhook_string, custom_header)
|
|
||||||
else:
|
|
||||||
self.logger.info(
|
|
||||||
"Dry run, not actually posting to webhook, faking return code 200"
|
|
||||||
)
|
|
||||||
response = SimpleNamespace(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"]
|
||||||
|
|
@ -199,14 +168,14 @@ class Discorss:
|
||||||
for i, result in enumerate(results):
|
for i, result in enumerate(results):
|
||||||
hook = self.app_config["feeds"][i]
|
hook = self.app_config["feeds"][i]
|
||||||
if isinstance(result, asyncio.TimeoutError):
|
if isinstance(result, asyncio.TimeoutError):
|
||||||
self.logger.critical(
|
self.logger.error(
|
||||||
"Timed out processing feed %s after %d seconds",
|
"Timed out processing feed %s after %d seconds",
|
||||||
hook["name"],
|
hook["name"],
|
||||||
self.FEED_TIMEOUT_SECONDS,
|
self.FEED_TIMEOUT_SECONDS,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
if isinstance(result, requests.RequestException):
|
if isinstance(result, requests.RequestException):
|
||||||
self.logger.critical(
|
self.logger.error(
|
||||||
"Network error while processing feed %s: %s",
|
"Network error while processing feed %s: %s",
|
||||||
hook["name"],
|
hook["name"],
|
||||||
result,
|
result,
|
||||||
|
|
@ -222,14 +191,10 @@ class Discorss:
|
||||||
if result is None:
|
if result is None:
|
||||||
continue
|
continue
|
||||||
if "lasthash" not in hook:
|
if "lasthash" not in hook:
|
||||||
self.logger.debug(
|
self.logger.info(
|
||||||
"Feed %s has no existing hash, likely a new feed!", hook["name"]
|
"Feed %s has no existing hash, likely a new feed!", hook["name"]
|
||||||
)
|
)
|
||||||
hash_history = self._get_hash_history(hook)
|
self.app_config["feeds"][i]["lasthash"] = result
|
||||||
hash_history.append(result)
|
|
||||||
if len(hash_history) > self.HASH_HISTORY_LIMIT:
|
|
||||||
hash_history = hash_history[-self.HASH_HISTORY_LIMIT :]
|
|
||||||
self.app_config["feeds"][i]["lasthash"] = hash_history
|
|
||||||
|
|
||||||
# This function gets and formats the brief excerpt that goes in the embed
|
# This function gets and formats the brief excerpt that goes in the embed
|
||||||
# Different feeds put summaries in different fields, so we pick the best
|
# Different feeds put summaries in different fields, so we pick the best
|
||||||
|
|
@ -300,13 +265,12 @@ class Discorss:
|
||||||
# Set up logging
|
# Set up logging
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename=self.log_file_path,
|
filename=str(self.log_dir + self.log_file_path),
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
level=logging.INFO,
|
level=logging.ERROR,
|
||||||
datefmt="%m/%d/%Y %H:%M:%S",
|
datefmt="%m/%d/%Y %H:%M:%S",
|
||||||
format="%(asctime)s [%(threadName)s] -> %(levelname)s: %(message)s",
|
format="%(asctime)s: %(levelname)s: %(message)s",
|
||||||
)
|
)
|
||||||
self.logger.info("========= Started discorss.py ==========")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
|
|
@ -324,7 +288,7 @@ class Discorss:
|
||||||
self.app_config["lastupdate"] = self.now
|
self.app_config["lastupdate"] = self.now
|
||||||
with open(self.config_file_path, "w") as config_file:
|
with open(self.config_file_path, "w") as config_file:
|
||||||
json.dump(self.app_config, config_file, indent=4)
|
json.dump(self.app_config, config_file, indent=4)
|
||||||
self.logger.info("========= Ended discord.py =========")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -332,30 +296,7 @@ class Discorss:
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
app = Discorss()
|
||||||
description="DiscoRSS: publish feed updates to Discord webhooks."
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-d",
|
|
||||||
"--dry-run",
|
|
||||||
action="store_true",
|
|
||||||
help="Parse feeds and update state without posting to Discord.",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-c",
|
|
||||||
"--config-file",
|
|
||||||
default=None,
|
|
||||||
help="Alternate config file path. Defaults to the existing config location.",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-l",
|
|
||||||
"--log-file",
|
|
||||||
default=None,
|
|
||||||
help="Alternate log file path. Defaults to the existing log location.",
|
|
||||||
)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
app = Discorss(args)
|
|
||||||
app.process()
|
app.process()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue