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>
|
|
|
|
|
2025-01-31 23:35:02 -05:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2025-01-27 16:51:46 -05:00
|
|
|
# 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
|
2025-02-05 15:29:04 -05:00
|
|
|
import hashlib
|
2025-01-27 16:51:46 -05:00
|
|
|
from pathlib import Path
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import os
|
2025-02-03 14:34:15 -05:00
|
|
|
import re
|
2025-01-27 16:51:46 -05:00
|
|
|
|
2025-02-07 20:24:14 -05:00
|
|
|
config_dir = os.environ.get('XDG_CONFIG_HOME')
|
|
|
|
if config_dir is None:
|
|
|
|
config_file_path = r"~/.config/discorss/discorss.conf"
|
|
|
|
config_dir = r"~/.config/discorss"
|
|
|
|
else:
|
|
|
|
config_file_path = config_dir + r"/discorss/discorss.conf"
|
2025-02-03 14:34:15 -05:00
|
|
|
log_file_path = r"/var/log/discorss"
|
|
|
|
# log_file_path = r"./log"
|
2025-01-27 16:51:46 -05:00
|
|
|
log_file_name = r"/app.log"
|
2025-02-03 14:34:15 -05:00
|
|
|
# Yes, I know you "can't parse HTML with regex", but
|
|
|
|
# just watch me.
|
|
|
|
html_filter = re.compile(r"\<\/?([A-Za-z \"\=])*\>")
|
2025-02-07 20:24:14 -05:00
|
|
|
success_codes = ["200", "201", "202", "203", "204", "205", "206"]
|
2025-01-27 16:51:46 -05:00
|
|
|
|
|
|
|
|
2025-02-07 20:24:14 -05:00
|
|
|
# This function gets and formats the brief excerpt that goes in the embed
|
|
|
|
# Different feeds put summaries in different fields, so we pick the best
|
|
|
|
# one and limit it to 150 characters.
|
|
|
|
# TODO: make the character limit smarter, as to split at a natural point
|
2025-01-31 23:35:02 -05:00
|
|
|
def get_description(feed):
|
2025-01-27 16:51:46 -05:00
|
|
|
try:
|
2025-01-31 23:35:02 -05:00
|
|
|
temporary_string = str(feed.entries[0]["summary_detail"]["value"])
|
2025-02-03 14:34:15 -05:00
|
|
|
temporary_string = html_filter.sub("", temporary_string)
|
2025-01-31 23:35:02 -05:00
|
|
|
desc = (
|
2025-02-07 20:24:14 -05:00
|
|
|
temporary_string[:150]
|
|
|
|
if len(temporary_string) > 150
|
|
|
|
else temporary_string
|
2025-01-31 23:35:02 -05:00
|
|
|
)
|
2025-01-27 16:51:46 -05:00
|
|
|
except KeyError:
|
2025-01-31 23:35:02 -05:00
|
|
|
temporary_string = str(feed.entries[0]["description"])
|
2025-02-03 14:34:15 -05:00
|
|
|
temporary_string = html_filter.sub("", temporary_string)
|
2025-01-31 23:35:02 -05:00
|
|
|
desc = (
|
2025-02-07 20:24:14 -05:00
|
|
|
temporary_string[:150]
|
|
|
|
if len(temporary_string) > 150
|
|
|
|
else temporary_string
|
2025-01-31 23:35:02 -05:00
|
|
|
)
|
2025-01-27 16:51:46 -05:00
|
|
|
return desc
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
os.environ["TZ"] = "America/Toronto"
|
|
|
|
time.tzset()
|
2025-02-07 20:24:14 -05:00
|
|
|
# Check for log and config files/paths, create empty directories if needed
|
2025-01-27 16:51:46 -05:00
|
|
|
try:
|
|
|
|
Path(log_file_path).mkdir(parents=True, exist_ok=True)
|
|
|
|
except FileExistsError:
|
2025-02-07 20:24:14 -05:00
|
|
|
print("The logfile path {} already exists and is not a directory!".format(log_file_path))
|
2025-01-27 16:51:46 -05:00
|
|
|
if not Path(config_file_path).exists():
|
2025-02-07 20:24:14 -05:00
|
|
|
print("No config file at {}! Snarf.\n{} was created for you.".format(config_file_path, config_dir))
|
|
|
|
Path(config_file_path).mkdir(parents=True, exist_ok=True)
|
2025-01-27 16:51:46 -05:00
|
|
|
return
|
|
|
|
with open(config_file_path, "r") as config_file:
|
|
|
|
app_config = json.load(config_file)
|
|
|
|
now = time.mktime(time.localtime())
|
2025-02-05 23:27:49 -05:00
|
|
|
try:
|
|
|
|
last_check = app_config["lastupdate"]
|
|
|
|
except KeyError:
|
2025-02-07 20:24:14 -05:00
|
|
|
last_check = (
|
|
|
|
now - 21600
|
|
|
|
) # first run, no lastupdate, check up to 6 hours ago
|
2025-02-05 15:29:04 -05:00
|
|
|
for i, hook in enumerate(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"]
|
2025-02-03 14:34:15 -05:00
|
|
|
print("Parsing feed {}...".format(hook["name"]))
|
2025-02-05 23:28:14 -05:00
|
|
|
# Hash the title of the latest post and use that to determine if it's been posted
|
2025-02-07 20:24:14 -05:00
|
|
|
new_hash = hashlib.sha3_512(
|
|
|
|
bytes(feed.entries[0]["title"], "utf-8")
|
|
|
|
).hexdigest()
|
2025-02-05 15:29:04 -05:00
|
|
|
try:
|
|
|
|
if hook["lasthash"] != new_hash:
|
|
|
|
app_config["feeds"][i]["lasthash"] = new_hash
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
except KeyError:
|
|
|
|
app_config["feeds"][i]["lasthash"] = new_hash
|
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-02-02 11:21:52 -05:00
|
|
|
"color": 216128,
|
|
|
|
"footer": {
|
2025-01-31 16:28:04 -05:00
|
|
|
"name": "DiscoRSS",
|
2025-02-02 11:21:52 -05:00
|
|
|
# "url": "https://git.frzn.dev/amr/discorss",
|
2025-01-31 16:28:04 -05:00
|
|
|
},
|
2025-02-07 20:24:14 -05:00
|
|
|
"author": {
|
|
|
|
"name": str(hook["name"]),
|
|
|
|
"url": str(hook["siteurl"]),
|
|
|
|
},
|
2025-01-31 16:17:13 -05:00
|
|
|
"fields": [
|
|
|
|
{
|
2025-02-03 14:34:15 -05:00
|
|
|
"name": "Excerpt from post:",
|
2025-01-31 23:35:02 -05:00
|
|
|
"value": get_description(feed),
|
2025-01-31 16:17:13 -05:00
|
|
|
}
|
|
|
|
],
|
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
|
|
|
}
|
2025-01-31 23:35:02 -05:00
|
|
|
custom_header = {
|
|
|
|
"user-agent": "DiscoRSS (https://git.frzn.dev/amr/discorss, 0.2rc1)",
|
2025-01-30 16:28:30 -05:00
|
|
|
"content-type": "application/json",
|
2025-01-27 16:51:46 -05:00
|
|
|
}
|
2025-01-31 23:35:02 -05:00
|
|
|
webhook_string = json.dumps(webhook)
|
2025-02-05 23:40:27 -05:00
|
|
|
|
|
|
|
if published_time > last_check:
|
2025-01-31 23:35:02 -05:00
|
|
|
r = requests.post(
|
|
|
|
hook["webhook"], data=webhook_string, headers=custom_header
|
|
|
|
)
|
2025-02-05 15:30:34 -05:00
|
|
|
if r.status_code not in success_codes:
|
2025-02-03 14:34:15 -05:00
|
|
|
print(
|
|
|
|
"Error {} while trying to post {}".format(
|
|
|
|
r.status_code, hook["webhook"]
|
|
|
|
)
|
|
|
|
)
|
2025-01-31 16:17:13 -05:00
|
|
|
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()
|