diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..242f09b --- /dev/null +++ b/config.ini.example @@ -0,0 +1,8 @@ +[general] +last_run = PASTE_TIMESTAMP +token = MATRIX_TOKEN + +[feed.myfeed] +title = Feed Title +url = FEED_URL +room_id = ROOM_ID \ No newline at end of file diff --git a/rss.py b/rss.py new file mode 100644 index 0000000..f6445b9 --- /dev/null +++ b/rss.py @@ -0,0 +1,34 @@ +import xml.etree.ElementTree as ET +from urllib.request import urlopen, Request +from time import time +from datetime import datetime +from configparser import ConfigParser +from uuid import uuid4 as uuid +import json +config = ConfigParser() +config.read("config.ini") +now = int(config["general"]["last_run"]) +for f in config.sections(): + if not "feed." in f: continue + with urlopen(config[f]["url"]) as r: + r = ET.parse(r) + for i in r.findall("./channel/item"): + date = i.find("pubDate").text + date = datetime.strptime(date, "%a, %d %b %Y %H:%M:%S %z") + date = datetime.timestamp(date) + date = int(date) + if date > now: + title = i.find("title").text + link = i.find("link").text + data = json.dumps( + dict(msgtype="m.text", + body=f"[{title}]({link})", + formatted_body=f'{title}', + format="org.matrix.custom.html" + )) + req = Request(f"https://fb.doesnm.cc/_matrix/client/v3/rooms/{config[f]['room_id']}/send/m.room.message/{uuid()}?access_token={config['general']['token']}",data.encode("ascii"),method="PUT") # TODO: homeserver config lol + with urlopen(req) as r: + pass + config['general']['last_run'] = str(int(time())) + with open("config.ini","w") as f: + config.write(f) \ No newline at end of file