Blob


1 """RSS-to-honk bot."""
2 import dbm
3 import xml.etree.ElementTree as ET
5 from argparse import ArgumentParser
6 from urllib.parse import urlencode
7 from urllib.request import urlopen
10 parser = ArgumentParser(prog='ronk', description='RSS-to-honk bot')
11 parser.add_argument('--db', required=True, help='path to local database file')
12 parser.add_argument('--rss', required=True, help='source RSS URL')
13 parser.add_argument('--honk', required=True, help='destination honk server URL')
14 parser.add_argument('--token', required=True, help='user token, see honk(3)')
15 args = parser.parse_args()
18 with dbm.open(args.db, 'c') as db:
19 with urlopen(args.rss, timeout=15) as response:
20 rss_content = response.read()
22 collected = []
23 for root_child in ET.fromstring(rss_content):
24 if root_child.tag == "channel":
25 for channel_child in root_child:
26 if channel_child.tag == 'item':
27 collected.insert(0, {i.tag: i.text or "" for i in channel_child})
28 break
30 for item in collected:
31 link = item.get('link')
32 if link and link not in db:
33 p = ET.Element('p')
34 a = ET.SubElement(p, 'a')
35 a.attrib.update(href=link)
36 a.text = item.get('title') or link
37 noise = ET.tostring(p, encoding="unicode")
38 noise += item.get("description", "").strip()
39 post_data = {"action": "honk", "token": args.token, "format": "html", "noise": noise}
40 with urlopen(args.honk, data=urlencode(post_data).encode(), timeout=15) as response:
41 honk_url = response.read().decode("utf8")
42 db[link] = honk_url or "1"