-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathfetch-contributors.py
56 lines (37 loc) · 1.58 KB
/
fetch-contributors.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
import re
import sys
import requests
import json
BASE_API_URL = 'https://api.github.com/repos/DishpitDev/Slopify'
def fetch_contributors():
page = 1
fetched_contributors = []
all_contributors = []
while page == 1 or fetched_contributors:
url = f'{BASE_API_URL}/contributors?per_page=100&page={page}'
response = requests.get(url)
fetched_contributors = response.json()
all_contributors += fetched_contributors
page += 1
return all_contributors
def get_rounded_img_url(url, size=64):
return f'https://images.weserv.nl/?url={url}?h={size}&w={size}&fit=cover&mask=circle'
def create_contributor_badge(contributor):
return f'<a href="{contributor["html_url"]}"><img src="{get_rounded_img_url(contributor["avatar_url"])}" alt="{contributor["login"]}"></a>'
def modify_readme(contributors):
with open('README.md', 'r', encoding='utf-8') as f:
readme = f.read()
badges = '\n'.join([create_contributor_badge(contributor) for contributor in contributors])
readme = re.sub(r'<!-- CONTRIBUTORS:START -->.*<!-- CONTRIBUTORS:END -->',
f'<!-- CONTRIBUTORS:START -->\n# Contributors ({len(contributors)})\n\n{badges}\n<!-- CONTRIBUTORS:END -->',
readme, flags=re.DOTALL)
with open('README.md', 'w', encoding='utf-8') as f:
f.write(readme)
def main():
contributors = fetch_contributors()
with open('contributors.json', 'w') as f:
json.dump(contributors, f)
if '--readme' in sys.argv:
modify_readme(contributors)
if __name__ == '__main__':
main()