commit - dd301323d34fe54b42e2bd9b1a70eb5b3bf27f82
commit + 29500f51d3c9142f1a604fac0a103e29144e6966
blob - fa8564a68d1bf6620e56038a86973dad30983aa0
blob + 8d02380be94eb01fdb8f37a05aacb23ef5ec581a
--- lonk.py
+++ lonk.py
#!/usr/bin/env python3
from json import loads as json_loads
+from mimetypes import guess_type
from os import environ
from pathlib import Path
from sys import stdout
class HtmlToGmi(HTMLParser):
- def __init__(self, base_url):
+ def __init__(self, base_url, fn_media_url):
super().__init__()
self.gmi_text = []
self.stack = []
self.base_url = base_url
+ self.fn_media_url = fn_media_url
def feed(self, html_text):
super().feed(html_text)
else:
src = img.get("src")
if src:
- self.gmi_text.append(f"=> {urljoin(self.base_url, src)} {title}")
+ img_url = urljoin(self.base_url, src)
+ mime, _ = guess_type(img_url)
+ img_url = self.fn_media_url(mime, img_url)
+ self.gmi_text.append(f"=> {img_url} {title}")
elif tag == "br":
if self.stack:
self.gmi_text.append(self.stack[-1].flush())
return self.stack[0]
-def _format_honk(honk, header, lonk_url):
+def _format_honk(honk, header, fn_media_url):
assert honk["Noise"] == honk["HTML"], honk
- lines = [header, f'=> {honk["XID"]}', HtmlToGmi(_build_url()).feed(honk["HTML"])]
+ lines = [header, f'=> {honk["XID"]}', HtmlToGmi(_build_url(), fn_media_url).feed(honk["HTML"])]
for donk in (honk.get("Donks") or []):
- if environ.get("LONK_PROXY_MEDIA"):
- gemini_url = urlunsplit(
- (
- "gemini",
- lonk_url.netloc,
- lonk_url.path + "/proxy",
- urlencode({"m": donk["Media"], "u": donk["URL"]}),
- ""
- )
- )
- lines.append(f'=> {gemini_url}')
- else:
- lines.append(f'=> {donk["URL"]}')
+ lines.append(f'=> {fn_media_url(donk["Media"], donk["URL"])}')
desc = donk.get("Desc") or donk.get("Name")
if desc:
lines.append(desc)
return "\n".join(lines)
+def _proxy_url_factory(proxy_media_enabled, lonk_url):
+ def _get_proxy_url(mime, url):
+ return urlunsplit(
+ (
+ "gemini",
+ lonk_url.netloc,
+ lonk_url.path + "/proxy",
+ urlencode({"m": mime, "u": url}),
+ "",
+ )
+ ) if mime and proxy_media_enabled else url
+ return _get_proxy_url
+
+
def demo(lonk_url):
collected = {}
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, lonk_url))
+ print(_format_honk(honk, header, fn_media_url))
def proxy(mime, url):