commit - e03b8dd39559fe4a0f3416fe33ecbc0145c45ee9
commit + 09255143a6035fc89094fe05f35b807640cdb489
blob - 86ee11075e51b794dabf4d95f03481e944ed0506
blob + 2e3d574612108799fb9bb62c4e3b0448b7127e92
--- lonk.py
+++ lonk.py
from html.parser import HTMLParser
-_SETTINGS = json_loads(
- (Path(__file__).parent / ".local" / "settings.json").read_text(encoding="utf8")
-)
-assert set(_SETTINGS.keys()) == {"token", "server"}
-
-
-_BUILD_URL_DEFAULTS = urlsplit(_SETTINGS["server"])
-
-def _build_url(scheme=None, netloc=None, path="", query="", fragment=""):
- return urlunsplit(
- (
- scheme or _BUILD_URL_DEFAULTS[0],
- netloc or _BUILD_URL_DEFAULTS[1],
- path,
- query,
- fragment,
- )
- )
-
-
-def _load_from(action, page, c=None):
- query = {"action": action, "page": page, "token": _SETTINGS["token"]}
- if c is not None:
- query["c"] = c
- with urlopen(_build_url(path="api", query=urlencode(query)), timeout=45) as f:
- return json_loads(f.read().decode("utf8"))
-
-
class _BaseTag:
def __init__(self, tag, attrs):
self.tag = tag
return self.stack[0]
-def _format_honk(honk, header, fn_media_url):
+def _format_honk(honk, header, honk_url, fn_media_url):
assert honk["Noise"] == honk["HTML"], honk
- lines = [header, f'=> {honk["XID"]}', HtmlToGmi(_build_url(), fn_media_url).feed(honk["HTML"])]
+ lines = [header, f'=> {honk["XID"]}', HtmlToGmi(honk_url.build_url(), fn_media_url).feed(honk["HTML"])]
for donk in (honk.get("Donks") or []):
- donk_url = _build_url(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"]
+ donk_url = honk_url.build_url(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"]
lines.append(f'=> {fn_media_url(donk["Media"], donk_url)}')
desc = donk.get("Desc") or donk.get("Name")
if desc:
@property
def query(self):
return self._splitted_url.query
+
+
+class HonkUrl:
+ def __init__(self, raw_url, token):
+ self._splitted_url = urlsplit(raw_url)
+ self._token = token
+ def build_url(self, scheme=None, netloc=None, path="", query="", fragment=""):
+ return urlunsplit(
+ (
+ scheme or self._splitted_url.scheme,
+ netloc or self._splitted_url.netloc,
+ path,
+ query,
+ fragment,
+ )
+ )
+ def do_get(self, action, page, c=None):
+ query = {"action": action, "page": page, "token": self._token}
+ if c is not None:
+ query["c"] = c
+ with urlopen(self.build_url(path="api", query=urlencode(query)), timeout=45) as f:
+ return json_loads(f.read().decode("utf8"))
+
+
def _proxy_url_factory(proxy_media_enabled, lonk_url):
def _get_proxy_url(mime, url):
return (
return _get_proxy_url
-def demo(lonk_url):
+def demo(lonk_url, honk_url):
collected = {}
- home = _load_from(action="gethonks", page="home")
+ home = honk_url.do_get(action="gethonks", page="home")
for honk in reversed(home["honks"]):
convoy = honk["Convoy"]
if convoy in collected:
continue
if honk.get("RID"):
- for honk_in_convoy in _load_from(action="gethonks", page="convoy", c=convoy)["honks"]:
+ 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"]} ๐งต'
collected[convoy] = (honk_in_convoy, header)
print("20 text/gemini\r")
print("# ๐ onk\n")
- line = f"=> {_build_url(path='atme')} @me"
+ line = f"=> {honk_url.build_url(path='atme')} @me"
if home["mecount"]:
line += f' ({home["mecount"]})'
print(line)
- line = f"=> {_build_url(path='chatter')} chatter"
+ line = f"=> {honk_url.build_url(path='chatter')} chatter"
if home["chatcount"]:
line += f' ({home["chatcount"]})'
print(line)
fn_media_url = _proxy_url_factory(environ.get("LONK_PROXY_MEDIA"), lonk_url)
for honk, header in reversed(collected.values()):
print()
- print(_format_honk(honk, header, fn_media_url))
+ print(_format_honk(honk, header, honk_url, fn_media_url))
def _create_schema(db_con):
print(f'30 {lonk_url.build("ask_server")}\r')
return
client_id, honk_url, token = row
- raise NotImplementedError(client_id, honk_url, token)
+ return demo(lonk_url, HonkUrl(honk_url, token))
def new_client_stage_1_ask_server(lonk_url):
lonk_url = LonkUrl(input().strip())
if lonk_url.page == "lonk":
lonk(cert_hash, lonk_url)
- elif lonk_url.page == "demo":
- demo(lonk_url)
elif lonk_url.page == "ask_server":
new_client_stage_1_ask_server(lonk_url)
elif lonk_url.page == "ask_username":