-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathrestar.py
More file actions
56 lines (42 loc) · 1.41 KB
/
restar.py
File metadata and controls
56 lines (42 loc) · 1.41 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
with open('_sections/30-projects.md') as f:
data = f.read()
import re
import sys
import requests
import requests_cache
requests_cache.install_cache('cache')
token = None
if len(sys.argv) == 2:
token = sys.argv[1]
headers = {}
if token:
headers = {
"Authorization": f"token {token}"
}
match = re.compile('github.com/([a-zA-Z0-9-]+)/([0-9a-zA-Z-._]+)/?')
with open('_sections/30-projects.md','w') as f:
for line in data.splitlines():
if not 'github' in line:
f.write(line+'\n')
continue
m = match.findall(line)
if m:
print('.', end='')
sys.stdout.flush()
org,repo = m[-1]
url = f'https://api.github.com/repos/{org}/{repo}'
info = requests.get(url, headers=headers).json()
llll = line.split(' <!')[0]
# do not write url:.... if the link url match the same.
# if len(m) == 2:
# if m[1] == m[0]:
# f.write(llll + f" <!-- sg:{info['stargazers_count']} -->\n")
# continue
count = info.get('stargazers_count', None)
if count:
f.write(llll + f" <!-- url:https://github.com/{org}/{repo} sg:{count} -->\n")
else:
print('skip', m, info)
f.write(line+'\n')
else:
f.write(line+'\n')