-
Notifications
You must be signed in to change notification settings - Fork 130
/
build_readme.py
208 lines (185 loc) · 6.17 KB
/
build_readme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from python_graphql_client import GraphqlClient
import feedparser
import httpx
import json
import pathlib
import re
import os
root = pathlib.Path(__file__).parent.resolve()
client = GraphqlClient(endpoint="https://api.github.com/graphql")
TOKEN = os.environ.get("SIMONW_TOKEN", "")
SKIP_REPOS = {
"playing-with-actions",
"simonw-readthedocs-experiments",
"datasette-comments",
"datasette-plot",
"datasette-write-ui",
"datasette-litestream",
"datasette-metadata-editable",
"datasette-short-links",
}
def replace_chunk(content, marker, chunk, inline=False):
r = re.compile(
r"<!\-\- {} starts \-\->.*<!\-\- {} ends \-\->".format(marker, marker),
re.DOTALL,
)
if not inline:
chunk = "\n{}\n".format(chunk)
chunk = "<!-- {} starts -->{}<!-- {} ends -->".format(marker, chunk, marker)
return r.sub(chunk, content)
GRAPHQL_SEARCH_QUERY = """
query {
search(first: 100, type:REPOSITORY, query:"is:public owner:simonw owner:dogsheep owner:datasette sort:updated", after: AFTER) {
pageInfo {
hasNextPage
endCursor
}
nodes {
__typename
... on Repository {
name
description
url
releases(orderBy: {field: CREATED_AT, direction: DESC}, first: 1) {
totalCount
nodes {
name
publishedAt
url
}
}
}
}
}
}
"""
def make_query(after_cursor=None, include_organization=False):
return GRAPHQL_SEARCH_QUERY.replace(
"AFTER", '"{}"'.format(after_cursor) if after_cursor else "null"
)
def fetch_releases(oauth_token):
repos = []
releases = []
# Skip these repos:
repo_names = set(SKIP_REPOS)
has_next_page = True
after_cursor = None
first = True
while has_next_page:
data = client.execute(
query=make_query(after_cursor, include_organization=first),
headers={"Authorization": "Bearer {}".format(oauth_token)},
)
first = False
print()
print(json.dumps(data, indent=4))
print()
repo_nodes = data["data"]["search"]["nodes"]
for repo in repo_nodes:
if repo["releases"]["totalCount"] and repo["name"] not in repo_names:
repos.append(repo)
repo_names.add(repo["name"])
releases.append(
{
"repo": repo["name"],
"repo_url": repo["url"],
"description": repo["description"],
"release": repo["releases"]["nodes"][0]["name"]
.replace(repo["name"], "")
.strip(),
"published_at": repo["releases"]["nodes"][0]["publishedAt"],
"published_day": repo["releases"]["nodes"][0][
"publishedAt"
].split("T")[0],
"url": repo["releases"]["nodes"][0]["url"],
"total_releases": repo["releases"]["totalCount"],
}
)
after_cursor = data["data"]["search"]["pageInfo"]["endCursor"]
has_next_page = after_cursor
return releases
def fetch_tils():
sql = """
select path, replace(title, '_', '\_') as title, url, topic, slug, created_utc
from til order by created_utc desc limit 6
""".strip()
return httpx.get(
"https://til.simonwillison.net/tils.json",
params={
"sql": sql,
"_shape": "array",
},
).json()
def fetch_blog_entries():
entries = feedparser.parse("https://simonwillison.net/atom/entries/")["entries"]
return [
{
"title": entry["title"],
"url": entry["link"].split("#")[0],
"published": entry["published"].split("T")[0],
}
for entry in entries
]
if __name__ == "__main__":
readme = root / "README.md"
project_releases = root / "releases.md"
releases = fetch_releases(TOKEN)
releases.sort(key=lambda r: r["published_at"], reverse=True)
md = "\n\n".join(
[
"[{repo} {release}]({url}) - {published_day}".format(**release)
for release in releases[:8]
]
)
readme_contents = readme.open().read()
rewritten = replace_chunk(readme_contents, "recent_releases", md)
# Write out full project-releases.md file
project_releases_md = "\n".join(
[
(
"* **[{repo}]({repo_url})**: [{release}]({url}) {total_releases_md}- {published_day}\n"
"<br />{description}"
).format(
total_releases_md="- ([{} releases total]({}/releases)) ".format(
release["total_releases"], release["repo_url"]
)
if release["total_releases"] > 1
else "",
**release
)
for release in releases
]
)
project_releases_content = project_releases.open().read()
project_releases_content = replace_chunk(
project_releases_content, "recent_releases", project_releases_md
)
project_releases_content = replace_chunk(
project_releases_content, "project_count", str(len(releases)), inline=True
)
project_releases_content = replace_chunk(
project_releases_content,
"releases_count",
str(sum(r["total_releases"] for r in releases)),
inline=True,
)
project_releases.open("w").write(project_releases_content)
tils = fetch_tils()
tils_md = "\n\n".join(
[
"[{title}](https://til.simonwillison.net/{topic}/{slug}) - {created_at}".format(
title=til["title"],
topic=til["topic"],
slug=til["slug"],
created_at=til["created_utc"].split("T")[0],
)
for til in tils
]
)
rewritten = replace_chunk(rewritten, "tils", tils_md)
entries = fetch_blog_entries()[:6]
entries_md = "\n\n".join(
["[{title}]({url}) - {published}".format(**entry) for entry in entries]
)
rewritten = replace_chunk(rewritten, "blog", entries_md)
readme.open("w").write(rewritten)