Commit Diff


commit - e7690a9edcce0f8ab1de9f59b9fc0e86d3b29f2e
commit + ed872adb6974ecbf7e9f9b2bfe529274fcc376b0
blob - e3aeb0945fe0a42d28918dc2047a96c13811ac0d
blob + 32fb137f1cb25e44105534aabd1a7db4d08bcf40
--- lonk.py
+++ lonk.py
@@ -38,7 +38,7 @@ class ParagraphTag(_BaseTag):
         self.content = []
         self.footer = []
         return "\n".join([result] + footer) + "\n" if result else ""
-    
+
 
 class LinkTag(_BaseTag):
     def __init__(self, href, paragraph, tag, attrs):
@@ -331,37 +331,51 @@ def page_lonk(lonk_url, honk_url):
     gethonks_answer = honk_url.get("gethonks", page="home")
 
     timeline = {}
-    for honk in reversed(gethonks_answer["honks"]):
+    lonk_tree = {}
+    for i, honk in enumerate(reversed(gethonks_answer["honks"])):
+        timeline.setdefault(honk["Convoy"], i)
         if honk.get("RID"):
-            timeline.setdefault(honk["Convoy"], _LonkTreeItem(None))
-        else:
-            timeline[honk["Convoy"]] = _LonkTreeItem(honk)
+            lonk_tree.setdefault(honk["Convoy"], _LonkTreeItem(None))
+        else:
+            lonk_tree[honk["Convoy"]] = _LonkTreeItem(honk)
 
+    # fetch first 36 threads (without start honk)
+    sorted_ = sorted(timeline.keys(), key=lambda convoy: timeline[convoy], reverse=True)
     correction_map = {}
-    for convoy in list(reversed([convoy for convoy, item in timeline.items() if item.honk is None]))[:36]:
-        for honk in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]:
-            if honk["What"] == "honked":
-                if convoy != honk["Convoy"] and honk["Convoy"] in timeline:
+    for convoy in [convoy for convoy in sorted_ if lonk_tree[convoy].honk is None][:36]:
+        for honk in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]:
+            if not honk.get("RID"):
+                if convoy != honk["Convoy"]:
                     correction_map[convoy] = honk["Convoy"]
-                    timeline.pop(convoy)
+                    tl_weight_1 = timeline.pop(convoy)
                     convoy = honk["Convoy"]
-                if timeline[convoy].honk is None:
-                    timeline[convoy].honk = honk
+                    tl_weight_2 = timeline.get(convoy)
+                    timeline[convoy] = tl_weight_1 if tl_weight_2 is None else min(tl_weight_1, tl_weight_2)
+
+                item = lonk_tree.get(convoy)
+                if item is None:
+                    lonk_tree[convoy] = _LonkTreeItem(honk)
+                elif item.honk is None:
+                    item.honk = honk
                 break
         else:
+            # no thread start found
             timeline.pop(convoy)
 
+    # link answers to thread
     for honk in reversed(gethonks_answer.pop("honks")):
-        if not honk.get("RID"):
-            continue
-        convoy = correction_map.get(honk["Convoy"], honk["Convoy"])
-        if convoy in timeline:
-            timeline[convoy].thread.append(honk)
+        if honk.get("RID"):
+            item = lonk_tree.get(correction_map.get(honk["Convoy"], honk["Convoy"]))
+            if item is not None:
+                if item.honk is not None:
+                    item.thread.append(honk)
 
+    # build honks for page
     gethonks_answer["honks"] = []
-    for item in reversed(timeline.values()):
+    for convoy in sorted(timeline.keys(), key=lambda convoy: timeline[convoy], reverse=True):
+        item = lonk_tree[convoy]
         if item.honk is None:
-            break
+            break # first unfetched
         gethonks_answer["honks"] += list(item.iterate_honks())
 
     print_header("lonk home")
@@ -627,7 +641,7 @@ def vgi(cert_hash, raw_url):
         print("51 Not found\r")
 
 
-if __name__ == '__main__':
+def main():
     cert_hash_ = environ.get("VGI_CERT_HASH")
     if cert_hash_:
         try:
@@ -649,3 +663,7 @@ if __name__ == '__main__':
     else:
         stderr.write("Certificate required\n")
         print("60 Certificate required\r")
+
+
+if __name__ == '__main__':
+    main()