decentralized and customizable links page on top of atproto ligo.at
atproto link-in-bio python uv

cache profile & request reload

+31 -17
+31 -17
src/main.py
··· 1 - from flask import Flask, render_template 2 - from urllib import request 3 import json 4 5 app = Flask(__name__) 6 7 PLC_DIRECTORY = "https://plc.directory" 8 ··· 17 if handle == "favicon.ico": 18 return "not found", 404 19 20 - did = resolve_did_from_handle(handle) 21 - pds = resolve_pds_from_did(did) 22 - profile = load_profile(pds, did) 23 - links = load_links(pds, did) 24 return render_template("profile.html", profile=profile, links=links) 25 26 27 - def load_links(pds: str, did: str) -> list[dict[str, str]]: 28 response = get_record(pds, did, "one.nauta.actor.links", "self") 29 record = json.loads(response) 30 - return record["value"]["links"] 31 32 33 - def load_profile(pds: str, did: str) -> tuple[str, str]: 34 response = get_record(pds, did, "app.bsky.actor.profile", "self") 35 record = json.loads(response) 36 value: dict[str, str] = record["value"] 37 - return (value["displayName"], value["description"]) 38 - 39 - 40 - pdss: dict[str, str] = {} 41 42 43 def resolve_pds_from_did(did: str, reload: bool = False) -> str: ··· 51 pdss[did] = pds 52 app.logger.debug(f"caching pds {pds} for {did}") 53 return pds 54 - 55 - 56 - dids: dict[str, str] = {} 57 58 59 def resolve_did_from_handle(handle: str, reload: bool = False) -> str: ··· 83 84 85 def http_get(url: str) -> str: 86 - return request.urlopen(url).read()
··· 1 + from flask import Flask, render_template, request 2 + from urllib import request as http_request 3 import json 4 5 app = Flask(__name__) 6 + pdss: dict[str, str] = {} 7 + dids: dict[str, str] = {} 8 + links: dict[str, list[dict[str, str]]] = {} 9 + profiles: dict[str, tuple[str, str]] = {} 10 11 PLC_DIRECTORY = "https://plc.directory" 12 ··· 21 if handle == "favicon.ico": 22 return "not found", 404 23 24 + reload = request.args.get("reload") is not None 25 + 26 + did = resolve_did_from_handle(handle, reload=reload) 27 + pds = resolve_pds_from_did(did, reload=reload) 28 + profile = load_profile(pds, did, reload=reload) 29 + links = load_links(pds, did, reload=reload) 30 return render_template("profile.html", profile=profile, links=links) 31 32 33 + def load_links(pds: str, did: str, reload: bool = False) -> list[dict[str, str]]: 34 + if did in links and not reload: 35 + app.logger.debug(f"returning cached links for {did}") 36 + return links[did] 37 + 38 response = get_record(pds, did, "one.nauta.actor.links", "self") 39 record = json.loads(response) 40 + link = record["value"]["links"] 41 + app.logger.debug(f"caching links for {did}") 42 + links[did] = link 43 + return link 44 45 46 + def load_profile(pds: str, did: str, reload: bool = False) -> tuple[str, str]: 47 + if did in profiles and not reload: 48 + app.logger.debug(f"returning cached profile for {did}") 49 + return profiles[did] 50 + 51 response = get_record(pds, did, "app.bsky.actor.profile", "self") 52 record = json.loads(response) 53 value: dict[str, str] = record["value"] 54 + profile = (value["displayName"], value["description"]) 55 + app.logger.debug(f"caching profile for {did}") 56 + profiles[did] = profile 57 + return profile 58 59 60 def resolve_pds_from_did(did: str, reload: bool = False) -> str: ··· 68 pdss[did] = pds 69 app.logger.debug(f"caching pds {pds} for {did}") 70 return pds 71 72 73 def resolve_did_from_handle(handle: str, reload: bool = False) -> str: ··· 97 98 99 def http_get(url: str) -> str: 100 + return http_request.urlopen(url).read()