chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

board.py (2410B)


      1 #!/usr/bin/env python3
      2 # fetch the board's posts (github discussions) into docs/board.json, run by the site workflow
      3 # usage: GITHUB_TOKEN=... tools/site/board.py > docs/board.json
      4 import json, os, sys, urllib.request
      5 
      6 REPO = ("tortr-rs", "chamesle")
      7 QUERY = """
      8 query($owner: String!, $name: String!, $after: String) {
      9   repository(owner: $owner, name: $name) {
     10     discussions(first: 100, after: $after, orderBy: {field: UPDATED_AT, direction: DESC}) {
     11       pageInfo { hasNextPage endCursor }
     12       nodes {
     13         number title createdAt updatedAt upvoteCount body
     14         author { login avatarUrl }
     15         category { name slug }
     16         reactions(content: THUMBS_UP) { totalCount }
     17         comments(first: 1) { totalCount nodes { body author { login avatarUrl } } }
     18       }
     19     }
     20   }
     21 }"""
     22 
     23 
     24 def gql(variables):
     25     req = urllib.request.Request("https://api.github.com/graphql",
     26                                  data=json.dumps({"query": QUERY, "variables": variables}).encode(),
     27                                  headers={"Authorization": "bearer " + os.environ["GITHUB_TOKEN"],
     28                                           "Content-Type": "application/json"})
     29     with urllib.request.urlopen(req) as r:
     30         return json.load(r)["data"]["repository"]["discussions"]
     31 
     32 
     33 posts, after = [], None
     34 while len(posts) < 500:
     35     page = gql({"owner": REPO[0], "name": REPO[1], "after": after})
     36     for d in page["nodes"]:
     37         author, body = d["author"] or {"login": "ghost", "avatarUrl": ""}, d["body"]
     38         comments, site = d["comments"]["totalCount"], 0
     39         # posts made from the website start as a giscus discussion: the real post is its first comment
     40         if author["login"] == "giscus" and d["comments"]["nodes"]:
     41             first = d["comments"]["nodes"][0]
     42             author, body = first["author"] or author, first["body"]
     43             comments -= 1
     44             site = 1
     45         posts.append({
     46             "n": d["number"], "t": d["title"], "c": d["category"]["slug"],
     47             "a": author["login"], "av": author["avatarUrl"],
     48             "at": d["createdAt"], "up": d["updatedAt"],
     49             "v": d["upvoteCount"] + d["reactions"]["totalCount"],
     50             "k": comments, "b": body if not site else body[:600], "s": site,
     51         })
     52     if not page["pageInfo"]["hasNextPage"]:
     53         break
     54     after = page["pageInfo"]["endCursor"]
     55 
     56 json.dump(posts, sys.stdout, separators=(",", ":"))