mrrobot/rss.py

34 lines
1.3 KiB
Python

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'<a href="{link}">{title}</a>',
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)