-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_readme.py
More file actions
256 lines (213 loc) · 7.29 KB
/
generate_readme.py
File metadata and controls
256 lines (213 loc) · 7.29 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
"""Generate README.md from template using GitHub API and RSS feed."""
import json
import os
import re
import sys
import urllib.request
import xml.etree.ElementTree as ET
from datetime import datetime, timezone
from pathlib import Path
GITHUB_USERNAME = "guysoft"
TEMPLATE_PATH = "templates/README.md.tpl"
CONFIG_PATH = "config.yml"
OUTPUT_PATH = "README.md"
RSS_URL = "https://guysoft.wordpress.com/feed/"
CONTRIBUTIONS_QUERY = """
query {
user(login: "%s") {
repositoriesContributedTo(
first: 7
orderBy: {field: PUSHED_AT, direction: DESC}
contributionTypes: [COMMIT, PULL_REQUEST, ISSUE]
includeUserRepositories: true
) {
nodes {
nameWithOwner
url
description
pushedAt
}
}
repositories(
first: 30
orderBy: {field: PUSHED_AT, direction: DESC}
ownerAffiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER]
) {
nodes {
nameWithOwner
url
description
latestRelease {
tagName
url
publishedAt
}
}
}
}
}
""" % GITHUB_USERNAME
def github_graphql(query):
token = os.environ.get("GITHUB_TOKEN", "")
headers = {
"Content-Type": "application/json",
"User-Agent": "readme-generator",
}
if token:
headers["Authorization"] = f"bearer {token}"
data = json.dumps({"query": query}).encode()
req = urllib.request.Request(
"https://api.github.com/graphql", data=data, headers=headers
)
with urllib.request.urlopen(req) as resp:
result = json.loads(resp.read().decode())
if "errors" in result:
print(f"GraphQL errors: {result['errors']}", file=sys.stderr)
sys.exit(1)
return result["data"]
def github_rest(endpoint):
token = os.environ.get("GITHUB_TOKEN", "")
headers = {"User-Agent": "readme-generator", "Accept": "application/vnd.github+json"}
if token:
headers["Authorization"] = f"token {token}"
req = urllib.request.Request(
f"https://api.github.com/{endpoint}", headers=headers
)
with urllib.request.urlopen(req) as resp:
return json.loads(resp.read().decode())
def humanize(iso_timestamp):
"""Convert ISO timestamp to human-readable relative time like '2 days ago'."""
if not iso_timestamp:
return "unknown"
dt = datetime.fromisoformat(iso_timestamp.replace("Z", "+00:00"))
now = datetime.now(timezone.utc)
delta = now - dt
seconds = int(delta.total_seconds())
if seconds < 60:
return "just now"
minutes = seconds // 60
if minutes < 60:
return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
hours = minutes // 60
if hours < 24:
return f"{hours} hour{'s' if hours != 1 else ''} ago"
days = hours // 24
if days < 30:
return f"{days} day{'s' if days != 1 else ''} ago"
months = days // 30
if months < 12:
return f"{months} month{'s' if months != 1 else ''} ago"
years = days // 365
return f"{years} year{'s' if years != 1 else ''} ago"
def fetch_contributions():
data = github_graphql(CONTRIBUTIONS_QUERY)
user = data["user"]
contributions = []
for repo in user["repositoriesContributedTo"]["nodes"]:
contributions.append({
"name": repo["nameWithOwner"],
"url": repo["url"],
"description": repo.get("description") or "No description",
"pushed_at": repo["pushedAt"],
})
releases = []
for repo in user["repositories"]["nodes"]:
rel = repo.get("latestRelease")
if rel:
releases.append({
"name": repo["nameWithOwner"],
"url": repo["url"],
"description": repo.get("description") or "No description",
"tag": rel["tagName"],
"release_url": rel["url"],
"published_at": rel["publishedAt"],
})
releases.sort(key=lambda r: r["published_at"] or "", reverse=True)
return contributions[:7], releases[:10]
def fetch_rss(url, count=5):
req = urllib.request.Request(url, headers={"User-Agent": "readme-generator"})
with urllib.request.urlopen(req, timeout=15) as resp:
tree = ET.parse(resp)
root = tree.getroot()
items = []
for item in root.iter("item"):
title = item.findtext("title", "")
link = item.findtext("link", "")
pub_date = item.findtext("pubDate", "")
if pub_date:
try:
from email.utils import parsedate_to_datetime
dt = parsedate_to_datetime(pub_date)
pub_iso = dt.isoformat()
except Exception:
pub_iso = pub_date
else:
pub_iso = ""
items.append({"title": title, "url": link, "published_at": pub_iso})
if len(items) >= count:
break
return items
def parse_config(path):
"""Parse the simple config.yml to extract pinned_repos list."""
repos = []
with open(path) as f:
for line in f:
line = line.strip()
if line.startswith("- "):
repos.append(line[2:].strip())
return repos
def fetch_pinned_repos(repo_slugs):
lines = []
for slug in repo_slugs:
try:
info = github_rest(f"repos/{slug}")
desc = info.get("description") or "No description"
stars = info.get("stargazers_count", 0)
lines.append(
f"- [{info['full_name']}]({info['html_url']}) - {desc} ⭐ {stars}"
)
except Exception as e:
print(f"Warning: could not fetch {slug}: {e}", file=sys.stderr)
return "\n".join(lines)
def build_contributions_section(contributions):
lines = []
for c in contributions:
lines.append(
f"- [{c['name']}]({c['url']}) - {c['description']} ({humanize(c['pushed_at'])})"
)
return "\n".join(lines)
def build_releases_section(releases):
lines = []
for r in releases:
lines.append(
f"- [{r['name']}]({r['url']}) ([{r['tag']}]({r['release_url']}), "
f"{humanize(r['published_at'])}) - {r['description']}"
)
return "\n".join(lines)
def build_blog_section(posts):
lines = []
for p in posts:
lines.append(f"- [{p['title']}]({p['url']}) ({humanize(p['published_at'])})")
return "\n".join(lines)
def main():
print("Fetching GitHub data...")
contributions, releases = fetch_contributions()
print(f" {len(contributions)} contributions, {len(releases)} releases")
print("Fetching RSS feed...")
posts = fetch_rss(RSS_URL)
print(f" {len(posts)} blog posts")
print("Fetching pinned repos...")
pinned_slugs = parse_config(CONFIG_PATH)
pinned_md = fetch_pinned_repos(pinned_slugs)
print(f" {len(pinned_slugs)} pinned repos")
template = Path(TEMPLATE_PATH).read_text()
readme = template
readme = readme.replace("<!-- PINNED_REPOS -->", pinned_md)
readme = readme.replace("<!-- CONTRIBUTIONS -->", build_contributions_section(contributions))
readme = readme.replace("<!-- RELEASES -->", build_releases_section(releases))
readme = readme.replace("<!-- BLOG_POSTS -->", build_blog_section(posts))
Path(OUTPUT_PATH).write_text(readme)
print(f"Wrote {OUTPUT_PATH}")
if __name__ == "__main__":
main()