2025-01-27 16:51:46 -05:00
|
|
|
#!/usr/bin/env python
|
2025-01-30 16:28:11 -05:00
|
|
|
# -*- coding: UTF-8 -*-
|
2025-01-27 16:51:46 -05:00
|
|
|
# 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:
|
2025-01-31 16:16:27 -05:00
|
|
|
tempStr = str(feed.entries[0]["summary_detail"]["value"])
|
2025-01-31 16:27:44 -05:00
|
|
|
desc = tempStr[:150] if len(tempStr) > 150 else tempStr
|
2025-01-27 16:51:46 -05:00
|
|
|
except KeyError:
|
2025-01-31 16:16:27 -05:00
|
|
|
tempStr = str(feed.entries[0]["description"])
|
2025-01-31 16:27:44 -05:00
|
|
|
desc = tempStr[:150] if len(tempStr) > 150 else tempStr
|
2025-01-27 16:51:46 -05:00
|
|
|
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"]:
|
2025-01-30 16:28:11 -05:00
|
|
|
# Get the feed
|
2025-01-27 16:51:46 -05:00
|
|
|
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)
|
2025-01-30 16:28:11 -05:00
|
|
|
# Generate the webhook
|
2025-01-27 16:51:46 -05:00
|
|
|
webhook = {
|
|
|
|
"embeds": [
|
|
|
|
{
|
|
|
|
"title": str(feed.entries[0]["title"]),
|
|
|
|
"url": str(feed.entries[0]["link"]),
|
2025-01-31 16:17:13 -05:00
|
|
|
"color": 5814783,
|
2025-01-31 16:28:04 -05:00
|
|
|
"provider": {
|
|
|
|
"name": "DiscoRSS",
|
|
|
|
"url": "https://git.frzn.dev/amr/discorss",
|
|
|
|
},
|
|
|
|
"author": {"name": str(hook["name"]), "url": str(hook["siteurl"])},
|
2025-01-31 16:17:13 -05:00
|
|
|
"fields": [
|
|
|
|
{
|
|
|
|
"name": str(feed.entries[0]["title"]),
|
|
|
|
"value": getDescription(feed),
|
|
|
|
}
|
|
|
|
],
|
2025-01-27 16:51:46 -05:00
|
|
|
}
|
2025-01-31 16:17:13 -05:00
|
|
|
],
|
|
|
|
"attachments": [],
|
2025-01-29 20:13:48 -05:00
|
|
|
}
|
|
|
|
customHeader = {
|
2025-01-30 16:28:30 -05:00
|
|
|
"user-agent": "DiscoRSS (https://git.frzn.dev/amr/discorss, 0.1)",
|
|
|
|
"content-type": "application/json",
|
2025-01-27 16:51:46 -05:00
|
|
|
}
|
2025-01-31 16:17:13 -05:00
|
|
|
webhookStr = json.dumps(webhook)
|
|
|
|
print(webhookStr)
|
2025-01-27 16:51:46 -05:00
|
|
|
if published_time > last_check and published_time < now:
|
2025-01-31 16:17:13 -05:00
|
|
|
r = requests.post(hook["webhook"], data=webhookStr, headers=customHeader)
|
|
|
|
app_config["lastupdate"] = now
|
|
|
|
with open(config_file_path, "w") as config_file:
|
|
|
|
json.dump(app_config, config_file, indent=4)
|
2025-01-27 16:51:46 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|