Repository navigation
Expand file tree
/
Copy pathfetch_github_stats.py
More file actions
executable file
·141 lines (129 loc) · 4.99 KB
/
Copy pathfetch_github_stats.py
File metadata and controls
executable file
·141 lines (129 loc) · 4.99 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
#!/usr/bin/env python3
import json
import os
import re
from pathlib import Path
import httpx
# requires a GitHub token with the necessary permissions read:org and read:user
# call with `GITHUB_TOKEN=ghp_XXX ./fetch_github_stats.py`
HEADERS = {
'Authorization': f'token {os.getenv("GITHUB_TOKEN")}',
'Accept': 'application/vnd.github.v3+json',
}
response = httpx.post(
'https://api.github.com/graphql',
json={
'query': '''
query($organization: String!) {
organization(login: $organization) {
sponsorshipsAsMaintainer(first: 100, includePrivate: true) {
nodes {
sponsorEntity {
... on User {
login
name
url
avatarUrl
}
... on Organization {
login
name
url
avatarUrl
}
}
tier {
monthlyPriceInDollars
name
isOneTime
}
createdAt
}
}
}
}
''',
'variables': {'organization': 'zauberzeug'},
},
headers=HEADERS,
timeout=10.0,
)
response.raise_for_status()
data = response.json()
if 'errors' in data:
raise RuntimeError(f'GitHub API Error: {data["errors"]}')
sponsors = []
for sponsor in data['data']['organization']['sponsorshipsAsMaintainer']['nodes']:
sponsor_entity = sponsor['sponsorEntity']
tier = sponsor['tier']
sponsors.append({
'login': sponsor_entity['login'],
'name': sponsor_entity['name'],
'url': sponsor_entity['url'],
'avatar_url': sponsor_entity['avatarUrl'],
'tier_name': tier['name'],
'tier_amount': tier['monthlyPriceInDollars'],
'tier_is_one_time': tier['isOneTime'],
'created_at': sponsor['createdAt'],
})
sponsors.sort(key=lambda s: s['created_at'])
contributors = []
page = 1
while True:
contributors_response = httpx.get(
f'https://api.github.com/repos/zauberzeug/nicegui/contributors?page={page}&per_page=100',
headers=HEADERS,
timeout=10.0,
)
contributors_response.raise_for_status()
page_contributors = contributors_response.json()
if not page_contributors:
break
contributors.extend(page_contributors)
page += 1
repo_response = httpx.get('https://api.github.com/repos/zauberzeug/nicegui', headers=HEADERS, timeout=10.0)
repo_response.raise_for_status()
stars = repo_response.json()['stargazers_count']
print(f'Found {len(sponsors)} sponsors')
print(f'Total contributors for NiceGUI: {len(contributors)}')
print(f'Star count: {stars}')
json_path = Path('website/github_stats.json')
special_sponsors = json.loads(json_path.read_text(encoding='utf-8'))['special']
# NOTE: match against the directory listing instead of using Path.exists(),
# which is case-insensitive on macOS and would miss a mismatch that breaks the logo on the Linux server
logo_names = {path.name for path in Path('website/static/sponsors').iterdir()}
for sponsor in special_sponsors:
assert f'{sponsor}.webp' in logo_names or {f'{sponsor}.light.webp', f'{sponsor}.dark.webp'} <= logo_names, \
f'Logo for special sponsor "{sponsor}" is missing in website/static/sponsors'
top_sponsors = [
s['login']
for s in sponsors
# NOTE: LambdaTest-Inc is excluded because it appears as special sponsor "testmu-ai"
if s['tier_amount'] >= 100 and not s['tier_is_one_time'] and s['login'] not in {*special_sponsors, 'LambdaTest-Inc'}
]
json_path.write_text(json.dumps({
'special': special_sponsors,
'top': top_sponsors,
'total': len(sponsors),
'contributors': len(contributors),
'stars': stars,
}, indent=2) + '\n', encoding='utf-8')
URL_OVERRIDES = {
'LambdaTest-Inc': 'https://www.testmuai.com/?utm_medium=sponsor&utm_source=nicegui',
}
sponsor_html = '<p align="center">\n'
for sponsor in sponsors:
if sponsor['tier_amount'] >= 25 and not sponsor['tier_is_one_time']:
href = URL_OVERRIDES.get(sponsor['login'], sponsor['url'])
sponsor_html += f' <a href="{href}"><img src="{sponsor["url"]}.png" width="50px" alt="{sponsor["name"]}" /></a>\n'
sponsor_html += '</p>'
readme_path = Path('README.md')
readme_content = readme_path.read_text(encoding='utf-8')
updated_content = re.sub(
r'<!-- SPONSORS -->.*?<!-- SPONSORS -->',
f'<!-- SPONSORS -->\n\n{sponsor_html}\n<!-- SPONSORS -->',
readme_content,
flags=re.DOTALL,
)
readme_path.write_text(updated_content, encoding='utf-8')
print('README.md and github_stats.json updated successfully.')