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:
|
|
|
|
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"]:
|
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"]),
|
|
|
|
"description": getDescription(feed),
|
|
|
|
"provider": "DiscoRSS",
|
|
|
|
}
|
2025-01-30 16:28:30 -05:00
|
|
|
]
|
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
|
|
|
}
|
|
|
|
if published_time > last_check and published_time < now:
|
2025-01-30 16:28:30 -05:00
|
|
|
print(json.dumps(webhook))
|
|
|
|
r = requests.post(
|
|
|
|
hook["webhook"], data=json.dumps(webhook), headers=customHeader
|
|
|
|
)
|
2025-01-27 16:51:46 -05:00
|
|
|
print(webhook["embeds"][0]["title"])
|
2025-01-30 16:28:30 -05:00
|
|
|
print(r.text, r.status_code, r.json())
|
2025-01-27 16:51:46 -05:00
|
|
|
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()
|