forked from rahul-jha98/github-stats-transparent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_images.py
123 lines (101 loc) · 4.49 KB
/
generate_images.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
#!/usr/bin/python3
import asyncio
import os
import re
import aiohttp
from github_stats import Stats
################################################################################
# Helper Functions
################################################################################
def generate_output_folder() -> None:
"""
Create the output folder if it does not already exist
"""
if not os.path.isdir("generated"):
os.mkdir("generated")
################################################################################
# Individual Image Generation Functions
################################################################################
async def generate_overview(s: Stats) -> None:
"""
Generate an SVG badge with summary statistics
:param s: Represents user's GitHub statistics
"""
with open("templates/overview.svg", "r") as f:
output = f.read()
output = re.sub("{{ name }}", await s.name, output)
output = re.sub("{{ stars }}", f"{await s.stargazers:,}", output)
output = re.sub("{{ forks }}", f"{await s.forks:,}", output)
output = re.sub("{{ contributions }}", f"{await s.total_contributions:,}",
output)
changed = (await s.lines_changed)[0] + (await s.lines_changed)[1]
output = re.sub("{{ lines_changed }}", f"{changed:,}", output)
output = re.sub("{{ views }}", f"{await s.views:,}", output)
output = re.sub("{{ repos }}", f"{len(await s.all_repos):,}", output)
generate_output_folder()
with open("generated/overview.svg", "w") as f:
f.write(output)
async def generate_languages(s: Stats) -> None:
"""
Generate an SVG badge with summary languages used
:param s: Represents user's GitHub statistics
"""
with open("templates/languages.svg", "r") as f:
output = f.read()
progress = ""
lang_list = ""
sorted_languages = sorted((await s.languages).items(), reverse=True,
key=lambda t: t[1].get("size"))
delay_between = 150
for i, (lang, data) in enumerate(sorted_languages):
color = data.get("color")
color = color if color is not None else "#000000"
ratio = [.98, .02]
if data.get("prop", 0) > 50:
ratio = [.99, .01]
if i == len(sorted_languages) - 1:
ratio = [1, 0]
progress += (f'<span style="background-color: {color};'
f'width: {(ratio[0] * data.get("prop", 0)):0.3f}%;'
f'margin-right: {(ratio[1] * data.get("prop", 0)):0.3f}%;" '
f'class="progress-item"></span>')
lang_list += f"""
<li style="animation-delay: {i * delay_between}ms;">
<svg xmlns="http://www.w3.org/2000/svg" class="octicon" style="fill:{color};"
viewBox="0 0 16 16" version="1.1" width="16" height="16"><path
fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8z"></path></svg>
<span class="lang">{lang}</span>
<span class="percent">{data.get("prop", 0):0.2f}%</span>
</li>
"""
output = re.sub(r"{{ progress }}", progress, output)
output = re.sub(r"{{ lang_list }}", lang_list, output)
generate_output_folder()
with open("generated/languages.svg", "w") as f:
f.write(output)
################################################################################
# Main Function
################################################################################
async def main() -> None:
"""
Generate all badges
"""
access_token = os.getenv("ACCESS_TOKEN")
if not access_token:
# access_token = os.getenv("GITHUB_TOKEN")
raise Exception("A personal access token is required to proceed!")
user = os.getenv("GITHUB_ACTOR")
exclude_repos = os.getenv("EXCLUDED")
exclude_repos = ({x.strip() for x in exclude_repos.split(",")}
if exclude_repos else None)
exclude_langs = os.getenv("EXCLUDED_LANGS")
exclude_langs = ({x.strip() for x in exclude_langs.split(",")}
if exclude_langs else None)
consider_forked_repos = len(os.getenv("COUNT_STATS_FROM_FORKS")) != 0
async with aiohttp.ClientSession() as session:
s = Stats(user, access_token, session, exclude_repos=exclude_repos,
exclude_langs=exclude_langs,
consider_forked_repos=consider_forked_repos)
await asyncio.gather(generate_languages(s), generate_overview(s))
if __name__ == "__main__":
asyncio.run(main())