Commit Diff


commit - 09255143a6035fc89094fe05f35b807640cdb489
commit + 0fc414424b202ec7078b7198e6420dab05116cb9
blob - 2e3d574612108799fb9bb62c4e3b0448b7127e92
blob + 094a44912c71f2bb2f0a53bce6107146911397a9
--- lonk.py
+++ lonk.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+from datetime import datetime
 from json import loads as json_loads
 from mimetypes import guess_type
 from os import environ
@@ -216,10 +217,12 @@ class HonkUrl:
             )
         )
 
-    def do_get(self, action, page, c=None):
+    def do_get(self, action, page, c=None, after=None):
         query = {"action": action, "page": page, "token": self._token}
         if c is not None:
             query["c"] = c
+        if after is not None:
+            query["after"] = after
         with urlopen(self.build_url(path="api", query=urlencode(query)), timeout=45) as f:
             return json_loads(f.read().decode("utf8"))
 
@@ -245,7 +248,8 @@ def demo(lonk_url, honk_url):
         if honk.get("RID"):
             for honk_in_convoy in honk_url.do_get(action="gethonks", page="convoy", c=convoy)["honks"]:
                 if not honk_in_convoy.get("RID"):
-                    header = f'## From: {honk_in_convoy["Handle"]}, {honk_in_convoy["Date"]} 🧵'
+                    from_ = honk_in_convoy.get("Oondle") or honk_in_convoy["Handle"]
+                    header = f'## From: {from_}, {honk_in_convoy["Date"]} (🧵 {honk["Handle"]})'
                     collected[convoy] = (honk_in_convoy, header)
                     break
         else:
@@ -275,7 +279,7 @@ def demo(lonk_url, honk_url):
         print(_format_honk(honk, header, honk_url, fn_media_url))
 
 
-def _create_schema(db_con):
+def _create_schema(db_con, cert_hash):
     db_con.execute("""
         CREATE TABLE
             client (
@@ -283,34 +287,46 @@ def _create_schema(db_con):
                 cert_hash TEXT UNIQUE,
                 honk_url TEXT NOT NULL,
                 token TEXT NOT NULL
-            )"""
+            )
+        """
+    )
+
+    # ==========================================================================
+    # TODO: test code
+    with open(".local/settings.json") as f:
+        settings = json_loads(f.read())
+    db_con.execute(
+        "INSERT INTO client (cert_hash, honk_url, token) VALUES (?, ?, ?)",
+        (cert_hash, settings["server"], settings["token"])
     )
+    # ==========================================================================
 
 
-def db_connect():
+def db_connect(cert_hash):
     db_file_path = Path(__file__).parent / ".local" / "db"
     db_file_path.parent.mkdir(parents=True, exist_ok=True)
     db_exist = db_file_path.exists()
     db_con = sqlite3_connect(db_file_path)
     if not db_exist:
         with db_con:
-            _create_schema(db_con)
+            _create_schema(db_con, cert_hash)
     return db_con
 
 
+
 def lonk(cert_hash, lonk_url):
-    db_con = db_connect()
+    db_con = db_connect(cert_hash)
     row = db_con.execute("SELECT client_id, honk_url, token FROM client WHERE cert_hash = ?", (cert_hash, )).fetchone()
     if not row:
         print(f'30 {lonk_url.build("ask_server")}\r')
         return
     client_id, honk_url, token = row
-    return demo(lonk_url, HonkUrl(honk_url, token))
+    demo(lonk_url, HonkUrl(honk_url, token))
 
 
 def new_client_stage_1_ask_server(lonk_url):
     if not lonk_url.query:
-        print("10 Honk server URL (for example: https://honk.any-key.press)\r")
+        print("10 Honk server URL\r")
         return
     splitted = urlsplit(unquote(lonk_url.query))
     honk_url = urlunsplit((splitted.scheme, splitted.netloc, "", "", ""))
@@ -347,7 +363,7 @@ def new_client_stage_3_ask_password(cert_hash, lonk_ur
     }
     with urlopen(honk_url + "/dologin", data=urlencode(post_data).encode(), timeout=15) as f:
         token = f.read().decode("utf8")
-    db_con = db_connect()
+    db_con = db_connect(cert_hash)
     with db_con:
         db_con.execute(
             "INSERT INTO client (cert_hash, honk_url, token) VALUES (?, ?, ?)",