commit 8079f07c416fb09ae89bfca9419bc78db928ee7b Author: A.M. Rowsell Date: Mon Jan 27 16:51:46 2025 -0500 First commit of DiscoRSS project For some reason we are getting 400s from Discord. I suspect it's a Cloudflare thing. diff --git a/discorss.py b/discorss.py new file mode 100755 index 0000000..683ea55 --- /dev/null +++ b/discorss.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: © 2025 A.M. Rowsell + +# DiscoRSS: A simple RSS feed reader for Discord. Takes RSS feeds and then sends them to +# webhooks. Intended to run using systemd timers. + +import requests +import feedparser +from pathlib import Path +import json +import datetime +import time +import os + +config_file_path = r"/etc/discorss.conf" +# config_file_path = r"discorss.conf" +# log_file_path = r"/var/log/discorss" +log_file_path = r"./log" +log_file_name = r"/app.log" + + +def getDescription(feed): + try: + desc = str(feed.entries[0]["summary_detail"]["value"]) + except KeyError: + desc = str(feed.entries[0]["description"]) + return desc + + +def main(): + os.environ["TZ"] = "America/Toronto" + time.tzset() + try: + Path(log_file_path).mkdir(parents=True, exist_ok=True) + except FileExistsError: + print("This path already exists and is not a directory!") + # Load and read the config file + if not Path(config_file_path).exists(): + print("No config file! Snarf. Directories were created for you.") + return + with open(config_file_path, "r") as config_file: + app_config = json.load(config_file) + now = time.mktime(time.localtime()) + last_check = app_config["lastupdate"] + for hook in app_config["feeds"]: + feed = feedparser.parse(hook["url"]) + published_time = time.mktime(feed.entries[0]["published_parsed"]) + published_time = published_time + hook["offset"] + print(feed.entries[0]["published"], published_time, now) + webhook = { + "embeds": [ + { + "title": str(feed.entries[0]["title"]), + "url": str(feed.entries[0]["link"]), + "timestamp": str(feed.entries[0]["published"]), + "description": getDescription(feed), + "provider": "DiscoRSS", + } + ] + } + if published_time > last_check and published_time < now: + r = requests.post(hook["webhook"], json=webhook) + print(webhook["embeds"][0]["title"]) + print(r.text, r.status_code, r.headers) + app_config["lastupdate"] = now + with open(config_file_path, "w") as config_file: + json.dump(app_config, config_file, indent=4) + + return + + +if __name__ == "__main__": + main()