#!/usr/bin/env python # -*- coding: UTF-8 -*- # 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"]: # Get the feed 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) # Generate the webhook webhook = { "embeds": [ { "title": str(feed.entries[0]["title"]), "url": str(feed.entries[0]["link"]), "description": getDescription(feed), "provider": "DiscoRSS", } ] } customHeader = { "user-agent": "DiscoRSS (https://git.frzn.dev/amr/discorss, 0.1)", "content-type": "application/json", } if published_time > last_check and published_time < now: print(json.dumps(webhook)) r = requests.post( hook["webhook"], data=json.dumps(webhook), headers=customHeader ) print(webhook["embeds"][0]["title"]) print(r.text, r.status_code, r.json()) 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()