Blob


1 """RSS-to-honk bot."""
2 import dbm
3 import argparse
4 import xml.etree.ElementTree as ET
6 import requests
9 parser = argparse.ArgumentParser(prog='ronk', description='RSS-to-honk bot')
10 parser.add_argument('--db', required=True, help='path to local database file')
11 parser.add_argument('--rss', required=True, help='source RSS URL')
12 parser.add_argument('--honk', required=True, help='destination honk server URL')
13 parser.add_argument('--token', required=True, help='user token, see honk(3)')
14 args = parser.parse_args()
16 with dbm.open(args.db, 'c') as db:
17 resp = requests.get(args.rss, timeout=10)
18 resp.raise_for_status()
20 collected = []
21 for root_child in ET.fromstring(resp.text):
22 if root_child.tag == "channel":
23 for channel_child in root_child:
24 if channel_child.tag == 'item':
25 collected.insert(0, {i.tag: i.text or "" for i in channel_child})
26 break
28 honk_server = requests.Session()
29 for item in collected:
30 link = item.get('link')
31 if link and link not in db:
32 params = {"action": "honk", "token": args.token, "format": "html"}
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 resp = honk_server.post(args.honk, params=params, files={"noise": (None, noise)}, timeout=10)
40 resp.raise_for_status()
41 db[link] = resp.text or "1"