fork/cohost-backup

git clone https://git.julia.blue/fork/cohost-backup

Julia RyanPackage with nix and fix crash83cdd7d

main
2.4 KiB62 linesraw
1#!/usr/bin/env python3
2import json, sys
3from os import path
4from datetime import datetime
5from urllib import request
6
7def render_time(when):
8    return datetime.strptime(when, "%Y-%m-%dT%H:%M:%S.%f%z").astimezone().strftime("%F %T %Z")
9
10def render_who_when(who, when, how):
11    return f"""**{who["displayName"]} | @{who["handle"]}** {how} at {render_time(when)}:"""
12
13# the markdown is vaguely based around Zulip's, but massaged to look more like cohost's html formatting
14def render(post):
15    rendered = []
16    for block in post["content"]:
17        if "markdown" in block:
18            rendered.append(block["markdown"])
19        elif "ask" in block:
20            ask = block["ask"]
21            content, sent, who = ask["content"], ask["sentAt"], ask["who"]
22            if who.get("anon"):
23                who_when = f"anon asked at {render_time(sent)}"
24            else:
25                who_when = render_who_when(ask["who"], sent, "asked")
26            rendered.append(f"""{who_when}:
27            ```quote
28            {content}
29            ```
30            """)
31        else:
32            img = block["img"]
33            alt, url = img["altText"], img["fileURL"]
34            if url is None:
35                print("Warning: found image with missing url...")
36                continue
37            alt = alt or ""
38            basename = url.split("/")[-2] + path.splitext(url.split("/")[-1])[1]
39            dst = f"../img/{basename}"
40            if not path.exists(dst):
41                with request.urlopen(url) as download:
42                    img = download.read()
43                with open(dst, 'wb') as f:
44                    f.write(img)
45            dst = "../" + dst
46            # markdown doesn't allow newlines in image alt text
47            # TODO: figure out how to actually keep these: https://tech.lgbt/@jyn/112117398042554191
48            alt = alt.replace("\n", " ")
49            rendered.append(f"![{alt}]({dst})")
50    rendered = "\n\n".join(rendered)
51    tags = ", ".join("#" + tag for tag in post["tags"])
52    who_when = render_who_when(post["poster"], post["publishedAt"], "said")
53    return f"""{who_when}
54
55{rendered}
56
57{tags}"""
58
59for post in json.load(sys.stdin):
60    rendered = "\n\n".join([render(share) for share in post["shareTree"]] + [render(post)])
61    with open(f"""rendered/{post["filename"]}.md""", "w") as f:
62        f.write(rendered)