Commit Diff


commit - 9edb36c07cf7a77bbcfe84a4902fb540d5f9b989
commit + 36e13c5d16c60d74625329d9aeff83e419da318e
blob - 5b60f1e5b1c7adc21a1da48d23df35b8eb3d40c0
blob + 67a7a48deaaa2fb00495fe971ca158094acf50c1
--- ronk.py
+++ ronk.py
@@ -1,3 +1,4 @@
+"""RSS-to-honk bot."""
 import dbm
 import argparse
 import xml.etree.ElementTree as ET
@@ -5,17 +6,15 @@ import xml.etree.ElementTree as ET
 import requests
 
 
-def format_item(item):
-    link = item.get('link')
-    p = ET.Element('p')
-    a = ET.SubElement(p, 'a')
-    a.attrib.update(href=link)
-    a.text = item.get('title') or link
-    return ET.tostring(p, encoding="unicode") + item.get("description", "").strip()
+parser = argparse.ArgumentParser(prog='ronk', description='RSS-to-honk bot')
+parser.add_argument('--db', required=True, help='path to local database file')
+parser.add_argument('--rss', required=True, help='source RSS URL')
+parser.add_argument('--honk', required=True, help='destination honk server URL')
+parser.add_argument('--token', required=True, help='user token, see honk(3)')
+args = parser.parse_args()
 
-
-def main(db, rss_url, server_url, token):
-    resp = requests.get(rss_url)
+with dbm.open(args.db, 'c') as db:
+    resp = requests.get(args.rss, timeout=10)
     resp.raise_for_status()
 
     collected = []
@@ -23,25 +22,20 @@ def main(db, rss_url, server_url, token):
         if root_child.tag == "channel":
             for channel_child in root_child:
                 if channel_child.tag == 'item':
-                    collected.insert(0, {i.tag: i.text for i in channel_child})
+                    collected.insert(0, {i.tag: i.text or "" for i in channel_child})
             break
 
     honk_server = requests.Session()
     for item in collected:
-        key = item.get('link')
-        if key and key not in db:
-            params = dict(action="honk", token=token, noise=format_item(item), format="html")
-            resp = honk_server.post(server_url, params=params, timeout=10)
+        link = item.get('link')
+        if link and link not in db:
+            params = {"action": "honk", "token": args.token, "format": "html"}
+            p = ET.Element('p')
+            a = ET.SubElement(p, 'a')
+            a.attrib.update(href=link)
+            a.text = item.get('title') or link
+            params["noise"] = ET.tostring(p, encoding="unicode")
+            params["noise"] += item.get("description", "").strip()
+            resp = honk_server.post(args.honk, params=params, timeout=10)
             resp.raise_for_status()
-            db[key] = resp.text or "1"
-
-
-if __name__ == '__main__':
-    parser = argparse.ArgumentParser(prog='ronk', description='RSS-to-honk bot')
-    parser.add_argument('--db', required=True, help='path to local database file')
-    parser.add_argument('--rss', required=True, help='source RSS URL')
-    parser.add_argument('--honk', required=True, help='destination honk server URL')
-    parser.add_argument('--token', required=True, help='user token, see honk(3)')
-    args = parser.parse_args()
-    with dbm.open(args.db, 'c') as db:
-        main(db, args.rss, args.honk, args.token)
+            db[link] = resp.text or "1"