discorss/discorss.py
A.M. Rowsell e6c43876ac
Changed getDescription() to limit description string length
Now it will print at most 100 characters, though at some point
I might add something to break the string at the end of a sentence.
2025-01-31 16:16:27 -05:00

85 lines
2.8 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# SPDX-License-Identifier: MPL-2.0
# SPDX-FileCopyrightText: © 2025 A.M. Rowsell <https://frzn.dev/~amr>
# 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:
tempStr = str(feed.entries[0]["summary_detail"]["value"])
desc = tempStr[:100] if len(tempStr) > 100 else tempStr
except KeyError:
tempStr = str(feed.entries[0]["description"])
desc = tempStr[:100] if len(tempStr) > 100 else tempStr
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()