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