-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_doc.py
158 lines (128 loc) · 4.86 KB
/
generate_doc.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
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
from os.path import isfile
import sys
from github import Github
import imgkit
import json
from PIL import Image
g = Github(sys.argv[1])
user = g.get_user()
cur_contrib = 0
with open("templates.json", "r") as j:
templates = json.load(j)
start = templates["start"].format(
user.login,
user.name if user.name is not None else user.login,
user.avatar_url,
user.name if user.name is not None else user.login,
)
middle = templates["middle"]
tail = templates["tail"]
with open("colors.json", "r") as j:
lang_colors = json.load(j)
if isfile("total_contribs"):
with open("total_contribs", "r") as f:
try:
prev_contrib = int(f.read())
except:
prev_contrib = -1
else:
prev_contrib = -1
def add_row(contribs, key, is_pr):
org, repo_name = key.split("/")
merged_link = contribs[key]["merged_url"]
open_link = contribs[key]["open_url"]
closed_link = contribs[key]["closed_url"]
n_merged = contribs[key]["n_merged"]
n_open = contribs[key]["n_open"]
n_closed = contribs[key]["n_closed"]
lang = contribs[key]["lang"]
n_days = contribs[key]["last_mod"]
stars = contribs[key]["stars"]
last_mod = ""
if n_days >= 365: last_mod = str(n_days // 365) + " years ago"
elif n_days > 30: last_mod = str(n_days // 30) + " months ago"
elif n_days > 0: last_mod = str(n_days) + " days ago"
else: last_mod = "today"
if last_mod.startswith('1'):
last_mod = last_mod.replace('days', 'day').replace('years', 'year').replace('months', 'month')
if is_pr and (n_merged + n_open) == 0:
return "", (n_merged + n_open + n_closed)
lang_color = lang_colors[lang] if lang in lang_colors else "rgb(0,255,0)"
repo_html = templates["pr_head"].format(org, repo_name, org, repo_name)
if n_merged:
repo_html += templates["pr_merged"].format(merged_link, n_merged)
if n_open:
repo_html += templates["pr_open"].format(open_link, n_open)
if n_closed:
repo_html += templates["pr_closed"].format(closed_link, n_closed)
repo_html += templates["pr_tail"].format(lang_color, lang, stars, last_mod)
return repo_html, (n_merged + n_open + n_closed)
def remove_prs_below_threshold(prs, threshold):
filtered_prs = {}
for repo, contrib in prs.items():
if contrib["n_merged"] + contrib["n_open"] >= threshold:
filtered_prs[repo] = contrib
return filtered_prs
def generate_readme_image(readme_prs, readme_pr_keys):
html = templates["readme_head"] + templates["readme_start"]
for key in readme_pr_keys:
code, _ = add_row(readme_prs, key, True)
html += code
html += templates["readme_tail"]
options = {
"xvfb": ""
}
imgkit.from_string(html, "contributions.png", options=options, )
img = Image.open("contributions.png")
width, height = img.size
print("before", width, height)
center = width // 2
img = img.crop((center - (385), 10, center + (115) , height))
print("After", width, height)
img.save("contributions.png")
def save_profile_readme_txt():
with open("profile_readme.txt", "w") as file:
content = ("[![My contributions]" +
"(https://{}.github.io/contributions.png)]".format(user.login) +
"(https://{}.github.io/contributions)".format(user.login))
file.write(content)
if __name__ == "__main__":
with open("my_contribs.json", "r") as j:
contribs = json.load(j)
contribs = json.loads(contribs)
try:
threshold = int(sys.argv[2])
except:
threshold = 0
issues = contribs["issues"]
prs = contribs["pulls"]
readme_prs = remove_prs_below_threshold(prs, threshold)
get_count_pr = lambda d: (prs[d]["n_open"] + prs[d]["n_merged"])
get_count_issue = lambda d: (issues[d]["n_open"] + issues[d]["n_closed"])
pr_keys = sorted(prs.keys(), key=get_count_pr, reverse=True)
issues_keys = sorted(issues.keys(), key=get_count_issue, reverse=True)
readme_pr_keys = sorted(readme_prs.keys(), key=get_count_pr, reverse=True)
html = templates["head"] + start
for key in pr_keys:
code, count = add_row(prs, key, is_pr=True)
html += code
cur_contrib += count
html += middle
for key in issues_keys:
code, count = add_row(issues, key, False)
html += code
cur_contrib += count
html += tail
# Exit if no new contributions
if prev_contrib == cur_contrib:
if not isfile("profile_readme.txt"):
save_profile_readme_txt()
if not isfile("contributions.png"):
generate_readme_image(readme_prs, readme_pr_keys)
exit()
generate_readme_image(readme_prs, readme_pr_keys)
save_profile_readme_txt()
with open("total_contribs", "w") as file:
file.write(str(cur_contrib))
with open("contributions.html", "w") as file:
file.write(html)