commit 056d37098b58e2e53ae59e8de059960b5796b3c8 from: Aleksey Ryndin date: Thu Oct 10 14:44:37 2024 UTC Add: newhonk and honkback commit - 56e61afd26ce9bf57edbf56f1f532607bbe548af commit + 056d37098b58e2e53ae59e8de059960b5796b3c8 blob - 02bc780d50d8c93d481db771edad1bbceed1d9d1 blob + e842129b7ab6db310097621fc975a31a9f92d3c4 --- lonk.py +++ lonk.py @@ -191,7 +191,7 @@ class HonkUrl: self._splitted_url = urlsplit(raw_url) self._token = token - def build_url(self, scheme=None, netloc=None, path="", query="", fragment=""): + def build(self, scheme=None, netloc=None, path="", query="", fragment=""): return urlunsplit( ( scheme or self._splitted_url.scheme, @@ -202,9 +202,9 @@ class HonkUrl: ) ) - def do_get(self, action, answer_is_json=True, **kwargs): + def get(self, action, answer_is_json=True, **kwargs): query = {**{"action": action, "token": self._token}, **kwargs} - with urlopen(self.build_url(path="api", query=urlencode(query)), timeout=45) as response: + with urlopen(self.build(path="api", query=urlencode(query)), timeout=45) as response: answer = response.read().decode("utf8") return json_loads(answer) if answer_is_json else answer @@ -336,7 +336,7 @@ class _LonkTreeItem: def page_lonk(db_con, client_id, lonk_url, honk_url): - gethonks_answer = honk_url.do_get("gethonks", page="home") + gethonks_answer = honk_url.get("gethonks", page="home") lonk_page = {} for honk in reversed(gethonks_answer.pop("honks", None) or []): @@ -386,7 +386,7 @@ def page_lonk(db_con, client_id, lonk_url, honk_url): lonk_page[convoy] = _LonkTreeItem(*row) for donk in (honk.get("Donks") or []): - donk_url = honk_url.build_url(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"] + donk_url = honk_url.build(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"] donk_mime, donk_text = donk["Media"], donk.get("Desc") or donk.get("Name") or None db_con.execute( "INSERT INTO donk (client_id, convoy_id, url, mime, alt_text) VALUES (?, ?, ?, ?, ?)", @@ -395,7 +395,7 @@ def page_lonk(db_con, client_id, lonk_url, honk_url): lonk_page[convoy].donks.append((donk_url, donk_mime, donk_text)) if honk.get("RID"): - for honk_in_convoy in honk_url.do_get("gethonks", page="convoy", c=convoy)["honks"]: + for honk_in_convoy in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]: if not honk_in_convoy.get("RID"): _save_convoy(convoy, honk_in_convoy) break @@ -423,7 +423,7 @@ def page_convoy(db_con, client_id, lonk_url, honk_url) print("51 Not found\r") return - gethonks_answer = honk_url.do_get("gethonks", page="convoy", c=query["c"]) + gethonks_answer = honk_url.get("gethonks", page="convoy", c=query["c"]) print("20 text/gemini\r") print(f"# 𝓗 onk: convoy {query['c']}\r") print("\r") @@ -436,7 +436,7 @@ def page_search(db_con, client_id, lonk_url, honk_url) return q = unquote(lonk_url.query) - gethonks_answer = honk_url.do_get("gethonks", page="search", q=q) + gethonks_answer = honk_url.get("gethonks", page="search", q=q) print("20 text/gemini\r") print(f"# 𝓗 onk: search - {q}\r") print("\r") @@ -444,7 +444,7 @@ def page_search(db_con, client_id, lonk_url, honk_url) def page_atme(db_con, client_id, lonk_url, honk_url): - gethonks_answer = honk_url.do_get("gethonks", page="atme") + gethonks_answer = honk_url.get("gethonks", page="atme") print("20 text/gemini\r") print("# 𝓗 onk: @me") print("\r") @@ -452,7 +452,7 @@ def page_atme(db_con, client_id, lonk_url, honk_url): def page_longago(db_con, client_id, lonk_url, honk_url): - gethonks_answer = honk_url.do_get("gethonks", page="longago") + gethonks_answer = honk_url.get("gethonks", page="longago") print("20 text/gemini\r") print("# 𝓗 onk: long ago") print("\r") @@ -460,7 +460,7 @@ def page_longago(db_con, client_id, lonk_url, honk_url def page_myhonks(db_con, client_id, lonk_url, honk_url): - gethonks_answer = honk_url.do_get("gethonks", page="myhonks") + gethonks_answer = honk_url.get("gethonks", page="myhonks") print("20 text/gemini\r") print("# 𝓗 onk: my honks") print("\r") @@ -473,7 +473,7 @@ def page_honker(db_con, client_id, lonk_url, honk_url) print("51 Not found\r") return - gethonks_answer = honk_url.do_get("gethonks", page="honker", xid=xid) + gethonks_answer = honk_url.get("gethonks", page="honker", xid=xid) print("20 text/gemini\r") print(f"# 𝓗 onk: honks of {xid}\r") print("\r") @@ -482,7 +482,7 @@ def page_honker(db_con, client_id, lonk_url, honk_url) def menu(lonk_url, honk_url, gethonks_answer=None): print(f"## 📝 Menu\r") - print(f"=> {honk_url.build_url(path='newhonk')} new honk\r") + print(f"=> {lonk_url.build('newhonk')} new honk\r") print(f"=> {lonk_url.build([])} lonk home\r") if gethonks_answer: @@ -491,7 +491,7 @@ def menu(lonk_url, honk_url, gethonks_answer=None): line += f' ({gethonks_answer["mecount"]})' print(line + "\r") - line = f"=> {honk_url.build_url(path='chatter')} chatter" + line = f"=> {honk_url.build(path='chatter')} chatter" if gethonks_answer["chatcount"]: line += f' ({gethonks_answer["chatcount"]})' print(line + "\r") @@ -502,10 +502,10 @@ def menu(lonk_url, honk_url, gethonks_answer=None): print(f"=> {lonk_url.build('gethonkers')} honkers\r") print(f"=> {lonk_url.build('addhonker')} add new honker\r") + def print_gethonks(gethonks_answer, lonk_url, honk_url): menu(lonk_url, honk_url, gethonks_answer) print("\r") - print("\r") for honk in gethonks_answer.get("honks") or []: convoy = honk["Convoy"] @@ -520,9 +520,9 @@ def print_gethonks(gethonks_answer, lonk_url, honk_url if re_url: lines.append(f'=> {re_url} Re: {re_url}') lines.append("") - lines.append(HtmlToGmi(honk_url.build_url(), lonk_url.media).feed(honk["HTML"])) + lines.append(HtmlToGmi(honk_url.build(), lonk_url.media).feed(honk["HTML"])) for donk in honk.get("Donks") or []: - donk_url = honk_url.build_url(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"] + donk_url = honk_url.build(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"] donk_mime, donk_text = donk["Media"], donk.get("Desc") or donk.get("Name") or None lines.append(f'=> {lonk_url.media(donk_mime, donk_url)} {donk_url}') if donk_text: @@ -530,7 +530,14 @@ def print_gethonks(gethonks_answer, lonk_url, honk_url lines.append("") if honk.get("Public"): lines.append(f'=> {lonk_url.build("bonk", urlencode({"w": honk["XID"]}))} ↺ bonk') - lines.append(f'=> {honk_url.build_url(path="newhonk", query=urlencode({"rid": honk["XID"]}))} ↱ honk back') + honk_back_url = lonk_url.build( + [ + quote(honk["Handles"] or " ", safe=""), + quote(honk["XID"], safe=""), + "honkback", + ] + ) + lines.append(f'=> {honk_back_url} ↱ honk back') for xonker in (honk.get("Honker"), honk.get("Oonker")): if xonker: lines.append(f'=> {lonk_url.build("honker", urlencode({"xid": xonker}))} honks of {xonker}') @@ -594,7 +601,7 @@ def bonk(db_con, client_id, lonk_url, honk_url): print("51 Not found\r") return - honk_url.do_get("zonkit", wherefore="bonk", what=what, answer_is_json=False) + honk_url.get("zonkit", wherefore="bonk", what=what, answer_is_json=False) print(f'30 {lonk_url.build("myhonks")}\r') @@ -606,7 +613,7 @@ def gethonkers(db_con, client_id, lonk_url, honk_url): menu(lonk_url, honk_url) print("\r") - honkers = honk_url.do_get("gethonkers").get("honkers") or [] + honkers = honk_url.get("gethonkers").get("honkers") or [] for honker in honkers: print(f'## {honker.get("Name") or honker["XID"]}\r') for field_name, display_name in zip(("Name", "XID", "Flavor"), ("name", "url", "flavor")): @@ -626,12 +633,12 @@ def gethonkers(db_con, client_id, lonk_url, honk_url): def addhonker(db_con, client_id, lonk_url, honk_url): if not lonk_url.query: - print("10 honker url (XID): \r") + print("10 honker url: \r") return url = unquote(lonk_url.query) try: - honk_url.do_get("savehonker", url=url, answer_is_json=False) + honk_url.get("savehonker", url=url, answer_is_json=False) print(f'30 {lonk_url.build("gethonkers")}\r') except HTTPError as error: print("20 text/gemini\r") @@ -650,7 +657,7 @@ def unsubscribe(db_con, client_id, lonk_url, honk_url) return url = unquote(lonk_url.query) - honk_url.do_get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False) + honk_url.get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False) print(f'30 {lonk_url.build("gethonkers")}\r') @@ -661,10 +668,33 @@ def subscribe(db_con, client_id, lonk_url, honk_url): return url = unquote(lonk_url.query) - honk_url.do_get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False) + honk_url.get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False) print(f'30 {lonk_url.build("gethonkers")}\r') +def newhonk(db_con, client_id, lonk_url, honk_url): + if not lonk_url.query: + print("10 let's make some noise: \r") + return + + noise = unquote(lonk_url.query) + honk_url.get("honk", noise=noise, answer_is_json=False) + print(f'30 {lonk_url.build("myhonks")}\r') + + +def honkback(db_con, client_id, lonk_url, honk_url): + if not lonk_url.query: + handles = unquote(lonk_url.splitted_path[-3]).strip() + rid = unquote(lonk_url.splitted_path[-2]) + print(f"10 Answer to {handles or rid}:\r") + return + + noise = unquote(lonk_url.query) + rid = unquote(lonk_url.splitted_path[-2]) + honk_url.get("honk", noise=noise, rid=rid, answer_is_json=False) + print(f'30 {lonk_url.build("myhonks")}\r') + + def authenticated(cert_hash, lonk_url, fn_impl): db_con = db_connect() row = db_con.execute("SELECT client_id, honk_url, token FROM client WHERE cert_hash=?", (cert_hash, )).fetchone() @@ -712,6 +742,10 @@ def vgi(cert_hash, raw_url): authenticated(cert_hash, lonk_url, unsubscribe) elif lonk_url.page == "subscribe": authenticated(cert_hash, lonk_url, subscribe) + elif lonk_url.page == "newhonk": + authenticated(cert_hash, lonk_url, newhonk) + elif lonk_url.page == "honkback": + authenticated(cert_hash, lonk_url, honkback) elif lonk_url.page == "ask_server": new_client_stage_1_ask_server(lonk_url) elif lonk_url.page == "ask_username":